Class: Scale::API

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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_keyObject (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_keyObject (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_paramsObject (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

#endpointObject (readonly)

Returns the value of attribute endpoint.



8
9
10
# File 'lib/scale/api.rb', line 8

def endpoint
  @endpoint
end

#paramsObject (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', 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

#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

Returns:

  • (Boolean)


45
46
47
# File 'lib/scale/api.rb', line 45

def valid_callback_key?(key)
  key.to_s == callback_key.to_s
end