open_exchange_rates 0.3.0 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -1,6 +1,7 @@
1
1
  # OpenExchangeRates
2
2
 
3
3
  [![Code Climate](https://codeclimate.com/badge.png)](https://codeclimate.com/github/vlado/open_exchange_rates)
4
+ [![Gem Version](https://badge.fury.io/rb/open_exchange_rates.png)](http://badge.fury.io/rb/open_exchange_rates)
4
5
 
5
6
  Ruby gem for currency conversion based on [Open Exchange Rates API](http://openexchangerates.org) - free / open source hourly-updated currency data for everybody
6
7
 
@@ -83,9 +84,9 @@ If you omit **:from** or **:to** option conversion will be related to base curre
83
84
  ## TODO
84
85
 
85
86
  - ability to set default currency (USD is currently always set as base currency)
86
- - ability to pass Date as :on option (only 'yyyy-mm-dd' works currently)
87
+ - <del>ability to pass Date as :on option (only 'yyyy-mm-dd' works currently)</del>
87
88
  - write some docs
88
- - write more test for specific situations (invalid date, ...)
89
+ - write more test for specific situations (<del>invalid date</del>, ...)
89
90
 
90
91
  ## Contributing
91
92
 
@@ -1,4 +1,5 @@
1
1
  require "open-uri"
2
+ require "date"
2
3
 
3
4
  module OpenExchangeRates
4
5
  class Rates
@@ -9,8 +10,15 @@ module OpenExchangeRates
9
10
  end
10
11
  end
11
12
 
12
- def initialize(app_id = nil)
13
- @app_id = app_id || OpenExchangeRates.configuration.app_id
13
+ attr_reader :app_id
14
+
15
+ def initialize(options = {})
16
+ if options.kind_of? Hash
17
+ @app_id = options[:app_id] || OpenExchangeRates.configuration.app_id
18
+ else
19
+ warn "[DEPRECATION] `OpenExchangeRates::Rates.new('myappid')` is deprecated. Please use `OpenExchangeRates::Rates.new(:app_id => 'myappid')` instead."
20
+ @app_id = options
21
+ end
14
22
  raise MissingAppIdError unless @app_id
15
23
  end
16
24
 
@@ -45,7 +53,24 @@ module OpenExchangeRates
45
53
  OpenExchangeRates::Response.new(@latest_response)
46
54
  end
47
55
 
48
- def on(date_string)
56
+ def valid_yyyy_mm_dd(date_string)
57
+ matches = date_string =~ /^([0-9]{4})(?:(1[0-2]|0[1-9])|-(1[0-2]|0[1-9])-)(3[0-1]|0[1-9]|[1-2][0-9])/
58
+ raise ArgumentError, 'Not a valid date string (ie. yyyy-mm-dd)' unless matches
59
+ date_string
60
+ end
61
+
62
+ def date_string_from(date_representation)
63
+ if date_representation.kind_of? Date
64
+ date_representation.to_s
65
+ elsif date_representation.kind_of? String
66
+ valid_yyyy_mm_dd date_representation
67
+ else
68
+ raise ArgumentError, "'on' must be a Date or 'yyyy-mm-dd' string"
69
+ end
70
+ end
71
+
72
+ def on(date_representation)
73
+ date_string = date_string_from(date_representation)
49
74
  OpenExchangeRates::Response.new(parse_on(date_string))
50
75
  end
51
76
 
@@ -64,4 +89,4 @@ module OpenExchangeRates
64
89
  end
65
90
 
66
91
  end
67
- end
92
+ end
@@ -1,3 +1,3 @@
1
1
  module OpenExchangeRates
2
- VERSION = "0.3.0"
2
+ VERSION = "0.4.0"
3
3
  end
data/test/rates_test.rb CHANGED
@@ -1,4 +1,5 @@
1
1
  require "test_helper"
2
+ require 'complex'
2
3
 
3
4
  class TestOpenExchangeRates < Test::Unit::TestCase
4
5
 
@@ -13,11 +14,27 @@ class TestOpenExchangeRates < Test::Unit::TestCase
13
14
  OpenExchangeRates.configuration.app_id = ENV['OPEN_EXCHANGE_RATES_APP_ID']
14
15
  end
15
16
 
17
+ def test_app_id_configuration
18
+ fx = OpenExchangeRates::Rates.new
19
+ assert_equal ENV['OPEN_EXCHANGE_RATES_APP_ID'], fx.app_id
20
+
21
+ OpenExchangeRates.configure do |config|
22
+ config.app_id = "myappid"
23
+ end
24
+ fx = OpenExchangeRates::Rates.new
25
+ assert_equal "myappid", fx.app_id
26
+
27
+ fx = OpenExchangeRates::Rates.new(:app_id => 'myotherappid')
28
+ assert_equal "myotherappid", fx.app_id
29
+
30
+ OpenExchangeRates.configuration.app_id = ENV['OPEN_EXCHANGE_RATES_APP_ID']
31
+ end
32
+
16
33
  def test_invalid_app_id_raise_error
17
34
  stub(OpenExchangeRates.configuration).app_id { "somethingstupid" }
18
35
  fx = OpenExchangeRates::Rates.new
19
36
 
20
- assert_raise NoMethodError do
37
+ assert_raise OpenURI::HTTPError do
21
38
  fx.exchange_rate(:from => "USD", :to => "EUR")
22
39
  end
23
40
  end
@@ -62,6 +79,25 @@ class TestOpenExchangeRates < Test::Unit::TestCase
62
79
  assert_equal 0.171472, fx.exchange_rate(:from => "HRK", :to => "AUD", :on => "2012-05-10")
63
80
  end
64
81
 
82
+ def test_exchange_rate_on_specific_date_specified_by_date_class
83
+ fx = OpenExchangeRates::Rates.new
84
+ stub(fx).parse_on { OpenExchangeRates::Parser.new.parse(open_asset("2012-05-10.json")) }
85
+
86
+ assert_equal 1, fx.exchange_rate(:from => "USD", :to => "USD", :on => Date.new(2012,05,10))
87
+ end
88
+
89
+ def test_exchange_requires_valid_date
90
+ fx = OpenExchangeRates::Rates.new
91
+
92
+ assert_raise ArgumentError do
93
+ fx.exchange_rate(:from => "USD", :to => "USD", :on => "somethingstupid")
94
+ end
95
+
96
+ assert_raise ArgumentError do
97
+ fx.exchange_rate(:from => "USD", :to => "USD", :on => Complex(0.3))
98
+ end
99
+ end
100
+
65
101
  def test_convert
66
102
  fx = OpenExchangeRates::Rates.new
67
103
  stub(fx).parse_latest { OpenExchangeRates::Parser.new.parse(open_asset("latest.json")) }
@@ -183,4 +219,4 @@ private
183
219
  File.open("#{assets_root}/#{filename}")
184
220
  end
185
221
 
186
- end
222
+ 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: 19
4
+ hash: 15
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
- - 3
8
+ - 4
9
9
  - 0
10
- version: 0.3.0
10
+ version: 0.4.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Vlado Cingel
@@ -15,10 +15,12 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2012-07-14 00:00:00 Z
18
+ date: 2013-01-14 00:00:00 Z
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
21
- version_requirements: &id001 !ruby/object:Gem::Requirement
21
+ name: yajl-ruby
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
22
24
  none: false
23
25
  requirements:
24
26
  - - ">="
@@ -27,12 +29,12 @@ dependencies:
27
29
  segments:
28
30
  - 0
29
31
  version: "0"
30
- requirement: *id001
31
32
  type: :runtime
32
- name: yajl-ruby
33
- prerelease: false
33
+ version_requirements: *id001
34
34
  - !ruby/object:Gem::Dependency
35
- version_requirements: &id002 !ruby/object:Gem::Requirement
35
+ name: rr
36
+ prerelease: false
37
+ requirement: &id002 !ruby/object:Gem::Requirement
36
38
  none: false
37
39
  requirements:
38
40
  - - ">="
@@ -41,12 +43,12 @@ dependencies:
41
43
  segments:
42
44
  - 0
43
45
  version: "0"
44
- requirement: *id002
45
46
  type: :development
46
- name: rr
47
- prerelease: false
47
+ version_requirements: *id002
48
48
  - !ruby/object:Gem::Dependency
49
- version_requirements: &id003 !ruby/object:Gem::Requirement
49
+ name: rake
50
+ prerelease: false
51
+ requirement: &id003 !ruby/object:Gem::Requirement
50
52
  none: false
51
53
  requirements:
52
54
  - - ">="
@@ -55,10 +57,8 @@ dependencies:
55
57
  segments:
56
58
  - 0
57
59
  version: "0"
58
- requirement: *id003
59
60
  type: :development
60
- name: rake
61
- prerelease: false
61
+ version_requirements: *id003
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
@@ -124,3 +124,4 @@ test_files:
124
124
  - test/assets/latest.json
125
125
  - test/rates_test.rb
126
126
  - test/test_helper.rb
127
+ has_rdoc: