Module: LittleBird::Response

Included in:
Client
Defined in:
lib/client.rb,
lib/response.rb

Instance Method Summary collapse

Instance Method Details

#error(response) ⇒ Object



3
4
5
6
7
8
9
10
# File 'lib/response.rb', line 3

def error(response)
  {
    authorization: AuthorizationError.new(response),
    internal: InternalError.new(response),
    parameter: ParameterError.new(response),
    rate_limit: RateLimitError.new(response)
  }[response["error_type"].to_sym]
end

#interpret(response, cast_class) ⇒ Object

Raises:



16
17
18
19
20
21
22
23
# File 'lib/response.rb', line 16

def interpret(response, cast_class)
  raise error(response), response["error"] if response_is_an_error(response)
  if cast_class.class == Array
    return iterate(response, cast_class.first)
  else
    return respond_as_class(response, cast_class)
  end
end

#iterate(response, cast_class) ⇒ Object



25
26
27
28
29
# File 'lib/response.rb', line 25

def iterate(response, cast_class)
  response.map do |response_obj|
    respond_as_class(response_obj, cast_class)
  end
end

#respond_as_class(response_obj, cast_class) ⇒ Object



36
37
38
39
40
# File 'lib/response.rb', line 36

def respond_as_class(response_obj, cast_class)
  object = cast_class.new(response_obj)
  set_client(object)
  object
end

#response_is_an_error(response) ⇒ Object



12
13
14
# File 'lib/response.rb', line 12

def response_is_an_error(response)
  response.class == Hash && !response["error"].nil?
end

#set_client(object) ⇒ Object



31
32
33
34
# File 'lib/response.rb', line 31

def set_client(object)
  object.instance_eval { class << self; self end }.send(:attr_accessor, :client)
  object.client = self
end