currency_exchange 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -4,7 +4,7 @@ module CurrencyExchange
4
4
  LATEST_EXCHANGE_URL = "http://openexchangerates.org/latest.json"
5
5
 
6
6
  def initialize
7
- @transporter = CurrencyExchange::Transporters::ExchangeTransporter.new
7
+ @transporter = CurrencyExchange::Transporters::ExchangeTransporter.load_instance(:json)
8
8
  end
9
9
 
10
10
  def convert(number, from, to)
@@ -1,6 +1,8 @@
1
1
  module CurrencyExchange
2
2
  class Storage::Cache
3
3
 
4
+ CACHE_STRATEGY = { :memcache => CurrencyExchange::Storage::MemCache }
5
+
4
6
  class << self
5
7
  attr_accessor :cache_strategy
6
8
 
@@ -19,11 +21,7 @@ module CurrencyExchange
19
21
  private
20
22
 
21
23
  def self.load_instance
22
- if self.cache_strategy == :memcache
23
- CurrencyExchange::Storage::MemCache.new
24
- else
25
- self.new
26
- end
24
+ (CACHE_STRATEGY[cache_strategy] || self).new
27
25
  end
28
26
 
29
27
  end
@@ -5,15 +5,17 @@ rescue LoadError
5
5
  end
6
6
 
7
7
  module CurrencyExchange
8
- class Storage::MemCache < Storage::Cache
8
+ module Storage
9
+ class MemCache < Cache
9
10
 
10
- def fetch(key)
11
- Rails.cache.read(key)
12
- end
11
+ def fetch(key)
12
+ Rails.cache.read(key)
13
+ end
13
14
 
14
- def store(key, value)
15
- Rails.cache.write(key, value, :expires_in => 1.day)
16
- value
15
+ def store(key, value)
16
+ Rails.cache.write(key, value, :expires_in => 1.day)
17
+ value
18
+ end
17
19
  end
18
20
  end
19
21
  end
@@ -9,21 +9,23 @@ module CurrencyExchange
9
9
  module Transporters
10
10
  class ExchangeTransporter
11
11
 
12
+ TRANSPORTER_STRATEGY = { :json => CurrencyExchange::Transporters::JsonTransporter }
13
+
12
14
  def initialize
13
15
  @storage = CurrencyExchange::Storage::Cache.instance
14
16
  end
15
17
 
16
18
  def retrieve_rates(url)
17
- @storage.fetch(url) || @storage.store(url, fetch_json(url))
19
+ @storage.fetch(url) || @storage.store(url, fetch_data(url))
18
20
  end
19
21
 
20
- private
21
-
22
- def fetch_json(url)
23
- response = RestClient.get(url, {:accept => :json})
24
- JSON.parse(response.body)
22
+ def fetch_data(url)
23
+ raise NotImplementedError.new("fetch_data")
25
24
  end
26
25
 
26
+ def self.load_instance(transporter_strategy)
27
+ (TRANSPORTER_STRATEGY[transporter_strategy] || self).new
28
+ end
27
29
  end
28
30
  end
29
31
  end
@@ -0,0 +1,20 @@
1
+ begin
2
+ require 'rest-client'
3
+ require 'json'
4
+ rescue LoadError
5
+ raise "You don't have the required gems installed"
6
+ end
7
+
8
+ module CurrencyExchange
9
+ module Transporters
10
+ class JsonTransporter < ExchangeTransporter
11
+
12
+ def fetch_data(url)
13
+ response = RestClient.get(url, {:accept => :json})
14
+ JSON.parse(response.body)
15
+ end
16
+
17
+ end
18
+ end
19
+ end
20
+
@@ -1,3 +1,3 @@
1
1
  module CurrencyExchange
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
@@ -8,6 +8,7 @@ module CurrencyExchange
8
8
 
9
9
  module Transporters
10
10
  autoload :ExchangeTransporter, 'currency_exchange/transporters/exchange_transporter'
11
+ autoload :JsonTransporter, 'currency_exchange/transporters/json_transporter'
11
12
  end
12
13
 
13
14
  module Data
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: 29
4
+ hash: 27
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 1
10
- version: 0.0.1
9
+ - 2
10
+ version: 0.0.2
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-13 00:00:00 Z
18
+ date: 2012-02-14 00:00:00 Z
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
21
21
  name: rspec
@@ -112,6 +112,7 @@ files:
112
112
  - lib/currency_exchange/storage/cache.rb
113
113
  - lib/currency_exchange/storage/mem_cache.rb
114
114
  - lib/currency_exchange/transporters/exchange_transporter.rb
115
+ - lib/currency_exchange/transporters/json_transporter.rb
115
116
  - lib/currency_exchange/version.rb
116
117
  - spec/data/currencies_spec.rb
117
118
  - spec/exchangers/open_exchange_spec.rb