Class: TripAdvisor::API

Inherits:
Object
  • Object
show all
Defined in:
lib/trip_advisor/api.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ API

Returns a new instance of API.



6
7
8
9
10
11
12
13
14
# File 'lib/trip_advisor/api.rb', line 6

def initialize(options={})
  require 'rest-client'
  require 'addressable/uri'
  require 'oj'

  self.api_key = options[:api_key]
  self.base_url = options[:base_url] || ENV['TRIPADVISOR_API_BASE_URL'] || 'https://www.tripadvisor.com/api/internal/1.0/'
  raise "API Key cannot be nil" unless api_key
end

Instance Attribute Details

#api_keyObject

Returns the value of attribute api_key.



3
4
5
# File 'lib/trip_advisor/api.rb', line 3

def api_key
  @api_key
end

#base_urlObject

Returns the value of attribute base_url.



3
4
5
# File 'lib/trip_advisor/api.rb', line 3

def base_url
  @base_url
end

#verbose=(value) ⇒ Object (writeonly)

Sets the attribute verbose

Parameters:

  • value

    the value to set the attribute verbose to.



4
5
6
# File 'lib/trip_advisor/api.rb', line 4

def verbose=(value)
  @verbose = value
end

Instance Method Details

#get(method, params = {}) ⇒ Object



33
34
35
36
37
38
# File 'lib/trip_advisor/api.rb', line 33

def get(method, params = {})    
  query_string = "?%s" % Addressable::URI.form_encode(params.merge('key' => api_key))
  url = Addressable::URI.join(base_url, method.to_s, query_string)
  puts_verbose("GET URL '#{url}'")
  RestClient.get(url.to_s)
end

#get_flights_localesObject



25
26
27
# File 'lib/trip_advisor/api.rb', line 25

def get_flights_locales
  get_locales.select { |locale| locale['is_mobile_flights_enabled'] == 'true' }
end

#get_localesObject



20
21
22
23
# File 'lib/trip_advisor/api.rb', line 20

def get_locales
  response = get(:locales)
  Oj.load(response)['data']
end

#verbose?Boolean

Returns:

  • (Boolean)


29
30
31
# File 'lib/trip_advisor/api.rb', line 29

def verbose?
  @verbose
end