currency_layer 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: e86057cf2480a4d84c5ce402739a83faa1610c97
4
+ data.tar.gz: 37ce1a95d27e9983fd8959168ac453ab5f354c73
5
+ SHA512:
6
+ metadata.gz: dfb4d2b61462cb5364197de0a965dbf3a6eedfa127dfc1d858ffb6a457ed1ad8fd5cb4d37778ae2fef5b7128df8aec4513a283df5b3896698e5e2fe0397da9d2
7
+ data.tar.gz: 9c968459751b9021d406e395aabed0728377b928efcb8aa9bca5c4dd5baf0e917e556929c8350344f69cb942e3bf7ca5f4572345b94ad6b115d67fb98e2da27d
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+ gemspec
3
+ gem "apilayer", :git => 'https://github.com/actfong/apilayer.git', :branch => 'refactor/separate_gems'
data/LICENSE ADDED
@@ -0,0 +1,19 @@
1
+ Copyright (C) 2016 Alex Fong http://www.github.com/actfong
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy of
4
+ this software and associated documentation files (the "Software"), to deal in
5
+ the Software without restriction, including without limitation the rights to
6
+ use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
7
+ of the Software, and to permit persons to whom the Software is furnished to do
8
+ so, subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included in all
11
+ copies or substantial portions of the Software.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19
+ SOFTWARE.
@@ -0,0 +1,78 @@
1
+ == currency_layer {<img src="https://secure.travis-ci.org/actfong/currency_layer.png"/>}[http://travis-ci.org/actfong/currency_layer] {<img src="https://codeclimate.com/github/actfong/currency_layer.png"/>}[https://codeclimate.com/github/actfong/currency_layer] {<img src="https://badge.fury.io/rb/currency_layer.svg" alt="Gem Version" />}[https://badge.fury.io/rb/currency_layer]
2
+
3
+ Ruby wrapper for currencylayer of apilayer.
4
+ See https://currencylayer.com/ and http://apilayer.com for more details.
5
+
6
+ === Introduction
7
+ This gem is extracted from the previous version of apilayer gem. The currency_layer gem depends on the apilayer gem to configure and create connections to apilayer.net
8
+
9
+ === Installation
10
+
11
+ ==== Using Bundler
12
+
13
+ Add apilayer in your <tt>Gemfile</tt>:
14
+
15
+ gem "currency_layer"
16
+
17
+ Run the following in your console:
18
+
19
+ $ bundle install
20
+
21
+ === Usage
22
+
23
+ ==== Set up currency_layer
24
+ Once you have signed up for *currencylayer.com*, you will receive an access_key.
25
+ Then configure your Apilayer::Currency module like this:
26
+
27
+ Apilayer::Currency.configure do |configs|
28
+ configs.access_key = "my_access_key_123"
29
+ configs.https = true
30
+ end
31
+
32
+ Please note that the https configuration is optional and only available to paid-accounts.
33
+ If unset, these configuration-values are just nil.
34
+
35
+ You can always review you configurations with:
36
+
37
+ Apilayer::Currency.configs
38
+
39
+ Once your configurations are set, you are ready to go
40
+
41
+ ==== currencylayer
42
+ After setting the access_key for *currencylayer*, you can use Apilayer::Currency to call *currencylayer*'s API
43
+ Apilayer::Currency.list
44
+
45
+ Apilayer::Currency.live
46
+ Apilayer::Currency.live(:currencies => %w[GBP, CHF])
47
+ Apilayer::Currency.live(:source => "EUR") # source-currency is USD by default
48
+ Apilayer::Currency.live(:source => "EUR", :currencies => %w[GBP, CHF])
49
+
50
+ Apilayer::Currency.historical("2016-01-01")
51
+ Apilayer::Currency.historical("2016-01-01", :currencies => %w[GBP CHF])
52
+ Apilayer::Currency.historical(:source => "EUR") # source-currency is USD by default
53
+ Apilayer::Currency.historical("2016-01-01", :currencies => %w[GBP CHF], :source => "EUR")
54
+
55
+ Apilayer::Currency.convert("EUR", "CHF", 100) # convert 100 EUR to CHF
56
+ Apilayer::Currency.convert("EUR", "CHF", 100, "2015-06-01") # based on specific date
57
+
58
+ Apilayer::Currency.timeframe("2016-01-01", "2016-06-01")
59
+ Apilayer::Currency.timeframe("2016-01-01", "2016-06-01", :currencies => %w[GBP CHF])
60
+ Apilayer::Currency.timeframe("2016-01-01", "2016-06-01", :source => "EUR")
61
+ Apilayer::Currency.timeframe("2016-01-01", "2016-06-01", :currencies => %w[GBP CHF], :source => "EUR")
62
+
63
+ Apilayer::Currency.change
64
+ Apilayer::Currency.change("2016-01-01", "2016-03-01")
65
+ Apilayer::Currency.change("2016-01-01", "2016-03-01", :source => "EUR")
66
+ Apilayer::Currency.change("2016-01-01", "2016-03-01", :currencies => %w[GBP CHF])
67
+ Apilayer::Currency.change("2016-01-01", "2016-03-01", :source => "EUR", :currencies => %w[GBP CHF])
68
+ Apilayer::Currency.change(nil, nil, {:source => "EUR"})
69
+ Apilayer::Currency.change(nil, nil, {:currencies => %w[GBP CHF]})
70
+ Apilayer::Currency.change(nil, nil, {:source => "EUR", :currencies => %w[GBP CHF]})
71
+
72
+ ==== Re-Configure access_key and https
73
+ If you happened to have forgotten to set or entered an incorrect value, you can re-configure your Apilayer module by:
74
+
75
+ # Example: reconfigure https
76
+ Apilayer::Currency.configure do |configs|
77
+ configs.https = true
78
+ end
@@ -0,0 +1,5 @@
1
+ require 'rspec/core/rake_task'
2
+ task :default => :spec
3
+ desc 'Run test suite'
4
+
5
+ RSpec::Core::RakeTask.new(:spec)
@@ -0,0 +1,152 @@
1
+ ##
2
+ # Ruby wrapper for currencylayer. See https://currencylayer.com/documentation for more info
3
+ module Apilayer
4
+ module Currency
5
+ extend Apilayer::ConnectionHelper
6
+
7
+ INVALID_OPTIONS_MSG = "You have provided an invalid option. Allowed options are :currencies and :source"
8
+ INVALID_TIMEFRAME_MSG = "start_date and end_date must be either provided together or left out together."
9
+ LIVE_SLUG = "live"
10
+ HISTORICAL_SLUG = "historical"
11
+ CONVERT_SLUG = "convert"
12
+ TIMEFRAME_SLUG = "timeframe"
13
+ CHANGE_SLUG = "change"
14
+ LIST_SLUG = "list"
15
+
16
+ ## Validations
17
+ #
18
+ def self.validate_options(options)
19
+ options.keys.each do |key|
20
+ unless [:currencies, :source].include? key
21
+ raise Apilayer::Error.new(INVALID_OPTIONS_MSG)
22
+ end
23
+ end
24
+ end
25
+
26
+ def self.validate_timeframe_completeness(start_date,end_date)
27
+ if [start_date,end_date].compact.size == 1
28
+ raise Apilayer::Error.new(INVALID_TIMEFRAME_MSG)
29
+ end
30
+ end
31
+ ### API methods
32
+ #
33
+
34
+ ##
35
+ # Api-Method: Calls the /list endpoint to display all supported currencies
36
+ def self.list
37
+ get_and_parse LIST_SLUG
38
+ end
39
+
40
+ ##
41
+ # Api-Method: Calls the /live endpoint to get real-time exchange rates.
42
+ # When no currency-codes are specified, it will return all exchange rates for your source-currency.
43
+ # Examples:
44
+ # Apilayer::Currency.live
45
+ # Apilayer::Currency.live(:currencies => %w[GBP, CHF])
46
+ # Apilayer::Currency.live(:source => "EUR")
47
+ # Apilayer::Currency.live(:source => "EUR", :currencies => %w[GBP, CHF])
48
+ def self.live(opts={})
49
+ validate_options(opts)
50
+
51
+ if opts.empty?
52
+ get_and_parse LIVE_SLUG
53
+ else
54
+ get_and_parse_with_options(LIVE_SLUG, opts)
55
+ end
56
+ end
57
+
58
+ ##
59
+ # Api-Method: Calls the /historical endpoint to get exchange rates for a specific date.
60
+ # When no currency-codes are specified, it will return all exchange rates for your source-currency.
61
+ # Examples:
62
+ # Apilayer::Currency.historical("2016-01-01")
63
+ # Apilayer::Currency.historical("2016-01-01", :currencies => %w[GBP CHF])
64
+ # Apilayer::Currency.historical(:source => "EUR")
65
+ # Apilayer::Currency.historical("2016-01-01", :currencies => %w[GBP CHF], :source => "EUR")
66
+ def self.historical(date, opts={})
67
+ validate_options(opts)
68
+ params = {:date => date}
69
+
70
+ if opts.empty?
71
+ get_and_parse(HISTORICAL_SLUG, params)
72
+ else
73
+ get_and_parse_with_options(HISTORICAL_SLUG, opts, params)
74
+ end
75
+ end
76
+
77
+ ##
78
+ # Api-Method: Calls the /convert endpoint, requires :from, :to and :amount
79
+ # When :date hasn't been passed, the latest available exchange rates will be used for your conversion.
80
+ # Examples:
81
+ # Apilayer::Currency.convert("EUR", "CHF", 100)
82
+ # Apilayer::Currency.convert("EUR", "CHF", 100, "2015-03-01")
83
+ def self.convert(from, to, amount, date=nil)
84
+ params = {:from => from, :to => to, :amount => amount}
85
+ params.merge!(:date => date) if date
86
+ get_and_parse(CONVERT_SLUG, params)
87
+ end
88
+
89
+ ##
90
+ # Api-Method: Calls the /timeframe endpoint. Requires :start_date and :end_date
91
+ # If :currencies hasn't been provided as an option, it will return all exchange-rates for that period of your source-currency
92
+ # :source can be provided as option to change the source-currency, which is USD by default
93
+ # The difference between start_date and end_date can be maximum 365 days
94
+ # Examples:
95
+ # Apilayer::Currency.timeframe("2016-01-01", "2016-03-01")
96
+ # Apilayer::Currency.timeframe("2016-01-01", "2016-03-01", :currencies => %w[GBP CHF])
97
+ # Apilayer::Currency.timeframe("2016-01-01", "2016-03-01", :currencies => %w[GBP CHF], :source => "EUR")
98
+ def self.timeframe(start_date, end_date, opts={})
99
+ validate_options(opts)
100
+ params = {:start_date => start_date, :end_date => end_date}
101
+ get_and_parse_with_options(TIMEFRAME_SLUG, opts, params)
102
+ end
103
+
104
+ ##
105
+ # Api-Method: Calls the /change endpoint.
106
+ # start_date and end_date are optional, but can't provide one without the other
107
+ # :currencies and :source are optional
108
+ # Examples:
109
+ # Apilayer::Currency.change
110
+ # Apilayer::Currency.change("2016-01-01", "2016-03-01")
111
+ # Apilayer::Currency.change("2016-01-01", "2016-03-01", :source => "EUR")
112
+ # Apilayer::Currency.change("2016-01-01", "2016-03-01", :currencies => %w[GBP CHF])
113
+ # Apilayer::Currency.change("2016-01-01", "2016-03-01", :source => "EUR", :currencies => %w[GBP CHF])
114
+ # Apilayer::Currency.change(nil, nil, {:source => "EUR"})
115
+ # Apilayer::Currency.change(nil, nil, {:currencies => %w[GBP CHF]})
116
+ # Apilayer::Currency.change(nil, nil, {:source => "EUR", :currencies => %w[GBP CHF]})
117
+ def self.change(start_date=nil, end_date=nil, opts={})
118
+ validate_options(opts)
119
+ validate_timeframe_completeness(start_date,end_date)
120
+ params = {:start_date => start_date,
121
+ :end_date => end_date
122
+ }.reject{ |k,v| v.nil? }
123
+ get_and_parse_with_options(CHANGE_SLUG, opts, params)
124
+ end
125
+
126
+ ##
127
+ #
128
+ def self.get_and_parse_with_options(slug, opts, params={})
129
+ params = add_options_to_params(opts, params)
130
+ get_and_parse(slug, params)
131
+ end
132
+
133
+ ##
134
+ # Adds currencies and source to query string if they have been provided with options-hash
135
+ def self.add_options_to_params(opts, params)
136
+ if opts[:currencies] && opts[:currencies].any?
137
+ params[:currencies] = join_by_commas(opts[:currencies])
138
+ end
139
+ if opts[:source]
140
+ params[:source] = opts[:source]
141
+ end
142
+ params
143
+ end
144
+
145
+ ##
146
+ # Joins currencies in an array as a comma-separated-string
147
+ def self.join_by_commas(currencies)
148
+ currencies.map(&:strip).join(",")
149
+ end
150
+
151
+ end
152
+ end
@@ -0,0 +1,4 @@
1
+ require 'rubygems'
2
+ require 'apilayer'
3
+
4
+ require "apilayer/currency"
metadata ADDED
@@ -0,0 +1,194 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: currency_layer
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Alex Fong
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-06-02 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rake
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '10.1'
20
+ - - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: 0.10.1
23
+ type: :development
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - "~>"
28
+ - !ruby/object:Gem::Version
29
+ version: '10.1'
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: 0.10.1
33
+ - !ruby/object:Gem::Dependency
34
+ name: pry
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - "~>"
38
+ - !ruby/object:Gem::Version
39
+ version: '0.10'
40
+ - - ">="
41
+ - !ruby/object:Gem::Version
42
+ version: 0.10.1
43
+ type: :development
44
+ prerelease: false
45
+ version_requirements: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - "~>"
48
+ - !ruby/object:Gem::Version
49
+ version: '0.10'
50
+ - - ">="
51
+ - !ruby/object:Gem::Version
52
+ version: 0.10.1
53
+ - !ruby/object:Gem::Dependency
54
+ name: bundler
55
+ requirement: !ruby/object:Gem::Requirement
56
+ requirements:
57
+ - - "~>"
58
+ - !ruby/object:Gem::Version
59
+ version: '1.7'
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ version: 1.7.9
63
+ type: :development
64
+ prerelease: false
65
+ version_requirements: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - "~>"
68
+ - !ruby/object:Gem::Version
69
+ version: '1.7'
70
+ - - ">="
71
+ - !ruby/object:Gem::Version
72
+ version: 1.7.9
73
+ - !ruby/object:Gem::Dependency
74
+ name: rspec
75
+ requirement: !ruby/object:Gem::Requirement
76
+ requirements:
77
+ - - "~>"
78
+ - !ruby/object:Gem::Version
79
+ version: '3.0'
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: 3.0.0
83
+ type: :development
84
+ prerelease: false
85
+ version_requirements: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '3.0'
90
+ - - ">="
91
+ - !ruby/object:Gem::Version
92
+ version: 3.0.0
93
+ - !ruby/object:Gem::Dependency
94
+ name: simplecov
95
+ requirement: !ruby/object:Gem::Requirement
96
+ requirements:
97
+ - - "~>"
98
+ - !ruby/object:Gem::Version
99
+ version: '0.11'
100
+ - - ">="
101
+ - !ruby/object:Gem::Version
102
+ version: 0.11.0
103
+ type: :development
104
+ prerelease: false
105
+ version_requirements: !ruby/object:Gem::Requirement
106
+ requirements:
107
+ - - "~>"
108
+ - !ruby/object:Gem::Version
109
+ version: '0.11'
110
+ - - ">="
111
+ - !ruby/object:Gem::Version
112
+ version: 0.11.0
113
+ - !ruby/object:Gem::Dependency
114
+ name: vcr
115
+ requirement: !ruby/object:Gem::Requirement
116
+ requirements:
117
+ - - "~>"
118
+ - !ruby/object:Gem::Version
119
+ version: '3.0'
120
+ - - ">="
121
+ - !ruby/object:Gem::Version
122
+ version: 3.0.1
123
+ type: :development
124
+ prerelease: false
125
+ version_requirements: !ruby/object:Gem::Requirement
126
+ requirements:
127
+ - - "~>"
128
+ - !ruby/object:Gem::Version
129
+ version: '3.0'
130
+ - - ">="
131
+ - !ruby/object:Gem::Version
132
+ version: 3.0.1
133
+ - !ruby/object:Gem::Dependency
134
+ name: webmock
135
+ requirement: !ruby/object:Gem::Requirement
136
+ requirements:
137
+ - - "~>"
138
+ - !ruby/object:Gem::Version
139
+ version: '2.0'
140
+ - - ">="
141
+ - !ruby/object:Gem::Version
142
+ version: 2.0.1
143
+ type: :development
144
+ prerelease: false
145
+ version_requirements: !ruby/object:Gem::Requirement
146
+ requirements:
147
+ - - "~>"
148
+ - !ruby/object:Gem::Version
149
+ version: '2.0'
150
+ - - ">="
151
+ - !ruby/object:Gem::Version
152
+ version: 2.0.1
153
+ description: Ruby wrapper for currencylayer by apilayer. This gem depends on the apilayer
154
+ gem, which provides a common connection-interface to various services of apilayer.net
155
+ (such as currencylayer and vatlayer). See https://currencylayer.com/ and https://apilayer.com/
156
+ for more details.
157
+ email:
158
+ - actfong@gmail.com
159
+ executables: []
160
+ extensions: []
161
+ extra_rdoc_files: []
162
+ files:
163
+ - Gemfile
164
+ - LICENSE
165
+ - README.rdoc
166
+ - Rakefile
167
+ - lib/apilayer/currency.rb
168
+ - lib/currencylayer.rb
169
+ homepage: https://github.com/actfong/currency_layer
170
+ licenses:
171
+ - MIT
172
+ metadata: {}
173
+ post_install_message:
174
+ rdoc_options: []
175
+ require_paths:
176
+ - lib
177
+ required_ruby_version: !ruby/object:Gem::Requirement
178
+ requirements:
179
+ - - ">="
180
+ - !ruby/object:Gem::Version
181
+ version: '0'
182
+ required_rubygems_version: !ruby/object:Gem::Requirement
183
+ requirements:
184
+ - - ">="
185
+ - !ruby/object:Gem::Version
186
+ version: '0'
187
+ requirements: []
188
+ rubyforge_project:
189
+ rubygems_version: 2.4.5
190
+ signing_key:
191
+ specification_version: 4
192
+ summary: Ruby wrapper for currencylayer by apilayer. See https://currencylayer.com/
193
+ for more details.
194
+ test_files: []