lib/zumata/client.rb in zumata-0.0.1 vs lib/zumata/client.rb in zumata-0.0.2
- old
+ new
@@ -7,23 +7,25 @@
module Zumata
class Client
include HTTParty
- raise Zumata::ClientConfigError unless ENV["ZUMATA_API_URL"]
- base_uri ENV["ZUMATA_API_URL"]
-
- def initialize api_key
- @api_key = api_key
+ def initialize opts={}
+ raise Zumata::ClientConfigError.new("No API URL configured") if Zumata.configuration.nil? || Zumata.configuration.api_url == ''
+ @api_url = Zumata.configuration.api_url
+ @api_key = opts[:api_key] unless opts[:api_key].nil?
@timeout = 600
end
+ def get_api_key
+ @api_key || Zumata.configuration.api_key
+ end
# GET /search
def search_by_destination destination, opts={}
- q = { api_key: opts[:api_key] || @api_key,
+ q = { api_key: opts[:api_key] || get_api_key,
destination: destination,
rooms: opts[:rooms] || 1,
adults: opts[:adults] || 2,
gzip: true,
currency: opts[:currency] || "USD" }
@@ -34,11 +36,11 @@
# optional
q[:lang] = opts[:lang] if opts[:lang]
q[:timeout] = opts[:timeout] if opts[:timeout]
- res = self.class.get("/search", query: q).response
+ res = self.class.get("#{@api_url}/search", query: q).response
# todo - handle errors from search
Zumata::GenericResponse.new(context: q, code: res.code.to_i, body: res.body)
end
@@ -48,15 +50,15 @@
def book booking_key, guest, payment, opts={}
# raise InvalidRequestError unless valid_guest_params?(guest)
# raise InvalidRequestError unless valid_payment_params?(payment)
- body_params = { api_key: opts[:api_key] || @api_key,
+ body_params = { api_key: opts[:api_key] || get_api_key,
booking_key: booking_key,
guest: guest,
payment: payment }
- res = self.class.post("/book", body: body_params.to_json, headers: { 'Content-Type' => 'application/json' }, timeout: @timeout)
+ res = self.class.post("#{@api_url}/book", body: body_params.to_json, headers: { 'Content-Type' => 'application/json' }, timeout: @timeout)
status_code = res.code.to_i
raise Zumata::GeneralError, res.body unless VALID_STATUS_CODES.include?(status_code)
case status_code
\ No newline at end of file