Class: FlightCaster::Request

Inherits:
Object
  • Object
show all
Defined in:
lib/flightcaster/request.rb

Constant Summary collapse

API_ROOT =
"http://api.flightcaster.com"

Class Method Summary collapse

Class Method Details

.expand(params) ⇒ Object



45
46
47
48
49
50
51
# File 'lib/flightcaster/request.rb', line 45

def self.expand(params)
  final = ''
  params.each do |k, v|
    final << "&#{k}=#{v}"
  end
  final
end

.full_uri(path, params = {}) ⇒ Object



16
17
18
19
# File 'lib/flightcaster/request.rb', line 16

def self.full_uri(path, params={})
  options = expand(params)
  "#{API_ROOT}#{path}?api_key=#{@api_key}#{options}"
end

.get(path, params = {}) ⇒ Object



10
11
12
13
14
# File 'lib/flightcaster/request.rb', line 10

def self.get(path, params={})
  uri = full_uri(path, params)
  response = HTTParty.get(uri)
  make_friendly(response)
end

.make_friendly(response) ⇒ Object



21
22
23
24
25
26
27
28
29
30
# File 'lib/flightcaster/request.rb', line 21

def self.make_friendly(response)
  begin
    raise_errors(response)
    data = parse(response)
    h = Hashie::Mash.new(data)
    h = h[h.keys[0]]
    FlightCaster::Rash.new(h)
  rescue
  end
end

.parse(response) ⇒ Object



41
42
43
# File 'lib/flightcaster/request.rb', line 41

def self.parse(response)
  Crack::XML.parse(response.body)
end

.raise_errors(response) ⇒ Object



32
33
34
35
36
37
38
39
# File 'lib/flightcaster/request.rb', line 32

def self.raise_errors(response)
  case response.code.to_i
    when 404
      raise NotFound, "(#{response.code}): #{response.message}"
    when 422
      raise OldAPI, "The API version you are using is no longer supported. (#{response.code}): #{response.message}"
  end
end

.set_api_key(api_key) ⇒ Object



6
7
8
# File 'lib/flightcaster/request.rb', line 6

def self.set_api_key(api_key)
  @api_key = api_key
end