class Scale::API
Constants
- DEFAULT_PARAMS
Attributes
api_key[R]
callback_key[R]
default_request_params[R]
endpoint[R]
params[R]
Public Class Methods
new(params = {})
click to toggle source
# 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
Public Instance Methods
build_callback(data, type = 'task', options = {})
click to toggle source
# File lib/scale/api.rb, line 34 def build_callback(data, type = 'task', options = {}) options = Scale.hash options matchers = Scale.descendants(Scale::Callbacks::Base) klass = matchers.find { |c| c.match? type } validate_callback_handler! klass validate_callback_token! options[:callback_key] klass.new data end
method_missing(m, *array)
click to toggle source
Endpoint helper. If the method is not defined, then try looking into the available endpoints.
Calls superclass method
# 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
request(type, path, payload = {})
click to toggle source
# 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)
click to toggle source
# File lib/scale/api.rb, line 45 def valid_callback_key?(key) key.to_s == callback_key.to_s end
Protected Instance Methods
fetch_attribute(name)
click to toggle source
# File lib/scale/api.rb, line 64 def fetch_attribute(name) params[name] || send(name) end
invalid_attribute(name)
click to toggle source
# File lib/scale/api.rb, line 99 def invalid_attribute(name) "Invalid Scale API `#{name}`. Please, set a valid `#{name}` parameter" end
url(path)
click to toggle source
# File lib/scale/api.rb, line 68 def url(path) "#{endpoint.end_with?('/') ? endpoint : endpoint + '/'}#{path}" end
validate!()
click to toggle source
# File lib/scale/api.rb, line 58 def validate! validate_endpoint! validate_api_key! validate_callback_key! end
validate_api_key!()
click to toggle source
# File lib/scale/api.rb, line 95 def validate_api_key! raise GenericError, invalid_attribute('api_key') unless api_key.to_s != '' end
validate_callback_handler!(klass)
click to toggle source
Validators
# File lib/scale/api.rb, line 76 def validate_callback_handler!(klass) raise GenericError, "Callback handler '#{name}' not found. Try #{matchers.map(&:shortcut).join ','}" unless klass end
validate_callback_key!()
click to toggle source
# File lib/scale/api.rb, line 85 def validate_callback_key! return if callback_key.nil? raise GenericError, invalid_attribute('callback_key') unless api_key.to_s != '' end
validate_callback_token!(key)
click to toggle source
# File lib/scale/api.rb, line 80 def validate_callback_token!(key) return if callback_key.nil? raise GenericError, "Invalid HTTP callback with key #{key}" unless valid_callback_key? key end
validate_endpoint!()
click to toggle source
# File lib/scale/api.rb, line 90 def validate_endpoint! valid = URI.parse "endpoint" rescue nil raise GenericError, invalid_attribute('endpoint') unless valid end