arbolito 0.2.0 → 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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 0ff6f1a76f1000bf5d8b5538269128ac8c86e612
4
- data.tar.gz: b03928212cfc737e58cc637f4f806b05e68ca278
3
+ metadata.gz: 39c67c7fc0b5d92aee360462456aa02a90b725f8
4
+ data.tar.gz: 970cdd3e45248a88523809ecdfc61437d6055f95
5
5
  SHA512:
6
- metadata.gz: 24bb5c6c42112c89f73bdb10810dfcaf8094808177feaeeb8d1f8e8d378c51c2a426e1e740f0c472191b7fdd0cbb9792638ffb23643750c53c8099a137627faa
7
- data.tar.gz: 2f8285ad78e8008d1ec509ba5fa0aa3c0e7a13c4e6ce3a6be7be3df5e9775db5d74c93efc540e1d1c8d07c7a4658f940124cbad56762658e62cf4349b3b960d4
6
+ metadata.gz: 369cd395a3c52d0b20b17e8578e6460a37b3487eec8f6a6434842ab8abfbfefc6505aa7a3a649f316263fd8b3cb72a350b72902ec2ee4d2b3c3d0d430c912617
7
+ data.tar.gz: aea0d2c5fad8944d11ad826815b7cf435cba0f4d1acd442129d77e3e68f4bc42da38be6d9a213e8a4c2305b6284b8aa6f01b5e717e23673becd32aa85c81171a
data/.gitignore CHANGED
@@ -7,3 +7,4 @@
7
7
  /pkg/
8
8
  /spec/reports/
9
9
  /tmp/
10
+ .env
data/README.md CHANGED
@@ -9,7 +9,7 @@ It's like your Florida street best companion!
9
9
  ## Features
10
10
  * Doesn't encapsulates the currencies in any object model, it just use `BigDecimal` class and `Hash`
11
11
  * You can add manually price rates
12
- * If the rate isn't found it fetches the rate using Yahoo Finance API as exchange
12
+ * If the rate isn't found it fetches the rate using Yahoo Finance API as exchange or you can implement your own exchange.
13
13
  * You implement your own exchange and configure it
14
14
  * It stores in memory the rates fetched with a configurable expiration time
15
15
  * You can implement your own store and configure it
@@ -83,6 +83,16 @@ Arbolito.set(:store, Store::Mongo)
83
83
 
