Class: Scale::Api
- Inherits:
-
Struct
- Object
- Struct
- Scale::Api
- Defined in:
- lib/scale/api.rb,
lib/scale/api/tasks.rb,
lib/scale/api/errors.rb,
lib/scale/api/callback.rb,
lib/scale/api/task_list.rb,
lib/scale/api/tasks/base_task.rb
Defined Under Namespace
Classes: APIKeyInvalid, BadRequest, Callback, ConnectionError, Error, InternalServerError, NotFound, TaskList, Tasks, TooManyRequests, Unauthorized
Constant Summary collapse
- SCALE_API_URL =
'https://api.scaleapi.com/v1/'- SCALEAPI_GEM_INFO =
Gem.loaded_specs["scaleapi"]
- SCALE_RUBY_CLIENT_VERSION =
SCALEAPI_GEM_INFO ? SCALEAPI_GEM_INFO.version.to_s : '0.1.1'.freeze
Instance Attribute Summary collapse
-
#api_key ⇒ Object
Returns the value of attribute api_key.
-
#callback_auth_key ⇒ Object
Returns the value of attribute callback_auth_key.
-
#default_request_params ⇒ Object
Returns the value of attribute default_request_params.
-
#logging ⇒ Object
Returns the value of attribute logging.
Instance Method Summary collapse
- #connection ⇒ Object
- #create_task(type, args = {}) ⇒ Object
- #get(url, params = {}) ⇒ Object
- #handle_error(response) ⇒ Object
- #post(url, body = {}) ⇒ Object
Instance Attribute Details
#api_key ⇒ Object
Returns the value of attribute api_key
6 7 8 |
# File 'lib/scale/api.rb', line 6 def api_key @api_key end |
#callback_auth_key ⇒ Object
Returns the value of attribute callback_auth_key
6 7 8 |
# File 'lib/scale/api.rb', line 6 def callback_auth_key @callback_auth_key end |
#default_request_params ⇒ Object
Returns the value of attribute default_request_params
6 7 8 |
# File 'lib/scale/api.rb', line 6 def default_request_params @default_request_params end |
#logging ⇒ Object
Returns the value of attribute logging
6 7 8 |
# File 'lib/scale/api.rb', line 6 def logging @logging end |
Instance Method Details
#connection ⇒ Object
11 12 13 14 15 16 17 18 |
# File 'lib/scale/api.rb', line 11 def connection @connection ||= Faraday.new(:url => SCALE_API_URL) do |faraday| faraday.request :basic_auth, self.api_key, '' faraday.request :url_encoded # form-encode POST params faraday.response :logger if logging # log requests to STDOUT faraday.adapter Faraday.default_adapter # make requests with Net::HTTP end end |
#create_task(type, args = {}) ⇒ Object
75 76 77 78 |
# File 'lib/scale/api.rb', line 75 def create_task(type, args = {}) response = post("task/#{type}", args) Api::Tasks::BaseTask.new(JSON.parse(response.body), self) end |
#get(url, params = {}) ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/scale/api.rb', line 20 def get(url, params = {}) response = connection.get do |req| req.url "#{SCALE_API_URL}#{url}" req.params.merge!(default_request_params.merge(params)) req.headers['X-API-Client'] = "Ruby" req.headers["X-API-Client-Version"] = SCALE_RUBY_CLIENT_VERSION end if response.status != 200 return handle_error(response) end response rescue Faraday::Error::ConnectionFailed raise Scale::Api::ConnectionError end |
#handle_error(response) ⇒ Object
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/scale/api.rb', line 58 def handle_error(response) error_body = JSON.parse(response.body) if response.status == 404 raise Scale::Api::NotFound.new(error_body['error'], response.status) elsif response.status == 429 raise Scale::Api::TooManyRequests.new(error_body['error'], response.status) elsif response.status > 499 raise Scale::Api::InternalServerError.new(error_body['error'], response.status) elsif response.status == 401 raise Scale::Api::.new(error_body['error'], response.status) else raise Scale::Api::BadRequest.new(error_body['error'], response.status) end rescue JSON::ParserError raise Scale::Api::InternalServerError end |
#post(url, body = {}) ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/scale/api.rb', line 37 def post(url, body = {}) body.delete(:callback_url) if body.keys.include?(:callback_url) && body[:callback_url].nil? body = default_request_params.merge(body) response = connection.post do |req| req.url "#{SCALE_API_URL}#{url}" req.headers['Content-Type'] = 'application/json' req.body = body.to_json req.headers['X-API-Client'] = "Ruby" req.headers["X-API-Client-Version"] = SCALE_RUBY_CLIENT_VERSION end if response.status != 200 return handle_error(response) end response rescue Faraday::Error::ConnectionFailed raise Scale::Api::ConnectionError end |