Module: Skiddle::Client

Includes:
HTTParty
Defined in:
lib/skiddle/client.rb

Class Method Summary collapse

Class Method Details

.convert_query_to_url(params) ⇒ Object



8
9
10
11
12
# File 'lib/skiddle/client.rb', line 8

def self.convert_query_to_url(params)
  params.map do |k,v|
    "&#{k}=#{v}"
  end.join
end

.make_request(klass, endpoint, params = {}) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/skiddle/client.rb', line 14

def self.make_request(klass, endpoint, params = {})
  query_url = convert_query_to_url(params)
  result = get("#{endpoint}?api_key=#{ENV['SKIDDLE_API_KEY']}#{query_url}")
  parsed_result = JSON.parse(result.body)
  if parsed_result["error"] == 1
  	parsed_result
  else
  	if parsed_result["results"].class == Array
    objects = parsed_result["results"].map do |result|
      klass.new(result)
    end
  		objects.length == 1 ? objects.first : objects
  else
    klass.new(result)
  end
  end
end