84
84
  ```
85
85
 
86
+ # Update: 2017-11-03
87
+ Yahoo discountinued their Yahoo Finance API so I've implemented another Exchange [Alpha Vantage](https://www.alphavantage.co/).
88
+
89
+ To use you need to get an API Key from them and the configure it like this.
90
+
91
+ ```ruby
92
+ api_key = ENV['API_KEY']
93
+ Arbolito.set(:exchange, Arbolito::Exchange::AlphaVantage.new(api_key))
94
+ ```
95
+
86
96
  ## Implementing Store and Exchange
87
97
 
88
98
  Please take a look at the code to see how you can implement your own stores and exchanges.
@@ -20,6 +20,6 @@ Gem::Specification.new do |s|
20
20
 
21
21
  s.add_development_dependency "bundler", "~> 1.10"
22
22
  s.add_development_dependency "rake", "~> 10.0"
23
- s.add_development_dependency "rspec"
24
- s.add_development_dependency "pry"
23
+ s.add_development_dependency "rspec", "~> 3.3"
24
+ s.add_development_dependency "pry", "~> 0.10"
25
25
  end
@@ -5,6 +5,7 @@ require 'arbolito/currency/rate'
5
5
  require 'arbolito/currency/non_expirable_rate'
6
6
  require 'arbolito/store/memory'
7
7
  require 'arbolito/exchange/yahoo_finance'
8
+ require 'arbolito/exchange/alpha_vantage'
8
9
  require "arbolito/version"
9
10
 
10
11
  module Arbolito
@@ -0,0 +1,45 @@
1
+ require 'net/http'
2
+ require 'json'
3
+
4
+ module Arbolito
5
+ module Exchange
6
+ class AlphaVantage
7
+ BaseURL = 'https://www.alphavantage.co/query?function=CURRENCY_EXCHANGE_RATE&from_currency=%s&to_currency=%s&apikey=%s'.freeze
8
+
9
+ attr_reader :api_key
10
+
11
+ def initialize(api_key)
12
+ @api_key = api_key
13
+ end
14
+
15
+ def find_current_rate(quote)
16
+ data = response(build_uri(quote.from,quote.to))
17
+ rated_at_str = "#{data['6. Last Refreshed']} #{data['7. Time Zone']}"
18
+ rated_at = Time.strptime(rated_at_str,'%Y-%m-%d %H:%M:%S %Z')
19
+
20
+ values = {
21
+ quote: quote.to_hash,
22
+ price: data['5. Exchange Rate'],
23
+ rated_at: rated_at.localtime
24
+ }
25
+
26
+ Currency::Rate.new(
27
+ values[:price],
28
+ values[:quote].to_hash,
29
+ values[:rated_at]
30
+ )
31
+ end
32
+
33
+ def response(uri)
34
+ response = Net::HTTP.get(uri)
35
+ json = JSON.parse(response)
36
+ json['Realtime Currency Exchange Rate']
37
+ end
38
+
39
+ def build_uri(from, to)
40
+ url = BaseURL % [from.upcase, to.upcase, api_key]
41
+ URI(url)
42
+ end
43
+ end
44
+ end
45
+ end
@@ -3,35 +3,37 @@ require 'net/http'
3
3
  module Arbolito
4
4
  module Exchange
5
5
  class YahooFinance
6
- class << self
7
- def find_current_rate(quote)
8
- data = response(build_uri(quote))
6
+ def initialize(api_key = '')
7
+ end
8
+ # DEPRECATED Yahoo Finance is not supported anymore by Yahoo so use Alpha Vantage API
9
+ def find_current_rate(quote)
10
+ warn "[DEPRECATION] Yahoo Finance `find_current_rate` is deprecated. Please use AlphaVantage `find_current_rate` instead."
11
+ data = response(build_uri(quote))
9
12
 
10
- values = {
11
- quote: quote,
12
- price: data[1],
13
- }
13
+ values = {
14
+ quote: quote,
15
+ price: data[1],
16
+ }
14
17
 
15
- build_rate(values)
16
- end
18
+ build_rate(values)
19
+ end
17
20
 
18
- private
19
- def response(uri)
20
- response = Net::HTTP.get(uri)
21
- data = response.gsub(/"|\\n/,'').split(',')
22
- end
21
+ private
22
+ def response(uri)
23
+ response = Net::HTTP.get(uri)
24
+ data = response.gsub(/"|\\n/,'').split(',')
25
+ end
23
26
 
24
- def build_uri(quote)
25
- param = "#{quote.from.upcase}#{quote.to.upcase}"
26
- URI("http://download.finance.yahoo.com/d/quotes.csv?s=#{param}=X&f=nl1d1t1")
27
- end
27
+ def build_uri(quote)
28
+ param = "#{quote.from.upcase}#{quote.to.upcase}"
29
+ URI("http://download.finance.yahoo.com/d/quotes.csv?s=#{param}=X&f=nl1d1t1")
30
+ end
28
31
 
29
- def build_rate(values)
30
- Currency::Rate.new(
31
- values[:price],
32
- values[:quote].to_hash
33
- )
34
- end
32
+ def build_rate(values)
33
+ Currency::Rate.new(
34
+ values[:price],
35
+ values[:quote].to_hash
36
+ )
35
37
  end
36
38
  end
37
39
  end
@@ -1,3 +1,3 @@
1
1
  module Arbolito
2
- VERSION = "0.2.0"
2
+ VERSION = "0.3.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: arbolito
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - jvillarejo
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2015-10-28 00:00:00.000000000 Z
11
+ date: 2017-11-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -42,30 +42,30 @@ dependencies:
42
42
  name: rspec
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
- - - ">="
45
+ - - "~>"
46
46
  - !ruby/object:Gem::Version
47
- version: '0'
47
+ version: '3.3'
48
48
  type: :development
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
- - - ">="
52
+ - - "~>"
53
53
  - !ruby/object:Gem::Version
54
- version: '0'
54
+ version: '3.3'
55
55
  - !ruby/object:Gem::Dependency
56
56
  name: pry
57
57
  requirement: !ruby/object:Gem::Requirement
58
58
  requirements:
59
- - - ">="
59
+ - - "~>"
60
60
  - !ruby/object:Gem::Version
61
- version: '0'
61
+ version: '0.10'
62
62
  type: :development
63
63
  prerelease: false
64
64
  version_requirements: !ruby/object:Gem::Requirement
65
65
  requirements:
66
- - - ">="
66
+ - - "~>"
67
67
  - !ruby/object:Gem::Version
68
- version: '0'
68
+ version: '0.10'
69
69
  description: A currency conversion api for the minimalist developer
70
70
  email:
71
71
  - contact@jonvillage.com
@@ -88,6 +88,7 @@ files:
88
88
  - lib/arbolito/currency/non_expirable_rate.rb
89
89
  - lib/arbolito/currency/quote.rb
90
90
  - lib/arbolito/currency/rate.rb
91
+ - lib/arbolito/exchange/alpha_vantage.rb
91
92
  - lib/arbolito/exchange/yahoo_finance.rb
92
93
  - lib/arbolito/store/memory.rb
93
94
  - lib/arbolito/version.rb
@@ -111,7 +112,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
111
112
  version: '0'
112
113
  requirements: []
113
114
  rubyforge_project:
114
- rubygems_version: 2.4.8
115
+ rubygems_version: 2.5.1
115
116
  signing_key:
116
117
  specification_version: 4
117
118
  summary: A currency conversion api for the minimalist developer