apilayer 1.3.0 → 1.4.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. checksums.yaml +4 -4
  2. data/README.rdoc +15 -0
  3. data/lib/apilayer/currency.rb +49 -9
  4. metadata +1 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 383eeb146f71eb3228479e26a5ebf2822215bb31
4
- data.tar.gz: 02b6171cecc55a1aad163ac06ef9a01ad14334fe
3
+ metadata.gz: 82aeeda21050efb48e62a0d2fb4b9ebe46a7acc8
4
+ data.tar.gz: 101b15014092486700fea46ad8b5fb31ea097918
5
5
  SHA512:
6
- metadata.gz: d3cc153cc6767b38b63e41fa64b203b9f2919b327b88916441b59335f62299479e6064e8f26859d256861ced50ce7822b333d1640cd3d2080886bf6d7ba3dde8
7
- data.tar.gz: fd7d9d9cbdd9fa20551c9e813b80a4a1cef581f2564966a8977358be5bff05ee4f2d87513fbe9be4405386e980efea4f4773b6f834e57003ab8eacd3402dd502
6
+ metadata.gz: e0a3603cf06becdddee31d8d8b48e51a7aa9149583ebba6bf501d81896627616ded7734d6a950b7afc7add8d6cb372bda235532e48298084714a97841b1cf9df
7
+ data.tar.gz: b0006ad23396afc3cd707dc500b71f3e193a26a8a18d37c23dfeec047879602c2fe878d5f8b46d10ad7f5996d66539bd5cb96cc93e520d46271261c1ba9727e3
@@ -3,6 +3,9 @@
3
3
  Ruby wrapper for various services of apilayer.
4
4
  See http://apilayer.com for more details.
5
5
 
6
+ === Version 1.4
7
+ Added .change and .list to Apilayer::Currency
8
+
6
9
  === Version 1.3
7
10
  Added .timeframe to Apilayer::Currency
8
11
 
@@ -44,6 +47,8 @@ Once that is done, you are ready to go
44
47
 
45
48
  ==== currencylayer
46
49
  After setting the access_key for *currencylayer*, you can use Apilayer::Currency to call *currencylayer*'s API
50
+ Apilayer::Currency.list
51
+
47
52
  Apilayer::Currency.live
48
53
  Apilayer::Currency.live(:currencies => %w[GBP, CHF])
49
54
  Apilayer::Currency.live(:source => "EUR") # source-currency is USD by default
@@ -61,6 +66,16 @@ After setting the access_key for *currencylayer*, you can use Apilayer::Currency
61
66
  Apilayer::Currency.timeframe("2016-01-01", "2016-06-01", :currencies => %w[GBP CHF])
62
67
  Apilayer::Currency.timeframe("2016-01-01", "2016-06-01", :source => "EUR")
63
68
  Apilayer::Currency.timeframe("2016-01-01", "2016-06-01", :currencies => %w[GBP CHF], :source => "EUR")
69
+
70
+ Apilayer::Currency.change
71
+ Apilayer::Currency.change("2016-01-01", "2016-03-01")
72
+ Apilayer::Currency.change("2016-01-01", "2016-03-01", :source => "EUR")
73
+ Apilayer::Currency.change("2016-01-01", "2016-03-01", :currencies => %w[GBP CHF])
74
+ Apilayer::Currency.change("2016-01-01", "2016-03-01", :source => "EUR", :currencies => %w[GBP CHF])
75
+ Apilayer::Currency.change(nil, nil, {:source => "EUR"})
76
+ Apilayer::Currency.change(nil, nil, {:currencies => %w[GBP CHF]})
77
+ Apilayer::Currency.change(nil, nil, {:source => "EUR", :currencies => %w[GBP CHF]})
78
+
64
79
  ==== vatlayer
65
80
  After setting the access_key for *vatlayer*, you can use Apilayer::Vat to call *vatlayer*'s API
66
81
 
@@ -9,10 +9,13 @@ module Apilayer
9
9
  # in order to to make a connection to currencylayer
10
10
  APILAYER_CONFIG_KEY = :currency_key
11
11
  INVALID_OPTIONS_MSG = "You have provided an invalid option. Allowed options are :currencies and :source"
12
+ INVALID_TIMEFRAME_MSG = "start_date and end_date must be either provided together or left out together."
12
13
  LIVE_SLUG = "live"
13
14
  HISTORICAL_SLUG = "historical"
14
15
  CONVERT_SLUG = "convert"
15
16
  TIMEFRAME_SLUG = "timeframe"
17
+ CHANGE_SLUG = "change"
18
+ LIST_SLUG = "list"
16
19
 
17
20
  ## Validations
18
21
  #
@@ -24,13 +27,24 @@ module Apilayer
24
27
  end
25
28
  end
26
29
 
30
+ def self.validate_timeframe_completeness(start_date,end_date)
31
+ if [start_date,end_date].compact.size == 1
32
+ raise Apilayer::Error.new(INVALID_TIMEFRAME_MSG)
33
+ end
34
+ end
27
35
  ### API methods
28
36
  #
29
37
 
38
+ ##
39
+ # Api-Method: Calls the /list endpoint to display all supported currencies
40
+ def self.list
41
+ get_and_parse LIST_SLUG
42
+ end
43
+
30
44
  ##
