Class: Skyscanner::Connection

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

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Connection

Returns a new instance of Connection.



55
56
57
58
59
60
61
# File 'lib/skyscanner.rb', line 55

def initialize(options = {})
  @options = self.class.options.merge({}.tap do |opts|
    options.each do |k, v|
      opts[k.to_sym] = v
    end
  end)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *params) ⇒ Object (private)



187
188
189
# File 'lib/skyscanner.rb', line 187

def method_missing(method, *params)
  @options.keys.include?(method.to_sym) && params.first.nil? ? @options[method.to_sym] : super
end

Instance Method Details

#api_keyObject



87
88
89
# File 'lib/skyscanner.rb', line 87

def api_key
  @options[:api_key]
end

#browse_dates(params = {}) ⇒ Object



164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/skyscanner.rb', line 164

def browse_dates(params = {})
  segments = {
    :country => true,
    :currency => true,
    :locale => true,
    :originPlace => true,
    :destinationPlace => true,
    :outboundPartialDate => true,
    :inboundPartialDate => false
  }
  request('browsedates', segments, params)
end

#browse_grid(params = {}) ⇒ Object



138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/skyscanner.rb', line 138

def browse_grid(params = {})
  segments = {
    :country => true,
    :currency => true,
    :locale => true,
    :originPlace => true,
    :destinationPlace => true,
    :outboundPartialDate => true,
    :inboundPartialDate => false
  }
  request('browsegrid', segments, params)
end

#browse_quotes(params = {}) ⇒ Object



125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/skyscanner.rb', line 125

def browse_quotes(params = {})
  segments = {
    :country => true,
    :currency => true,
    :locale => true,
    :originPlace => true,
    :destinationPlace => true,
    :outboundPartialDate => true,
    :inboundPartialDate => false
  }
  request('browsequotes', segments, params)
end

#browse_routes(params = {}) ⇒ Object



151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/skyscanner.rb', line 151

def browse_routes(params = {})
  segments = {
    :country => true,
    :currency => true,
    :locale => true,
    :originPlace => true,
    :destinationPlace => true,
    :outboundPartialDate => true,
    :inboundPartialDate => false
  }
  request('browseroutes', segments, params)
end

#endpoint_uri(method) ⇒ Object



177
178
179
# File 'lib/skyscanner.rb', line 177

def endpoint_uri(method)
  "#{protocol}://#{url}/#{method}/#{version}/"
end

#handle_response(response) ⇒ Object



63
64
65
66
67
68
69
# File 'lib/skyscanner.rb', line 63

def handle_response(response)
  if response_format == :ruby and response.status == 200
    MultiJson.decode(response.body)
  else
    { :status => response.status, :body => response.body }
  end
end

#idObject

Ruby 1.8.7 / ree compatibility



72
73
74
# File 'lib/skyscanner.rb', line 72

def id
  @options[:id]
end

#request(url, segments, params) ⇒ Object



76
77
78
79
80
81
# File 'lib/skyscanner.rb', line 76

def request(url, segments, params)
  handle_response(Faraday.new(*builder(url, segments_to_path(segments, params), params.clone)) do |build|
    build.adapter adapter
    #        build.use Faraday::Response::VerboseLogger, logger unless logger.nil?
  end.get)
end

#response_formatObject



83
84
85
# File 'lib/skyscanner.rb', line 83

def response_format
  @options[:response_format].to_sym
end

#segments_to_path(segments, params) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/skyscanner.rb', line 91

def segments_to_path(segments, params)
  out = []
  segments.each do |k, v|
    value = params[k]
    if value then
      out << value
    elsif v then
      raise ArgumentError.new("Mandatory parameter (#{k}) missing")
    end
  end
  out.join('/')
end

#site_redirect(params = {}) ⇒ Object



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/skyscanner.rb', line 104

def site_redirect(params = {})
  segments = {
    :country => true,
    :currency => true,
    :locale => true,
    :originPlace => true,
    :destinationPlace => true,
    :outboundPartialDate => true,
    :inboundPartialDate => false,
    :associateID => false,
    :utm_source => false,
    :utm_medium => false,
    :utm_name => false
  }

#      request('skyscannerredirect', segments, params)
  # Life isn't that easy - in this case the constructed URL is what we return
  ret = builder("referral", segments_to_path(segments, params), params.clone)
  "#{ret[0]}?apiKey=#{api_key}"
end

#uri(path) ⇒ Object



181
182
183
# File 'lib/skyscanner.rb', line 181

def uri(path)
  "#{protocol}://#{url}/#{path}/#{version}"
end