Module: Kigo

Defined in:
lib/kigo.rb,
lib/kigo/version.rb,
lib/kigo/configuration.rb

Defined Under Namespace

Classes: Configuration

Constant Summary collapse

APIUrl =
'https://app.kigo.net/api/ra/v1'
VERSION =
"0.0.1"

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.configurationObject

Configure Kigo someplace sensible, like config/initializers/kigo.rb

Examples:

Kigo.configure do |config|
  config.username = 'your username'
  config.password = 'your password'
end


19
20
21
# File 'lib/kigo/configuration.rb', line 19

def configuration
  @configuration
end

Class Method Details

.access(end_point, request_method, data = {}, headers = {}) ⇒ Object



35
36
37
38
39
40
41
42
43
# File 'lib/kigo.rb', line 35

def self.access(end_point, request_method, data={}, headers={})
  request = self.wrap_request(end_point, request_method, data, headers)
  request.on_complete do |response|
    return self.parse_response(response)
  end
  hydra = Typhoeus::Hydra.new
  hydra.queue(request)
  hydra.run
end

.access_multiple(resources = []) ⇒ Object



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

def self.access_multiple(resources=[])
  hydra = Typhoeus::Hydra.new
  request_responses = []

  resources.each do |resource|
    request = self.wrap_request(resource[:end_point], resource[:verb], resource[:params], resource[:headers])
    request.on_complete do |response|
      request_responses << self.parse_response(response)
    end
    hydra.queue(request)
  end
  hydra.run
  request_responses
end

.configure {|configuration| ... } ⇒ Object

Yields:



23
24
25
# File 'lib/kigo/configuration.rb', line 23

def self.configure
  yield(configuration)
end

.createConfirmedReservation(params) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/kigo.rb', line 90

def self.createConfirmedReservation(params)
  # params example:
  # {
  #   "PROP_ID" => 1434,
  #   "RES_CHECK_IN" => "2011-07-21",
  #   "RES_CHECK_OUT" => "2011-07-26",
  #   "RES_N_ADULTS" => 2,
  #   "RES_N_CHILDREN" => 1,
  #   "RES_N_BABIES" => 0,
  #   "RES_GUEST" => {
  #     "RES_GUEST_FIRSTNAME" => "Robert",
  #     "RES_GUEST_LASTNAME" => "Roquefort",
  #     "RES_GUEST_EMAIL" => "[email protected]",
  #     "RES_GUEST_PHONE" => "",
  #     "RES_GUEST_COUNTRY" => "GB"
  #   },
  #   "RES_COMMENT" => "",
  #   "RES_COMMENT_GUEST" => "",
  #   "RES_UDRA" => [
  #     {
  #       "UDRA_ID" => 161,
  #       "UDRA_CHOICE_ID" => 199
  #     },
  #     {
  #       "UDRA_ID" => 162,
  #       "UDRA_TEXT" => "John Smith told me about you!"
  #     }
  #   ]
  # }
  self.access('/createConfirmedReservation', :post, params)
end

.diff_property_calendar_reservations(diff_id = nil) ⇒ Object

Reservations



86
87
88
# File 'lib/kigo.rb', line 86

def self.diff_property_calendar_reservations(diff_id = nil)
  self.access('/diffPropertyCalendarReservations', :post, { 'DIFF_ID' => diff_id })
end

.diff_property_pricing_setup(diff_id = nil) ⇒ Object



136
137
138
# File 'lib/kigo.rb', line 136

def self.diff_property_pricing_setup(diff_id = nil)
  self.access('/diffPropertyPricingSetup', :post, { 'DIFF_ID' => diff_id })
end

.filter_array_params(array) ⇒ Object



76
77
78
# File 'lib/kigo.rb', line 76

def self.filter_array_params(array)
  return array.map { |a| a.is_a?(Hash) ? self.filter_params(a) : a }
end

.filter_params(hash) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/kigo.rb', line 58

