Class: Scale::API
- Inherits:
-
Object
- Object
- Scale::API
- Defined in:
- lib/scale/api.rb
Constant Summary collapse
- DEFAULT_PARAMS =
{ endpoint: 'https://api.scaleapi.com/v1/', default_request_params: {} }.freeze
Instance Attribute Summary collapse
-
#api_key ⇒ Object
readonly
Returns the value of attribute api_key.
-
#callback_key ⇒ Object
readonly
Returns the value of attribute callback_key.
-
#default_request_params ⇒ Object
readonly
Returns the value of attribute default_request_params.
-
#endpoint ⇒ Object
readonly
Returns the value of attribute endpoint.
-
#params ⇒ Object
readonly
Returns the value of attribute params.
Instance Method Summary collapse
- #build_callback(data, type = 'task', options = {}) ⇒ Object
-
#initialize(params = {}) ⇒ API
constructor
A new instance of API.
-
#method_missing(m, *array) ⇒ Object
Endpoint helper.
- #request(type, path, payload = {}) ⇒ Object
- #valid_callback_key?(key) ⇒ Boolean
Constructor Details
#initialize(params = {}) ⇒ API
Returns a new instance of API.
10 11 12 13 14 15 16 17 18 19 20 |
# File 'lib/scale/api.rb', line 10 def initialize(params = {}) params = DEFAULT_PARAMS.merge params @params = Scale.hash(params) @endpoint = fetch_attribute :endpoint @api_key = fetch_attribute :api_key @callback_key = fetch_attribute :callback_key @default_request_params = fetch_attribute :default_request_params validate! end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(m, *array) ⇒ Object
Endpoint helper. If the method is not defined, then try looking into the available endpoints.
50 51 52 53 54 |
# File 'lib/scale/api.rb', line 50 def method_missing(m, *array) endpoint = Scale.descendants(Scale::Endpoints::Endpoint).find { |e| e.match? m } return endpoint.new(self, *array).process if endpoint super end |
Instance Attribute Details
#api_key ⇒ Object (readonly)
Returns the value of attribute api_key.
8 9 10 |
# File 'lib/scale/api.rb', line 8 def api_key @api_key end |
#callback_key ⇒ Object (readonly)
Returns the value of attribute callback_key.
8 9 10 |
# File 'lib/scale/api.rb', line 8 def callback_key @callback_key end |
#default_request_params ⇒ Object (readonly)
Returns the value of attribute default_request_params.
8 9 10 |
# File 'lib/scale/api.rb', line 8 def default_request_params @default_request_params end |
#endpoint ⇒ Object (readonly)
Returns the value of attribute endpoint.
8 9 10 |
# File 'lib/scale/api.rb', line 8 def endpoint @endpoint end |
#params ⇒ Object (readonly)
Returns the value of attribute params.
8 9 10 |
# File 'lib/scale/api.rb', line 8 def params @params end |
Instance Method Details
#build_callback(data, type = 'task', options = {}) ⇒ Object
34 35 36 37 38 39 40 41 42 43 |
# File 'lib/scale/api.rb', line 34 def build_callback(data, type = 'task', = {}) = Scale.hash matchers = Scale.descendants(Scale::Callbacks::Base) klass = matchers.find { |c| c.match? type } validate_callback_handler! klass validate_callback_token! [:callback_key] klass.new data end |
#request(type, path, payload = {}) ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/scale/api.rb', line 22 def request(type, path, payload = {}) RestClient::Request.new( method: type, url: url(path), user: api_key, payload: Scale.hash(default_request_params).merge(Scale.hash(payload)).to_json, headers: { accept: :json, content_type: :json } ).execute rescue RestClient::Exception => e raise HttpError, e end |
#valid_callback_key?(key) ⇒ Boolean
45 46 47 |
# File 'lib/scale/api.rb', line 45 def valid_callback_key?(key) key.to_s == callback_key.to_s end |