Method: CurrencyLayer::Client#change

Defined in:
lib/currency_conversion.rb

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



269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
# File 'lib/currency_conversion.rb', line 269

def change(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::ChangeRequest.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('/change', req_dto)

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

    if (res[CurrencyLayer::ChangeResponse::ERROR_EXPR])
      raise CurrencyLayer::ChangeException.new res[CurrencyLayer::ChangeResponse::ERROR_EXPR]
    end

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

  rescue => e
    puts e.inspect
    return e

  end
end