Class: TripAdvisor::API
- Inherits:
-
Object
- Object
- TripAdvisor::API
- Defined in:
- lib/trip_advisor/api.rb
Instance Attribute Summary collapse
-
#api_key ⇒ Object
Returns the value of attribute api_key.
-
#base_url ⇒ Object
Returns the value of attribute base_url.
-
#verbose ⇒ Object
writeonly
Sets the attribute verbose.
Instance Method Summary collapse
- #get(method, params = {}) ⇒ Object
- #get_flights_locales ⇒ Object
- #get_locales ⇒ Object
-
#initialize(options = {}) ⇒ API
constructor
A new instance of API.
- #verbose? ⇒ Boolean
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(={}) require 'rest-client' require 'addressable/uri' require 'oj' self.api_key = [:api_key] self.base_url = [: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_key ⇒ Object
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_url ⇒ Object
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
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_locales ⇒ Object
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_locales ⇒ Object
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
29 30 31 |
# File 'lib/trip_advisor/api.rb', line 29 def verbose? @verbose end |