Class: TrustedSearch::APIResponse

Inherits:
Object
  • Object
show all
Defined in:
lib/trustedsearch/api_response.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(response) ⇒ APIResponse

Returns a new instance of APIResponse.



5
6
7
8
9
10
11
12
13
14
15
# File 'lib/trustedsearch/api_response.rb', line 5

def initialize(response)
			@request = response.request
  @code = response.code
  #handle case where body has no response and thus JSON.parse requires octet aka a byte.
  @data = (response.body.length >=2) ? JSON.parse(response.body.strip) : nil

  #Lets symbolize Version responses.
  if(TrustedSearch::api_version == 2)
  	@data = self.symbolize_keys(@data)
  end
end

Instance Attribute Details

#codeObject (readonly)

Returns the value of attribute code.



3
4
5
# File 'lib/trustedsearch/api_response.rb', line 3

def code
  @code
end

#dataObject (readonly)

Returns the value of attribute data.



3
4
5
# File 'lib/trustedsearch/api_response.rb', line 3

def data
  @data
end

#metaObject (readonly)

Returns the value of attribute meta.



3
4
5
# File 'lib/trustedsearch/api_response.rb', line 3

def meta
  @meta
end

#requestObject (readonly)

Returns the value of attribute request.



3
4
5
# File 'lib/trustedsearch/api_response.rb', line 3

def request
  @request
end

Instance Method Details

#symbolize_keys(x) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/trustedsearch/api_response.rb', line 17

def symbolize_keys(x)
  if x.is_a? Hash
    x.inject({}) do |result, (key, value)|
      new_key = case key
                when String then key.to_sym
                else key
                end
      new_value = case value
                  when Hash then symbolize_keys(value)
                  when Array then symbolize_keys(value)
                  else value
                  end
      result[new_key] = new_value
      result
    end
  elsif x.is_a? Array
    x.map {|el| symbolize_keys(el)}
  else
    x
  end
end