open_exchange_rates 0.2.1 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.gitignore +1 -0
- data/README.md +27 -0
- data/env.example +1 -0
- data/lib/open_exchange_rates/configuration.rb +5 -0
- data/lib/open_exchange_rates/rates.rb +13 -2
- data/lib/open_exchange_rates/version.rb +1 -1
- data/lib/open_exchange_rates.rb +15 -0
- data/test/rates_test.rb +23 -3
- data/test/test_helper.rb +11 -0
- metadata +19 -18
data/.gitignore
CHANGED
data/README.md
CHANGED
@@ -1,5 +1,7 @@
|
|
1
1
|
# OpenExchangeRates
|
2
2
|
|
3
|
+
[](https://codeclimate.com/github/vlado/open_exchange_rates)
|
4
|
+
|
3
5
|
Ruby gem for currency conversion based on [Open Exchange Rates API](http://openexchangerates.org) - free / open source hourly-updated currency data for everybody
|
4
6
|
|
5
7
|
## Accuracy
|
@@ -19,6 +21,31 @@ And then execute:
|
|
19
21
|
Or install it yourself as:
|
20
22
|
|
21
23
|
$ gem install open_exchange_rates
|
24
|
+
|
25
|
+
## Configuration
|
26
|
+
|
27
|
+
You will need App ID to use OpenExchangeRates, you can get one for free [here](https://openexchangerates.org/signup/). Wanna know [why App ID is needed](http://www.josscrowcroft.com/2012/projects/open-exchange-rates-update-the-partys-not-over-it-just-got-a-little-too-noisy/).
|
28
|
+
|
29
|
+
**Option 1**
|
30
|
+
|
31
|
+
Set OPEN_EXCHANGE_RATES_APP_ID environment variable and it will be used automatically. If you are using [foreman](http://ddollar.github.com/foreman/) for example just add it to your .env file like this
|
32
|
+
|
33
|
+
OPEN_EXCHANGE_RATES_APP_ID=YourAppID
|
34
|
+
|
35
|
+
**Option 2**
|
36
|
+
|
37
|
+
OpenExchangeRates.configure do |config|
|
38
|
+
config.app_id = "YourAppID"
|
39
|
+
end
|
40
|
+
|
41
|
+
If you are using Rails good place to add this is config/initializers/open_exchange_rates.rb
|
42
|
+
|
43
|
+
**Option 3**
|
44
|
+
|
45
|
+
Pass it on initialization
|
46
|
+
|
47
|
+
fx = OpenExchangeRates::Rates.new(:app_id => "YourAppID")
|
48
|
+
|
22
49
|
|
23
50
|
## Usage
|
24
51
|
|
data/env.example
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
OPEN_EXCHANGE_RATES_APP_ID=YourAppID
|
@@ -3,6 +3,17 @@ require "open-uri"
|
|
3
3
|
module OpenExchangeRates
|
4
4
|
class Rates
|
5
5
|
|
6
|
+
class MissingAppIdError < StandardError
|
7
|
+
def initialize(msg = "Go to https://openexchangerates.org/signup to get App ID and then add it to the configuration: OpenExchangeRates.configuration.app_id = 'your app id'")
|
8
|
+
super(msg)
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
def initialize(app_id = nil)
|
13
|
+
@app_id = app_id || OpenExchangeRates.configuration.app_id
|
14
|
+
raise MissingAppIdError unless @app_id
|
15
|
+
end
|
16
|
+
|
6
17
|
def exchange_rate(options = {})
|
7
18
|
from_curr = options[:from].to_s.upcase
|
8
19
|
to_curr = options[:to].to_s.upcase
|
@@ -44,12 +55,12 @@ module OpenExchangeRates
|
|
44
55
|
|
45
56
|
def parse_latest
|
46
57
|
@latest_parser ||= OpenExchangeRates::Parser.new
|
47
|
-
@latest_parser.parse(open(OpenExchangeRates::LATEST_URL))
|
58
|
+
@latest_parser.parse(open("#{OpenExchangeRates::LATEST_URL}?app_id=#{@app_id}"))
|
48
59
|
end
|
49
60
|
|
50
61
|
def parse_on(date_string)
|
51
62
|
@on_parser = OpenExchangeRates::Parser.new
|
52
|
-
@on_parser.parse(open("#{OpenExchangeRates::BASE_URL}/historical/#{date_string}.json"))
|
63
|
+
@on_parser.parse(open("#{OpenExchangeRates::BASE_URL}/historical/#{date_string}.json?app_id=#{@app_id}"))
|
53
64
|
end
|
54
65
|
|
55
66
|
end
|
data/lib/open_exchange_rates.rb
CHANGED
@@ -1,5 +1,6 @@
|
|
1
1
|
require "rubygems"
|
2
2
|
require "open_exchange_rates/version"
|
3
|
+
require "open_exchange_rates/configuration"
|
3
4
|
require "open_exchange_rates/parser"
|
4
5
|
require "open_exchange_rates/response"
|
5
6
|
require "open_exchange_rates/rates"
|
@@ -7,4 +8,18 @@ require "open_exchange_rates/rates"
|
|
7
8
|
module OpenExchangeRates
|
8
9
|
BASE_URL = "http://openexchangerates.org"
|
9
10
|
LATEST_URL = "#{BASE_URL}/latest.json"
|
11
|
+
|
12
|
+
class << self
|
13
|
+
attr_accessor :configuration
|
14
|
+
end
|
15
|
+
|
16
|
+
def self.configure
|
17
|
+
self.configuration ||= OpenExchangeRates::Configuration.new
|
18
|
+
yield(configuration)
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
# Default configuration
|
23
|
+
OpenExchangeRates.configure do |config|
|
24
|
+
config.app_id = ENV['OPEN_EXCHANGE_RATES_APP_ID']
|
10
25
|
end
|
data/test/rates_test.rb
CHANGED
@@ -2,6 +2,26 @@ require "test_helper"
|
|
2
2
|
|
3
3
|
class TestOpenExchangeRates < Test::Unit::TestCase
|
4
4
|
|
5
|
+
def test_app_id_is_required
|
6
|
+
assert_nothing_raised { OpenExchangeRates::Rates.new }
|
7
|
+
|
8
|
+
stub(OpenExchangeRates.configuration).app_id { nil }
|
9
|
+
assert_raise(OpenExchangeRates::Rates::MissingAppIdError) { OpenExchangeRates::Rates.new }
|
10
|
+
|
11
|
+
assert_nothing_raised { OpenExchangeRates::Rates.new(:app_id => "myappid") }
|
12
|
+
|
13
|
+
OpenExchangeRates.configuration.app_id = ENV['OPEN_EXCHANGE_RATES_APP_ID']
|
14
|
+
end
|
15
|
+
|
16
|
+
def test_invalid_app_id_raise_error
|
17
|
+
stub(OpenExchangeRates.configuration).app_id { "somethingstupid" }
|
18
|
+
fx = OpenExchangeRates::Rates.new
|
19
|
+
|
20
|
+
assert_raise NoMethodError do
|
21
|
+
fx.exchange_rate(:from => "USD", :to => "EUR")
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
5
25
|
def test_exchange_rate
|
6
26
|
fx = OpenExchangeRates::Rates.new
|
7
27
|
stub(fx).parse_latest { OpenExchangeRates::Parser.new.parse(open_asset("latest.json")) }
|
@@ -123,11 +143,10 @@ class TestOpenExchangeRates < Test::Unit::TestCase
|
|
123
143
|
|
124
144
|
assert_equal "USD", on_rates.base_currency
|
125
145
|
assert_equal "USD", on_rates.base
|
126
|
-
assert on_rates.rates.is_a?(Hash)
|
127
146
|
|
128
147
|
assert_equal 1, on_rates.rates["USD"]
|
129
|
-
assert_equal 0.
|
130
|
-
assert_equal 5.
|
148
|
+
assert_equal 0.991118, on_rates.rates["AUD"]
|
149
|
+
assert_equal 5.795542, on_rates.rates["HRK"]
|
131
150
|
end
|
132
151
|
|
133
152
|
def test_round
|
@@ -141,6 +160,7 @@ class TestOpenExchangeRates < Test::Unit::TestCase
|
|
141
160
|
|
142
161
|
def test_multiple_calls
|
143
162
|
fx = OpenExchangeRates::Rates.new
|
163
|
+
|
144
164
|
assert_nothing_raised do
|
145
165
|
fx.convert(123, :from => "EUR", :to => "AUD", :on => "2012-03-10")
|
146
166
|
fx.convert(100, :from => "USD", :to => "EUR")
|
data/test/test_helper.rb
CHANGED
@@ -3,6 +3,17 @@ require "test/unit"
|
|
3
3
|
require "rr"
|
4
4
|
require "open_exchange_rates"
|
5
5
|
|
6
|
+
# Pick up ENV variables from .env file if exists
|
7
|
+
dot_env_file_path = File.expand_path("../../.env", __FILE__)
|
8
|
+
if File.exist?(dot_env_file_path)
|
9
|
+
File.open(dot_env_file_path).each_line do |line|
|
10
|
+
key, value = line.strip.split("=")
|
11
|
+
ENV[key] = value unless key.nil? || value.nil?
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
OpenExchangeRates.configuration.app_id = ENV['OPEN_EXCHANGE_RATES_APP_ID']
|
16
|
+
|
6
17
|
class Test::Unit::TestCase
|
7
18
|
include RR::Adapters::TestUnit
|
8
19
|
end
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: open_exchange_rates
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 19
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
|
-
-
|
9
|
-
-
|
10
|
-
version: 0.
|
8
|
+
- 3
|
9
|
+
- 0
|
10
|
+
version: 0.3.0
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Vlado Cingel
|
@@ -15,12 +15,10 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2012-
|
18
|
+
date: 2012-07-14 00:00:00 Z
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|
21
|
-
|
22
|
-
prerelease: false
|
23
|
-
requirement: &id001 !ruby/object:Gem::Requirement
|
21
|
+
version_requirements: &id001 !ruby/object:Gem::Requirement
|
24
22
|
none: false
|
25
23
|
requirements:
|
26
24
|
- - ">="
|
@@ -29,12 +27,12 @@ dependencies:
|
|
29
27
|
segments:
|
30
28
|
- 0
|
31
29
|
version: "0"
|
30
|
+
requirement: *id001
|
32
31
|
type: :runtime
|
33
|
-
|
34
|
-
- !ruby/object:Gem::Dependency
|
35
|
-
name: rr
|
32
|
+
name: yajl-ruby
|
36
33
|
prerelease: false
|
37
|
-
|
34
|
+
- !ruby/object:Gem::Dependency
|
35
|
+
version_requirements: &id002 !ruby/object:Gem::Requirement
|
38
36
|
none: false
|
39
37
|
requirements:
|
40
38
|
- - ">="
|
@@ -43,12 +41,12 @@ dependencies:
|
|
43
41
|
segments:
|
44
42
|
- 0
|
45
43
|
version: "0"
|
44
|
+
requirement: *id002
|
46
45
|
type: :development
|
47
|
-
|
48
|
-
- !ruby/object:Gem::Dependency
|
49
|
-
name: rake
|
46
|
+
name: rr
|
50
47
|
prerelease: false
|
51
|
-
|
48
|
+
- !ruby/object:Gem::Dependency
|
49
|
+
version_requirements: &id003 !ruby/object:Gem::Requirement
|
52
50
|
none: false
|
53
51
|
requirements:
|
54
52
|
- - ">="
|
@@ -57,8 +55,10 @@ dependencies:
|
|
57
55
|
segments:
|
58
56
|
- 0
|
59
57
|
version: "0"
|
58
|
+
requirement: *id003
|
60
59
|
type: :development
|
61
|
-
|
60
|
+
name: rake
|
61
|
+
prerelease: false
|
62
62
|
description: Ruby library for Open Exchange Rates API - free / open source hourly-updated currency data for everybody
|
63
63
|
email:
|
64
64
|
- vladocingel@gmail.com
|
@@ -74,7 +74,9 @@ files:
|
|
74
74
|
- LICENSE
|
75
75
|
- README.md
|
76
76
|
- Rakefile
|
77
|
+
- env.example
|
77
78
|
- lib/open_exchange_rates.rb
|
79
|
+
- lib/open_exchange_rates/configuration.rb
|
78
80
|
- lib/open_exchange_rates/parser.rb
|
79
81
|
- lib/open_exchange_rates/rates.rb
|
80
82
|
- lib/open_exchange_rates/response.rb
|
@@ -122,4 +124,3 @@ test_files:
|
|
122
124
|
- test/assets/latest.json
|
123
125
|
- test/rates_test.rb
|
124
126
|
- test/test_helper.rb
|
125
|
-
has_rdoc:
|