currency_exchange 0.0.2 → 0.0.3
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/currency_exchange.gemspec +3 -4
- data/lib/currency_exchange.rb +2 -2
- data/lib/currency_exchange/storage/cache.rb +1 -1
- data/lib/currency_exchange/storage/{mem_cache.rb → rails_cache.rb} +3 -3
- data/lib/currency_exchange/transporters/exchange_transporter.rb +0 -7
- data/lib/currency_exchange/version.rb +1 -1
- data/spec/storage/cache_spec.rb +49 -0
- data/spec/storage/rails_cache_spec.rb +33 -0
- data/spec/transporters/exchange_transporter_spec.rb +59 -0
- data/spec/transporters/json_transporter_spec.rb +29 -0
- metadata +17 -25
data/currency_exchange.gemspec
CHANGED
@@ -20,8 +20,7 @@ Gem::Specification.new do |s|
|
|
20
20
|
|
21
21
|
# specify any dependencies here; for example:
|
22
22
|
s.add_development_dependency "rspec"
|
23
|
-
s.
|
24
|
-
s.
|
25
|
-
s.
|
26
|
-
s.add_runtime_dependency "rails", ">= 3.2.0"
|
23
|
+
s.add_development_dependency "rest-client"
|
24
|
+
s.add_development_dependency "json"
|
25
|
+
s.add_development_dependency "rails", ">= 3.2.0"
|
27
26
|
end
|
data/lib/currency_exchange.rb
CHANGED
@@ -7,8 +7,8 @@ module CurrencyExchange
|
|
7
7
|
end
|
8
8
|
|
9
9
|
module Transporters
|
10
|
-
autoload :ExchangeTransporter, 'currency_exchange/transporters/exchange_transporter'
|
11
10
|
autoload :JsonTransporter, 'currency_exchange/transporters/json_transporter'
|
11
|
+
autoload :ExchangeTransporter, 'currency_exchange/transporters/exchange_transporter'
|
12
12
|
end
|
13
13
|
|
14
14
|
module Data
|
@@ -16,8 +16,8 @@ module CurrencyExchange
|
|
16
16
|
end
|
17
17
|
|
18
18
|
module Storage
|
19
|
+
autoload :RailsCache, 'currency_exchange/storage/rails_cache'
|
19
20
|
autoload :Cache, 'currency_exchange/storage/cache'
|
20
|
-
autoload :MemCache, 'currency_exchange/storage/mem_cache'
|
21
21
|
end
|
22
22
|
|
23
23
|
class << self
|
@@ -1,19 +1,19 @@
|
|
1
1
|
begin
|
2
2
|
require 'rails'
|
3
3
|
rescue LoadError
|
4
|
-
raise "You don't have
|
4
|
+
raise "You don't have required libraries installed"
|
5
5
|
end
|
6
6
|
|
7
7
|
module CurrencyExchange
|
8
8
|
module Storage
|
9
|
-
class
|
9
|
+
class RailsCache < Cache
|
10
10
|
|
11
11
|
def fetch(key)
|
12
12
|
Rails.cache.read(key)
|
13
13
|
end
|
14
14
|
|
15
15
|
def store(key, value)
|
16
|
-
Rails.cache.write(key, value, :expires_in =>
|
16
|
+
Rails.cache.write(key, value, :expires_in => 86400)
|
17
17
|
value
|
18
18
|
end
|
19
19
|
end
|
@@ -0,0 +1,49 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe CurrencyExchange::Storage::Cache do
|
4
|
+
|
5
|
+
describe "#cache_strategy" do
|
6
|
+
let(:cache_strategy) { :ok }
|
7
|
+
|
8
|
+
before do
|
9
|
+
CurrencyExchange::Storage::Cache.cache_strategy = cache_strategy
|
10
|
+
end
|
11
|
+
|
12
|
+
it "should set a cache strategy" do
|
13
|
+
CurrencyExchange::Storage::Cache.cache_strategy.should == cache_strategy
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
describe "#load_instance" do
|
18
|
+
let(:cache_strategy) { nil }
|
19
|
+
subject { CurrencyExchange::Storage::Cache.send(:load_instance) }
|
20
|
+
|
21
|
+
before do
|
22
|
+
CurrencyExchange::Storage::Cache.cache_strategy = cache_strategy
|
23
|
+
end
|
24
|
+
|
25
|
+
it "should return self" do
|
26
|
+
subject.class.should == CurrencyExchange::Storage::Cache
|
27
|
+
end
|
28
|
+
|
29
|
+
context "when strategy is to rails cache" do
|
30
|
+
let(:cache_strategy) { :rails_cache }
|
31
|
+
|
32
|
+
it "should return rails cache storage" do
|
33
|
+
subject.class.should == CurrencyExchange::Storage::RailsCache
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
describe "#store" do
|
39
|
+
let(:key) { "abc" }
|
40
|
+
let(:value) { 10 }
|
41
|
+
subject { CurrencyExchange::Storage::Cache.new.store(key, value) }
|
42
|
+
|
43
|
+
it "should not store and return value" do
|
44
|
+
subject.should == value
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
end
|
49
|
+
|
@@ -0,0 +1,33 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe CurrencyExchange::Storage::RailsCache do
|
4
|
+
|
5
|
+
describe "#fetch" do
|
6
|
+
let(:cache_data) { "my data" }
|
7
|
+
let(:key) { 123 }
|
8
|
+
|
9
|
+
subject { CurrencyExchange::Storage::RailsCache.new.fetch(key) }
|
10
|
+
|
11
|
+
before do
|
12
|
+
Rails.stub_chain(:cache, :read).and_return(cache_data)
|
13
|
+
end
|
14
|
+
|
15
|
+
it "should retrieve from cache" do
|
16
|
+
subject.should == cache_data
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
describe "#store" do
|
21
|
+
let(:key) { 123 }
|
22
|
+
let(:value) { "ok" }
|
23
|
+
subject { CurrencyExchange::Storage::RailsCache.new.store(key, value) }
|
24
|
+
|
25
|
+
before do
|
26
|
+
Rails.stub_chain(:cache, :write).with(key, value, anything()).and_return(true)
|
27
|
+
end
|
28
|
+
|
29
|
+
it "should return back value" do
|
30
|
+
subject.should == value
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
@@ -0,0 +1,59 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe CurrencyExchange::Transporters::ExchangeTransporter do
|
4
|
+
|
5
|
+
describe "#load_instance" do
|
6
|
+
let(:strategy) { nil }
|
7
|
+
subject { CurrencyExchange::Transporters::ExchangeTransporter.load_instance(strategy) }
|
8
|
+
|
9
|
+
context "when strategy is to use json" do
|
10
|
+
let(:strategy) { :json }
|
11
|
+
|
12
|
+
it "should return json transporter" do
|
13
|
+
subject.class.should == CurrencyExchange::Transporters::ExchangeTransporter::TRANSPORTER_STRATEGY[:json]
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
it "should return an instance of itself" do
|
18
|
+
subject.class.should == CurrencyExchange::Transporters::ExchangeTransporter
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
describe "#fetch_data" do
|
23
|
+
let(:url) { "http://www.blah.com" }
|
24
|
+
|
25
|
+
subject { CurrencyExchange::Transporters::ExchangeTransporter.new.fetch_data(url) }
|
26
|
+
it "should throw a not implemented error" do
|
27
|
+
expect { subject }.to raise_error(NotImplementedError)
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
describe "#retrieve_rates" do
|
32
|
+
let(:url) { "http://www.blah.com" }
|
33
|
+
let(:fetch_data) { "fetched data" }
|
34
|
+
let(:store_data) { "store data" }
|
35
|
+
let(:storage) { mock('storage', :fetch => fetch_data, :store => store_data) }
|
36
|
+
let(:transporter) { CurrencyExchange::Transporters::ExchangeTransporter.new }
|
37
|
+
|
38
|
+
subject { transporter.retrieve_rates(url) }
|
39
|
+
|
40
|
+
before do
|
41
|
+
CurrencyExchange::Storage::Cache.stub(:instance).and_return(storage)
|
42
|
+
transporter.stub(:fetch_data)
|
43
|
+
end
|
44
|
+
|
45
|
+
it "should return data in storage" do
|
46
|
+
subject.should == fetch_data
|
47
|
+
end
|
48
|
+
|
49
|
+
context "when fetch data is nil" do
|
50
|
+
let(:fetch_data) { nil }
|
51
|
+
|
52
|
+
it "should store and return data" do
|
53
|
+
subject.should == store_data
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
end
|
59
|
+
|
@@ -0,0 +1,29 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe CurrencyExchange::Transporters::JsonTransporter do
|
4
|
+
|
5
|
+
describe "#fetch_data" do
|
6
|
+
let(:url) { "http://www.blah.com" }
|
7
|
+
let(:body) { '{"one":true}' }
|
8
|
+
let(:json) { { 'one' => true } }
|
9
|
+
let(:response) { mock("response", :body => body) }
|
10
|
+
|
11
|
+
subject { CurrencyExchange::Transporters::JsonTransporter.new.fetch_data url }
|
12
|
+
|
13
|
+
before do
|
14
|
+
RestClient.stub(:get).and_return(response)
|
15
|
+
end
|
16
|
+
|
17
|
+
it "should call rest client" do
|
18
|
+
RestClient.should_receive(:get).with(url, anything())
|
19
|
+
subject
|
20
|
+
end
|
21
|
+
|
22
|
+
it "should retrieve body of response" do
|
23
|
+
subject.should == json
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
end
|
28
|
+
|
29
|
+
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: currency_exchange
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 25
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 0
|
9
|
-
-
|
10
|
-
version: 0.0.
|
9
|
+
- 3
|
10
|
+
version: 0.0.3
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Thil Bandara
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2012-02-
|
18
|
+
date: 2012-02-15 00:00:00 Z
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|
21
21
|
name: rspec
|
@@ -43,7 +43,7 @@ dependencies:
|
|
43
43
|
segments:
|
44
44
|
- 0
|
45
45
|
version: "0"
|
46
|
-
type: :
|
46
|
+
type: :development
|
47
47
|
version_requirements: *id002
|
48
48
|
- !ruby/object:Gem::Dependency
|
49
49
|
name: json
|
@@ -57,10 +57,10 @@ dependencies:
|
|
57
57
|
segments:
|
58
58
|
- 0
|
59
59
|
version: "0"
|
60
|
-
type: :
|
60
|
+
type: :development
|
61
61
|
version_requirements: *id003
|
62
62
|
- !ruby/object:Gem::Dependency
|
63
|
-
name:
|
63
|
+
name: rails
|
64
64
|
prerelease: false
|
65
65
|
requirement: &id004 !ruby/object:Gem::Requirement
|
66
66
|
none: false
|
@@ -73,24 +73,8 @@ dependencies:
|
|
73
73
|
- 2
|
74
74
|
- 0
|
75
75
|
version: 3.2.0
|
76
|
-
type: :
|
76
|
+
type: :development
|
77
77
|
version_requirements: *id004
|
78
|
-
- !ruby/object:Gem::Dependency
|
79
|
-
name: rails
|
80
|
-
prerelease: false
|
81
|
-
requirement: &id005 !ruby/object:Gem::Requirement
|
82
|
-
none: false
|
83
|
-
requirements:
|
84
|
-
- - ">="
|
85
|
-
- !ruby/object:Gem::Version
|
86
|
-
hash: 15
|
87
|
-
segments:
|
88
|
-
- 3
|
89
|
-
- 2
|
90
|
-
- 0
|
91
|
-
version: 3.2.0
|
92
|
-
type: :runtime
|
93
|
-
version_requirements: *id005
|
94
78
|
description: A basic library to retrieve and convert currency
|
95
79
|
email:
|
96
80
|
- tbandara@gmail.com
|
@@ -110,7 +94,7 @@ files:
|
|
110
94
|
- lib/currency_exchange/exchangers/open_exchange.rb
|
111
95
|
- lib/currency_exchange/ext/fixnum.rb
|
112
96
|
- lib/currency_exchange/storage/cache.rb
|
113
|
-
- lib/currency_exchange/storage/
|
97
|
+
- lib/currency_exchange/storage/rails_cache.rb
|
114
98
|
- lib/currency_exchange/transporters/exchange_transporter.rb
|
115
99
|
- lib/currency_exchange/transporters/json_transporter.rb
|
116
100
|
- lib/currency_exchange/version.rb
|
@@ -118,6 +102,10 @@ files:
|
|
118
102
|
- spec/exchangers/open_exchange_spec.rb
|
119
103
|
- spec/ext/fixnum_spec.rb
|
120
104
|
- spec/spec_helper.rb
|
105
|
+
- spec/storage/cache_spec.rb
|
106
|
+
- spec/storage/rails_cache_spec.rb
|
107
|
+
- spec/transporters/exchange_transporter_spec.rb
|
108
|
+
- spec/transporters/json_transporter_spec.rb
|
121
109
|
homepage: https://github.com/thil/currency_exchange
|
122
110
|
licenses: []
|
123
111
|
|
@@ -156,3 +144,7 @@ test_files:
|
|
156
144
|
- spec/exchangers/open_exchange_spec.rb
|
157
145
|
- spec/ext/fixnum_spec.rb
|
158
146
|
- spec/spec_helper.rb
|
147
|
+
- spec/storage/cache_spec.rb
|
148
|
+
- spec/storage/rails_cache_spec.rb
|
149
|
+
- spec/transporters/exchange_transporter_spec.rb
|
150
|
+
- spec/transporters/json_transporter_spec.rb
|