arbolito 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: f6578546e1c600d31c863bb8595417a54c6f49e9
4
+ data.tar.gz: 50f297829ff304dbe48516d5d4a6871aa4a04f7b
5
+ SHA512:
6
+ metadata.gz: a5bd4af63f45e6b0856c3aaddda4cc2bbf173dab8eef549adfdef49f6c8afea2643fabe548525af5cb756cac85f7dd0ef121b1c35d5aa13d5f72058f5ed7180e
7
+ data.tar.gz: 67288a8b6a28bfb7393a265f572e9a3941539398f33c8afa86a514c4df400d84a9783f39422bb131ecaa2598283bb0c47ab2436049dbf795d00f2825e03df8e0
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.1.5
4
+ before_install: gem install bundler -v 1.10.4
@@ -0,0 +1,13 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
4
+
5
+ We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, ethnicity, age, or religion.
6
+
7
+ Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
8
+
9
+ Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
10
+
11
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
12
+
13
+ This Code of Conduct is adapted from the [Contributor Covenant](http://contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in arbolito.gemspec
4
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 jvillarejo
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,84 @@
1
+ # Arbolito
2
+
3
+ Arbolito is a minimalist Ruby API for currency conversions, it has no dependencies except the Ruby Standard Library.
4
+
5
+ It's like your Florida street best companion!
6
+
7
+ ![Florida Street](http://i.imgur.com/qupBCJN.jpg)
8
+
9
+ ## Features
10
+ * Doesn't encapsulates the currencies in any object model, it just use `BigDecimal` class and `Hash`
11
+ * You can add manually price rates
12
+ * If the rate isn't found it fetches the rate using Yahoo Finance API as exchange
13
+ * You implement your own exchange and configure it
14
+ * It stores in memory the rates fetched with a configurable expiration time
15
+ * You can implement your own store and configure it
16
+
17
+ ## Motivation
18
+ There were two reason why I've developed this gem.
19
+
20
+ First the argetine economy makes difficult to keep the prices updated with official exchanges rates like Yahoo Finance, so we had to manage our own prices rates. But also I wanted to know the rates of other currencies through Yahoo Finance. This gem lets you do both.
21
+
22
+ The second reason is that other gems implements their own money or currency model, and I didn't want to update all my models to manage those objects. That's why I tried to be agnostic on how you implement your currencies.
23
+
24
+ The API just receives a Big Decimal and a hash describing the conversion
25
+
26
+ ## Installation
27
+
28
+ Add this line to your application's Gemfile:
29
+
30
+ ```ruby
31
+ gem 'arbolito'
32
+ ```
33
+ And then execute:
34
+
35
+ $ bundle
36
+ Or install it yourself as:
37
+
38
+ $ gem install arbolito
39
+ ## Usage
40
+
41
+ ``` ruby
42
+ require 'arbolito'
43
+
44
+ # We can add multiple rates
45
+
46
+ Arbolito.add_currency_rate(BigDecimal.new(9), 'USD' => 'ARS')
47
+ Arbolito.add_currency_rate(BigDecimal.new(15), 'USD Blue' => 'ARS')
48
+ Arbolito.add_currency_rate(BigDecimal.new(12), 'USD Tarjeta' => 'ARS')
49
+ Arbolito.add_currency_rate(BigDecimal.new(6.5), 'USD Moreno' => 'ARS')
50
+
51
+ # It gives the stored rate
52
+ Arbolito.current_rate('USD' => 'ARS')
53
+ # => #<BigDecimal:7f061f0787e0,'0.15E2',9(27)>
54
+
55
+ # It stored rate backwards
56
+ Arbolito.current_rate('ARS' => 'USD')
57
+ # => #<BigDecimal:7f061f076af8,'0.6666666666 6666667E-1',18(36)>
58
+
59
+ # It can make the conversion for you
60
+ Arbolito.convert(BigDecimal.new(100), 'USD' => 'ARS')
61
+ # => #<BigDecimal:7f061f1bd010,'0.15E4',9(27)>
62
+
63
+ Arbolito.convert(BigDecimal.new(100), 'USD Tarjeta' => 'ARS')
64
+ => #<BigDecimal:7f061f39c818,'0.12E4',9(27)>
65
+
66
+ # Now we ask for the Uruguayan pesos to USD rate
67
+ Arbolito.current_rate('UYU' => 'USD')
68
+ #<BigDecimal:7f061f142ba8,'0.34E-1',9(18)>
69
+
70
+ # Now we ask to convert USD to Chilean Pesos
71
+ Arbolito.convert(BigDecimal.new(100), 'USD' => 'CLP')
72
+ => #<BigDecimal:7f061f35e680,'0.689695E5',18(36)>
73
+
74
+ ```
75
+
76
+ ## Contributing
77
+
78
+ Bug reports and pull requests are welcome on GitHub at https://github.com/jvillarjeo/arbolito. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](contributor-covenant.org) code of conduct.
79
+
80
+
81
+ ## License
82
+
83
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
84
+
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'arbolito/version'
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = "arbolito"
8
+ s.version = Arbolito::VERSION
9
+ s.authors = ["jvillarejo"]
10
+ s.email = ["contact@jonvillage.com"]
11
+
12
+ s.summary = "A currency conversion api for the minimalist developer"
13
+ s.description = s.summary
14
+ s.homepage = "https://github.com/jvillarejo/arbolito.git"
15
+ s.license = "MIT"
16
+ s.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
17
+ s.bindir = "exe"
18
+ s.executables = s.files.grep(%r{^exe/}) { |f| File.basename(f) }
19
+ s.require_paths = ["lib"]
20
+
21
+ s.add_development_dependency "bundler", "~> 1.10"
22
+ s.add_development_dependency "rake", "~> 10.0"
23
+ s.add_development_dependency "rspec"
24
+ s.add_development_dependency "pry"
25
+ end
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "arbolito"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,68 @@
1
+ require 'bigdecimal'
2
+ require 'time'
3
+ require 'arbolito/currency/quote'
4
+ require 'arbolito/currency/rate'
5
+ require 'arbolito/currency/non_expirable_rate'
6
+ require 'arbolito/store/memory'
7
+ require 'arbolito/exchange/yahoo_finance'
8
+ require "arbolito/version"
9
+
10
+ module Arbolito
11
+
12
+ class << self
13
+ def add_currency_rate(currency_price, from_to_currencies)
14
+ rate = Currency::NonExpirableRate.new(currency_price, from_to_currencies)
15
+ add_to_store(rate)
16
+ end
17
+
18
+ def current_rate(from_to_currencies)
19
+ fetch(Currency::Quote.new(from_to_currencies)).price
20
+ end
21
+
22
+ def convert(money, from_to_currencies)
23
+ quote = Currency::Quote.new(from_to_currencies)
24
+
25
+ rate = fetch(quote)
26
+
27
+ rate.convert(money)
28
+ end
29
+
30
+ def exchange=(exchange)
31
+ @@exchange = exchange
32
+ end
33
+
34
+ def expiration_time=(seconds)
35
+ @@expiration_time = seconds
36
+ end
37
+
38
+ private
39
+ def add_to_store(rate)
40
+ store.add(rate)
41
+ store.add(rate.backwards)
42
+ end
43
+
44
+ def fetch(quote)
45
+ rate = store.fetch(quote)
46
+
47
+ if(!rate || rate.expired?(expiration_time))
48
+ rate = exchange.find_current_rate(quote)
49
+
50
+ store.add(rate)
51
+ end
52
+
53
+ rate
54
+ end
55
+
56
+ def store
57
+ @@store ||= Store::Memory
58
+ end
59
+
60
+ def exchange
61
+ @@exchange ||= Exchange::YahooFinance
62
+ end
63
+
64
+ def expiration_time
65
+ @@expiration_time ||= 60
66
+ end
67
+ end
68
+ end
@@ -0,0 +1,9 @@
1
+ module Arbolito
2
+ module Currency
3
+ class NonExpirableRate < Rate
4
+ def expired?(expiration_time)
5
+ false
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,28 @@
1
+ module Arbolito
2
+ module Currency
3
+ class Quote
4
+ attr_reader :from, :to
5
+
6
+ def initialize(hash)
7
+ raise ArgumentError.new('You must specify 1 quote rate') if hash.size != 1
8
+
9
+ @from = hash.keys.first.to_sym
10
+ @to = hash.values.first.to_sym
11
+ end
12
+
13
+ def to_hash
14
+ { @from => @to }
15
+ end
16
+
17
+ def backwards
18
+ Quote.new( @to => @from )
19
+ end
20
+
21
+ def ==(other_currency_quote)
22
+ raise TypeError unless other_currency_quote.is_a?(Quote)
23
+
24
+ @from == other_currency_quote.from && @to == other_currency_quote.to
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,31 @@
1
+ module Arbolito
2
+ module Currency
3
+ class Rate
4
+ attr_reader :quote, :price, :rated_at
5
+
6
+ def initialize(price, from_to_hash, rated_at = Time.now)
7
+ @quote = Quote.new(from_to_hash)
8
+ @price = BigDecimal.new(price)
9
+ @rated_at = rated_at
10
+ end
11
+
12
+ def convert(money)
13
+ @price * BigDecimal.new(money)
14
+ end
15
+
16
+ def backwards
17
+ self.class.new(BigDecimal.new(1) / @price, @quote.backwards.to_hash)
18
+ end
19
+
20
+ def expired?(expiration_time)
21
+ (Time.now - rated_at) > expiration_time
22
+ end
23
+
24
+ def ==(other_currency_rate)
25
+ raise TypeError unless other_currency_rate.is_a?(CurrencyRate)
26
+
27
+ @quote == other_currency_rate.quote && @price == @other.price
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,38 @@
1
+ require 'net/http'
2
+
3
+ module Arbolito
4
+ module Exchange
5
+ class YahooFinance
6
+ class << self
7
+ def find_current_rate(quote)
8
+ data = response(build_uri(quote))
9
+
10
+ values = {
11
+ quote: quote,
12
+ price: data[1],
13
+ }
14
+
15
+ build_rate(values)
16
+ end
17
+
18
+ private
19
+ def response(uri)
20
+ response = Net::HTTP.get(uri)
21
+ data = response.gsub(/"|\\n/,'').split(',')
22
+ end
23
+
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
28
+
29
+ def build_rate(values)
30
+ Currency::Rate.new(
31
+ values[:price],
32
+ values[:quote].to_hash
33
+ )
34
+ end
35
+ end
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,37 @@
1
+ module Arbolito
2
+ module Store
3
+ class Memory
4
+ class << self
5
+ def mutex
6
+ @@mutex ||= Mutex.new
7
+ end
8
+
9
+ def add(rate)
10
+ synchronize do
11
+ hash[rate.quote.to_hash] = rate
12
+ end
13
+ end
14
+
15
+ def fetch(quote)
16
+ synchronize do
17
+ hash[quote.to_hash]
18
+ end
19
+ end
20
+
21
+ def clear
22
+ synchronize do
23
+ @@hash = {}
24
+ end
25
+ end
26
+
27
+ def synchronize(&block)
28
+ mutex.synchronize(&block)
29
+ end
30
+
31
+ def hash
32
+ @@hash ||= {}
33
+ end
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,3 @@
1
+ module Arbolito
2
+ VERSION = "0.1.0"
3
+ end
metadata ADDED
@@ -0,0 +1,118 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: arbolito
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - jvillarejo
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-10-22 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.10'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.10'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: pry
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description: A currency conversion api for the minimalist developer
70
+ email:
71
+ - contact@jonvillage.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - ".rspec"
78
+ - ".travis.yml"
79
+ - CODE_OF_CONDUCT.md
80
+ - Gemfile
81
+ - LICENSE.txt
82
+ - README.md
83
+ - Rakefile
84
+ - arbolito.gemspec
85
+ - bin/console
86
+ - bin/setup
87
+ - lib/arbolito.rb
88
+ - lib/arbolito/currency/non_expirable_rate.rb
89
+ - lib/arbolito/currency/quote.rb
90
+ - lib/arbolito/currency/rate.rb
91
+ - lib/arbolito/exchange/yahoo_finance.rb
92
+ - lib/arbolito/store/memory.rb
93
+ - lib/arbolito/version.rb
94
+ homepage: https://github.com/jvillarejo/arbolito.git
95
+ licenses:
96
+ - MIT
97
+ metadata: {}
98
+ post_install_message:
99
+ rdoc_options: []
100
+ require_paths:
101
+ - lib
102
+ required_ruby_version: !ruby/object:Gem::Requirement
103
+ requirements:
104
+ - - ">="
105
+ - !ruby/object:Gem::Version
106
+ version: '0'
107
+ required_rubygems_version: !ruby/object:Gem::Requirement
108
+ requirements:
109
+ - - ">="
110
+ - !ruby/object:Gem::Version
111
+ version: '0'
112
+ requirements: []
113
+ rubyforge_project:
114
+ rubygems_version: 2.4.8
115
+ signing_key:
116
+ specification_version: 4
117
+ summary: A currency conversion api for the minimalist developer
118
+ test_files: []