Class: MyVR::Client

Inherits:
Object
  • Object
show all
Includes:
API::Amenities, API::Fees, API::Photos, API::Properties, API::Rates, API::Rooms
Defined in:
lib/myvr/client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from API::Rooms

#room, #rooms, #rooms!

Methods included from API::Rates

#rate, #rates, #rates!

Methods included from API::Properties

#properties, #properties!, #property

Methods included from API::Photos

#photo, #photos, #photos!

Methods included from API::Fees

#fee, #fees, #fees!

Methods included from API::Amenities

#amenities, #amenities!, #fee

Constructor Details

#initialize(options = {}) {|_self| ... } ⇒ Client

Returns a new instance of Client.

Yields:

  • (_self)

Yield Parameters:

  • _self (MyVR::Client)

    the object that the method was called on



14
15
16
17
18
19
20
# File 'lib/myvr/client.rb', line 14

def initialize(options = {})
  @last_response = nil
  options.each do |key, value|
    instance_variable_set("@#{ key }", value)
  end
  yield(self) if block_given?
end

Instance Attribute Details

#access_keyObject

Returns the value of attribute access_key.



12
13
14
# File 'lib/myvr/client.rb', line 12

def access_key
  @access_key
end

#last_responseObject

Returns the value of attribute last_response.



12
13
14
# File 'lib/myvr/client.rb', line 12

def last_response
  @last_response
end

Instance Method Details

#base_url(path, params = {}) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/myvr/client.rb', line 48

def base_url(path, params={})
  if ENV["MYVR_TEST"]
    Addressable::URI.new(
      host: 'api.local.myvr.com',
      port: 8000,
      scheme: 'http',
      path: path,
      query_values: params
    )
  else
    Addressable::URI.new(
      host: 'api.myvr.com',
      port: 80,
      scheme: 'https',
      path: path,
      query_values: params
    )
  end
end

#get_object(path, params = {}) ⇒ Object



31
32
33
# File 'lib/myvr/client.rb', line 31

def get_object(path, params={})
  request(:get, path, params.dup)
end

#get_objects(path, params = {}) ⇒ Object



26
27
28
29
# File 'lib/myvr/client.rb', line 26

def get_objects(path, params={})
  response = request(:get, path, params.dup)
  response['results']
end

#request(http_method, path, params = {}) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/myvr/client.rb', line 35

def request(http_method, path, params={})
  url = base_url(path, params.dup)
  c = Curl::Easy.new(url)
  c.http_auth_types = :basic
  c.username = access_key
  c.send(http_method)
  if ENV["MYVR_TEST"]
    p JSON.parse(c.body_str)
  else
    JSON.parse(c.body_str)
  end
end

#user_agentObject



22
23
24
# File 'lib/myvr/client.rb', line 22

def user_agent
  @user_agent ||= "MyVRRubyGem/#{ MyVR::Version }"
end