class NinetyNine::Tasks::ApiClient
Public Class Methods
new(apikey, base_url: 'https://api.99designs.com/tasks/v1')
click to toggle source
# File lib/99designs/tasks/api_client.rb, line 9 def initialize(apikey, base_url: 'https://api.99designs.com/tasks/v1') @apikey = apikey @conn = Faraday.new(base_url) do |f| f.basic_auth apikey, '' f.request :multipart f.request :url_encoded # f.response :logger # DELETEME f.adapter Faraday.default_adapter end end
Public Instance Methods
approve_delivery(task_id, delivery_id)
click to toggle source
# File lib/99designs/tasks/api_client.rb, line 69 def approve_delivery(task_id, delivery_id) response = request :post, "tasks/#{task_id}/deliveries/#{delivery_id}/approve", nil interpret_response(response) end
attach_files(task_id, urls: [], filenames: [])
click to toggle source
# File lib/99designs/tasks/api_client.rb, line 45 def attach_files(task_id, urls: [], filenames: []) response = request :post, "tasks/#{task_id}/attachments", { attachment: files(urls, filenames) } interpret_response(response) end
create_task(body: nil, urls: [], filenames: [], webhook_url: nil, external_id: nil)
click to toggle source
# File lib/99designs/tasks/api_client.rb, line 20 def create_task(body: nil, urls: [], filenames: [], webhook_url: nil, external_id: nil) response = request :post, "tasks", { body: body, attachment: files(urls, filenames), webhook_url: webhook_url, external_id: external_id } interpret_response(response) end
delete_attachment(task_id, attachment_id)
click to toggle source
# File lib/99designs/tasks/api_client.rb, line 51 def delete_attachment(task_id, attachment_id) response = request :delete, "tasks/#{task_id}/attachments/#{attachment_id}", nil interpret_response(response) end
get_task(task_id)
click to toggle source
# File lib/99designs/tasks/api_client.rb, line 26 def get_task(task_id) response = request :get, "tasks/#{task_id}", nil interpret_response(response) end
my_tasks(external_id: nil, page: nil, per_page: nil)
click to toggle source
# File lib/99designs/tasks/api_client.rb, line 32 def my_tasks(external_id: nil, page: nil, per_page: nil) params = { external_id: external_id, page: page, per_page: per_page }.delete_if { |k, v| v.nil? } response = request :get, "tasks", nil, params: params interpret_response(response) end
post_comment(task_id, comment)
click to toggle source
# File lib/99designs/tasks/api_client.rb, line 57 def post_comment(task_id, comment) response = request :post, "tasks/#{task_id}/comment", { comment: comment } interpret_response(response) end
request_revision(task_id, delivery_id, revision_type, comment)
click to toggle source
# File lib/99designs/tasks/api_client.rb, line 63 def request_revision(task_id, delivery_id, revision_type, comment) response = request :post, "tasks/#{task_id}/deliveries/#{delivery_id}/request_revision", { revision_type: revision_type, comment: comment } interpret_response(response) end
update_task(task_id, params)
click to toggle source
# File lib/99designs/tasks/api_client.rb, line 39 def update_task(task_id, params) response = request :put,"tasks/#{task_id}", { body: params[:body] } interpret_response(response) end
Private Instance Methods
content_type(filename)
click to toggle source
# File lib/99designs/tasks/api_client.rb, line 130 def content_type(filename) types = MIME::Types.type_for(filename) if types.empty? 'application/octet-stream' else types.first.content_type end end
error_for_response(status, body, json)
click to toggle source
# File lib/99designs/tasks/api_client.rb, line 105 def error_for_response(status, body, json) case status when 400 NinetyNine::ValidationError.new(json['message'], status, body, json) when 401 NinetyNine::AuthenticationError.new(json['message'], status, body, json) when 402 NinetyNine::PaymentError.new(json['message'], status, body, json) when 404 NinetyNine::NotFoundError.new(json['message'], status, body, json) when 422 NinetyNine::InvalidStateError.new(json['message'], status, body, json) else NinetyNine::ApiError.new(json['message'] || "Unexpected error: \"#{body}\" (http status: #{status})", status, body, json) end end
files(urls, filenames)
click to toggle source
# File lib/99designs/tasks/api_client.rb, line 122 def files(urls, filenames) uploads = filenames.map do |filename| Faraday::UploadIO.new(filename, content_type(filename)) end urls + uploads end
interpret_response(response)
click to toggle source
# File lib/99designs/tasks/api_client.rb, line 84 def interpret_response(response) status = response.status body = response.body begin json = JSON.parse(body, symbolize_names: true) if body != '' rescue JSON::ParserError raise NinetyNine::ApiError.new("Unexpected response: \"#{body}\" (http status: #{status})", status, body, nil) end if response_ok?(status) return json else raise error_for_response(status, body, json) end end
request(method, path, body, headers: {}, params: nil)
click to toggle source
# File lib/99designs/tasks/api_client.rb, line 78 def request(method, path, body, headers: {}, params: nil) @conn.run_request(method, path, body, headers) do |request| request.params.update(params) if params end end
response_ok?(status)
click to toggle source
# File lib/99designs/tasks/api_client.rb, line 101 def response_ok?(status) (200..299).include?(status) end