Class: Rome2rio::Connection

Inherits:
Object
  • Object
show all
Defined in:
lib/rome2rio/connection.rb

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Connection

Returns a new instance of Connection.



3
4
5
6
# File 'lib/rome2rio/connection.rb', line 3

def initialize(opts = {})
  @apikey = opts[:apikey]
  @endpoint = opts[:endpoint] || "http://free.rome2rio.com"
end

Instance Method Details

#encode_params(params) ⇒ Object



16
17
18
# File 'lib/rome2rio/connection.rb', line 16

def encode_params(params)
  URI.encode_www_form(params)
end

#handle_response(response) ⇒ Object



8
9
10
11
12
13
14
# File 'lib/rome2rio/connection.rb', line 8

def handle_response(response)
  if response.status == 200
    SearchResponse.new(MultiJson.decode(response.body))
  else
    {:status => response.status, :body => response.body}
  end
end

#search(opts) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/rome2rio/connection.rb', line 20

def search(opts)
  opts[:key] ||= @apikey if @apikey

  # format SearchFlags
  flags = opts[:flags]
  opts[:flags] = SearchRequestFlags.new(flags)  if flags.is_a?(Symbol) or flags.is_a?(Array)

  request = "/api/1.2/json/Search?#{encode_params(opts)}"

  handle_response(Faraday.new(:url => @endpoint).get(request))

  #      handle_response(conn.get do |req|
  #	  req.headers['Content-Type'] = 'application/json'
  #	  req.url request_url
  #      end)
end