Method: CurrencyLayer::Client#timeframe

Defined in:
lib/currency_conversion.rb

#timeframe(start_date, end_date, currencies, options = {}) ⇒ Object



215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
# File 'lib/currency_conversion.rb', line 215

def timeframe(start_date, end_date, currencies, options = {})

  if start_date.nil?
    raise CurrencyLayer::MissingArgumentException.new 'start_date'
    return
  end

  if end_date.nil?
    raise CurrencyLayer::MissingArgumentException.new 'end_date'
    return
  end

  if currencies.nil?
    raise CurrencyLayer::MissingArgumentException.new 'currencies'
    return
  end

  # Create a shallow copy so we don't manipulate the original reference
  q = options.dup

  # Populate the Query
  q.access_key = @access_key
  q.start_date = start_date
  q.end_date = end_date
  q.currencies = currencies

  # We then create the Request
  req = CurrencyLayer::TimeframeRequest.new(q)

  #  We create a Hash of the request so we can send it via HTTP
  req_dto = req.to_dh

  begin

    # We make the actual request
    res = self.class.get('/timeframe', req_dto)

    # We ensure that we tap the response so we can use the results
    res.inspect

    if (res[CurrencyLayer::TimeframeResponse::ERROR_EXPR])
      raise CurrencyLayer::TimeframeException.new res[CurrencyLayer::TimeframeResponse::ERROR_EXPR]
    end

    # We just return the parsed binary response
    return res.parsed_response

  rescue => e
    puts e.inspect
    return e

  end
end