def self.filter_params(hash)
  filtered_attr = {}
  hash.each do |param_name, value|
    if value.is_a? Hash
      filtered_attr = filtered_attr.merge(param_name => filter_params(value))
    elsif value.is_a? Array
      filtered_attr = filtered_attr.merge(param_name => filter_array_params(value))
    else
      if value.class == ActionDispatch::Http::UploadedFile
        filtered_attr = filtered_attr.merge(param_name => value.tempfile)
      else
        filtered_attr = filtered_attr.merge(param_name => value)
      end
    end
  end
  return filtered_attr
end

.list_propertiesObject

Properties



123
124
125
# File 'lib/kigo.rb', line 123

def self.list_properties
  self.access('/listProperties', :post)
end

.parse_response(response) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/kigo.rb', line 45

def self.parse_response(response)
  if response.success?
    begin
      result = JSON.parse(response.body)
      return result["API_RESULT_CODE"] == "E_OK" ? result["API_REPLY"] : { 'error' => "#{result["API_RESULT_CODE"]} #{result["API_RESULT_TEXT"]}" }
    rescue
      return { 'error' => "Error parsing reply" }
    end
  else
    return { 'error' => "Response error. #{response.code} #{response.body}" }
  end
end

.pingObject



80
81
82
# File 'lib/kigo.rb', line 80

def self.ping
  self.access('/ping', :post, {})
end

.read_property(property_id) ⇒ Object



127
128
129
# File 'lib/kigo.rb', line 127

def self.read_property(property_id)
  self.access('/readProperty', :post, { 'PROP_ID' => property_id })
end

.read_property_pricing_setup(property_id) ⇒ Object

Pricing



132
133
134
# File 'lib/kigo.rb', line 132

def self.read_property_pricing_setup(property_id)
  self.access('/readPropertyPricingSetup', :post, { 'PROP_ID' => property_id })
end

.update_property_pricing_setup(property_id, pricing) ⇒ Object

Example of pricing hash:

{ "RENT" => {
    "PERGUEST_CHARGE" => nil,
    "PERIODS" => [
      { "CHECK_IN" => "2014-01-01",
        "CHECK_OUT" => "2014-03-01",
        "NAME" => "Winter 2014",
        "STAY_MIN" => { "UNIT" => "NIGHT", "NUMBER" => 3 },
        "WEEKLY" => false,
        "NIGHTLY_AMOUNTS" => [
          { "GUESTS_FROM" => 1,
            "WEEK_NIGHTS" => [ 1, 2, 3, 4, 5, 6, 7 ],
            "STAY_FROM" => { "UNIT" => "NIGHT", "NUMBER" => 1 },
            "AMOUNT" => "100.00"
          }
        ]
      },
      { "CHECK_IN" => "2014-03-01",
        "CHECK_OUT" => "2014-05-31",
        "NAME" => "",
        "STAY_MIN" => { "UNIT" => "NIGHT", "NUMBER" => 7 },
        "WEEKLY" => true,
        "WEEKLY_AMOUNTS" => [
          { "GUESTS_FROM" => 1,
            "AMOUNT" => "650.00"
          }
        ]
      }
    ]
  }
}


171
172
173
# File 'lib/kigo.rb', line 171

def self.update_property_pricing_setup(property_id, pricing)
  self.access('/updatePropertyPricingSetup', :post, { 'PROP_ID' => property_id, 'PRICING' => pricing })
end

.wrap_request(end_point, request_method, data = {}, headers = {}) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
# File 'lib/kigo.rb', line 8

def self.wrap_request(end_point, request_method, data={}, headers={})
  basic_auth = { username: Kigo.configuration.username, password: Kigo.configuration.password }
  encoded_auth = "Basic #{Base64.strict_encode64("#{basic_auth[:username]}:#{basic_auth[:password]}")}"
  data = self.filter_params(data)
  request_options = { method: request_method, headers: headers.merge(
      { 'Authorization' => encoded_auth,
        'Content-Type' => 'application/json' }
    ) }
  request_options = request_options.merge(request_method == :post ? { body: data.to_json } : { params: data.to_json })
  Typhoeus::Request.new("#{APIUrl}#{end_point}", request_options)
end