-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathswop_client.rb
More file actions
42 lines (32 loc) · 1.09 KB
/
swop_client.rb
File metadata and controls
42 lines (32 loc) · 1.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
require 'rubygems'
require 'bundler/setup'
require 'httparty'
class SwopClient
include HTTParty
base_uri 'https://swop.cx/rest'
def initialize(api_key)
@options = { headers: {"Authorization" => "ApiKey #{api_key}"} }
end
def single_rate(base_currency, quote_currency, date: nil)
query_params = build_query_params(Hash.new.tap do |qp|
qp[:date] = date.iso8601 if date
end)
self.class.get("/rates/#{base_currency}/#{quote_currency}#{query_params}", @options)
end
def timeseries(from_date, to_date, base: nil, targets: nil, include_meta: nil)
return if from_date.nil?
return if to_date.nil?
query_params = build_query_params(Hash.new.tap do |qp|
qp[:start] = from_date.iso8601
qp[:end] = to_date.iso8601
qp[:base] = base if base
qp[:targets] = targets.join(',') if targets
qp[:meta] = include_meta if include_meta
end)
self.class.get("/timeseries#{query_params}", @options)
end
private
def build_query_params(query_params)
"?#{query_params.map{|k, v| "#{k}=#{v}"}.join("&")}" if query_params.size > 0
end
end