31
45
  # Api-Method: Calls the /live endpoint to get real-time exchange rates.
32
46
  # When no currency-codes are specified, it will return all exchange rates for your source-currency.
33
- # Example:
47
+ # Examples:
34
48
  # Apilayer::Currency.live
35
49
  # Apilayer::Currency.live(:currencies => %w[GBP, CHF])
36
50
  # Apilayer::Currency.live(:source => "EUR")
@@ -48,7 +62,7 @@ module Apilayer
48
62
  ##
49
63
  # Api-Method: Calls the /historical endpoint to get exchange rates for a specific date.
50
64
  # When no currency-codes are specified, it will return all exchange rates for your source-currency.
51
- # Example:
65
+ # Examples:
52
66
  # Apilayer::Currency.historical("2016-01-01")
53
67
  # Apilayer::Currency.historical("2016-01-01", :currencies => %w[GBP CHF])
54
68
  # Apilayer::Currency.historical(:source => "EUR")
@@ -67,9 +81,9 @@ module Apilayer
67
81
  ##
68
82
  # Api-Method: Calls the /convert endpoint, requires :from, :to and :amount
69
83
  # When :date hasn't been passed, the latest available exchange rates will be used for your conversion.
70
- # Example:
84
+ # Examples:
71
85
  # Apilayer::Currency.convert("EUR", "CHF", 100)
72
- # Apilayer::Currency.convert("EUR", "CHF", 100, "2015-06-01")
86
+ # Apilayer::Currency.convert("EUR", "CHF", 100, "2015-03-01")
73
87
  def self.convert(from, to, amount, date=nil)
74
88
  params = {:from => from, :to => to, :amount => amount}
75
89
  params.merge!(:date => date) if date
@@ -81,22 +95,47 @@ module Apilayer
81
95
  # If :currencies hasn't been provided as an option, it will return all exchange-rates for that period of your source-currency
82
96
  # :source can be provided as option to change the source-currency, which is USD by default
83
97
  # The difference between start_date and end_date can be maximum 365 days
84
- # Example:
85
- # Apilayer::Currency.timeframe("2016-01-01", "2016-06-01")
86
- # Apilayer::Currency.timeframe("2016-01-01", "2016-06-01", :currencies => %w[GBP CHF])
87
- # Apilayer::Currency.timeframe("2016-01-01", "2016-06-01", :currencies => %w[GBP CHF], :source => "EUR")
98
+ # Examples:
99
+ # Apilayer::Currency.timeframe("2016-01-01", "2016-03-01")
100
+ # Apilayer::Currency.timeframe("2016-01-01", "2016-03-01", :currencies => %w[GBP CHF])
101
+ # Apilayer::Currency.timeframe("2016-01-01", "2016-03-01", :currencies => %w[GBP CHF], :source => "EUR")
88
102
  def self.timeframe(start_date, end_date, opts={})
89
103
  validate_options(opts)
90
104
  params = {:start_date => start_date, :end_date => end_date}
91
105
  get_and_parse_with_options(TIMEFRAME_SLUG, opts, params)
92
106
  end
93
107
 
94
- ## Internal Methods
108
+ ##
109
+ # Api-Method: Calls the /change endpoint.
110
+ # start_date and end_date are optional, but can't provide one without the other
111
+ # :currencies and :source are optional
112
+ # Examples:
113
+ # Apilayer::Currency.change
114
+ # Apilayer::Currency.change("2016-01-01", "2016-03-01")
115
+ # Apilayer::Currency.change("2016-01-01", "2016-03-01", :source => "EUR")
116
+ # Apilayer::Currency.change("2016-01-01", "2016-03-01", :currencies => %w[GBP CHF])
117
+ # Apilayer::Currency.change("2016-01-01", "2016-03-01", :source => "EUR", :currencies => %w[GBP CHF])
118
+ # Apilayer::Currency.change(nil, nil, {:source => "EUR"})
119
+ # Apilayer::Currency.change(nil, nil, {:currencies => %w[GBP CHF]})
120
+ # Apilayer::Currency.change(nil, nil, {:source => "EUR", :currencies => %w[GBP CHF]})
121
+ def self.change(start_date=nil, end_date=nil, opts={})
122
+ validate_options(opts)
123
+ validate_timeframe_completeness(start_date,end_date)
124
+ params = {:start_date => start_date,
125
+ :end_date => end_date
126
+ }.reject{ |k,v| v.nil? }
127
+ get_and_parse_with_options(CHANGE_SLUG, opts, params)
128
+ end
129
+
130
+ ##
131
+ #
95
132
  def self.get_and_parse_with_options(slug, opts, params={})
96
133
  params = add_options_to_params(opts, params)
97
134
  get_and_parse(slug, params)
98
135
  end
99
136
 
137
+ ##
138
+ # Adds currencies and source to query string if they have been provided with options-hash
100
139
  def self.add_options_to_params(opts, params)
101
140
  if opts[:currencies] && opts[:currencies].any?
102
141
  params[:currencies] = join_by_commas(opts[:currencies])
@@ -106,6 +145,7 @@ module Apilayer
106
145
  end
107
146
  params
108
147
  end
148
+
109
149
  ##
110
150
  # Joins currencies in an array as a comma-separated-string
111
151
  def self.join_by_commas(currencies)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: apilayer
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.3.0
4
+ version: 1.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alex Fong