dolar_realbr 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 36ae07856cf686111f1e1599367443c0685ffa23
4
+ data.tar.gz: dca3bb4dbe949c65b07b996b4b9b65663abf6788
5
+ SHA512:
6
+ metadata.gz: 3f205a3db5b3efd6a9dd385df894cadc3df861a6daac21e9b45c22475cf6739b81d7e7b9312c3434de1119caea2e2c11b43f9dbe7df8a5987e8f780177bfbd35
7
+ data.tar.gz: c652d7d20a900f8f34722ea3c29036c3a3132ef1c6a04a2c0e63cc2ccbbf07c366a3e1bf63045eb1d88225a6be738db3e1bacb102e55a10c676dcfffe697b830
data/.gitignore ADDED
@@ -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
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.1
4
+ before_install: gem install bundler -v 1.10.6
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in dolar_realbr.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Eduardo Alencar
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.
data/README.md ADDED
@@ -0,0 +1,118 @@
1
+ # DolarRealbr
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/dolar_realbr`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'dolar_realbr', '0.1.0'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+
20
+ ## Function
21
+
22
+ ```ruby
23
+ value_currency2realBR({currency: 'dollar', type: 'buy', date: 'DD/MM/YYYY'})
24
+ ```
25
+
26
+ ```ruby
27
+ value_realBR2currency({currency: 'dolar', type: 'buy', date: 'DD/MM/YYYY'})
28
+ ```
29
+
30
+ ```ruby
31
+ convert_currency2realBR({value: 1_000, currency: 'dolar', type: 'buy', date: 'DD/MM/YYYY'})
32
+ ```
33
+
34
+ ```ruby
35
+ convert_realBR2currency({value: 1.00, currency: 'dolar', type: 'buy', date: 'DD/MM/YYYY'})
36
+ ```
37
+
38
+
39
+ ## Usage
40
+
41
+
42
+ | NAME | CURRENCY | TYPE |
43
+ |--------------------|--------------------|------|
44
+ | Dollar | dollar | sale |
45
+ | Dollar | dollar | buy |
46
+ | Euro | euro | sale |
47
+ | Euro | euro | buy |
48
+ | Yen | yen | sale |
49
+ | Yen | yen | buy |
50
+ | Pound Sterling | pound_sterling | sale |
51
+ | Pound Sterling | pound_sterling | buy |
52
+ | Swiss Franc | swiss_franc | sale |
53
+ | Swiss Franc | swiss_franc | buy |
54
+ | Danish Krone | danish_krone | sale |
55
+ | Danish Krone | danish_krone | buy |
56
+ | Norwegian Krone | norwegian_krone | sale |
57
+ | Norwegian Krone | norwegian_krone | buy |
58
+ | Swedish Krona | swedish_krona | sale |
59
+ | Swedish Krona | swedish_krona | buy |
60
+ | Australian Dollar | australian_dollar | sale |
61
+ | Australian Dollar | australian_dollar | buy |
62
+ | Canadian Dollar | canadian_dollar | sale |
63
+ | Canadian Dollar | canadian_dollar | buy |
64
+
65
+
66
+ ```ruby
67
+ require 'dolar_realbr'
68
+
69
+ dol = DolarRealbr::Convert.new
70
+ ```
71
+
72
+ cotacao sem data
73
+
74
+ ```ruby
75
+ dol.value_value_currency2realBR({currency: 'euro', type: 'sale', date: '08/08/2014'})
76
+ dol.get_value // get value
77
+ dol.get_date // get date
78
+ ```
79
+
80
+ cotacao com data
81
+
82
+ ```ruby
83
+ dol.value_realBR2currency({currency: 'euro', type: 'sale', date: '08/08/2014'})
84
+ dol.get_value // get value
85
+ dol.get_date // get date
86
+ ```
87
+
88
+ conversao de valores sem data
89
+
90
+ ```ruby
91
+ dol.convert_currency2realBR({value: 100, currency: 'dollar', type: 'buy'})
92
+ dol.get_value // get value
93
+ dol.get_date // get date
94
+ ```
95
+
96
+ conversao de valores com data
97
+
98
+ ```ruby
99
+ dol.convert_currency2realBR({value: 100, currency: 'dollar', type: 'buy', date: '10/08/2015'})
100
+ dol.get_value // get value
101
+ dol.get_date // get date
102
+ ```
103
+
104
+ ## Development
105
+
106
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
107
+
108
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
109
+
110
+ ## Contributing
111
+
112
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/dolar_realbr.
113
+
114
+
115
+ ## License
116
+
117
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
118
+
data/Rakefile ADDED
@@ -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
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "dolar_realbr"
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
data/bin/setup ADDED
@@ -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,28 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'dolar_realbr/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "dolar_realbr"
8
+ spec.version = DolarRealbr::VERSION
9
+ spec.authors = ["Eduardo Alencar"]
10
+ spec.email = ["lebas66@gmail.com"]
11
+
12
+ spec.summary = %q{Exchange rate Real ( Brazil ), entre Dolar <-> Real e Euro <-> Real.}
13
+ spec.description = %q{Exchange rate Real ( Brazil ), entre Dolar <-> Real e Euro <-> Real.}
14
+ spec.homepage = "https://facebook.com/Solution4Mac"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ spec.bindir = "exe"
19
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.add_development_dependency "bundler", "~> 1.10"
23
+ spec.add_development_dependency "rake", "~> 10.0"
24
+ spec.add_development_dependency "rspec"
25
+
26
+ spec.add_dependency "savon", "~> 2.0"
27
+ spec.add_dependency "brazilian-rails"
28
+ end
@@ -0,0 +1,3 @@
1
+ module DolarRealbr
2
+ VERSION = "0.2.0"
3
+ end
@@ -0,0 +1,92 @@
1
+ require "dolar_realbr/version"
2
+ require "savon"
3
+ require "date"
4
+
5
+ module DolarRealbr
6
+ class Convert
7
+ def initialize()
8
+ @url_service = 'https://www3.bcb.gov.br/sgspub/JSP/sgsgeral/FachadaWSSGS.wsdl'
9
+ @CC = {'sale' => {'dollar' => 1, 'euro' => 21619, 'yen' => 21621, 'pound_sterling' => 21623, 'swiss_franc' => 21625, 'danish_krone' => 21627, 'norwegian_krone' => 21629, 'swedish_krona' => 21631, 'australian_dollar' => 21633, 'canadian_dollar' => 21635} , 'buy' => {'dollar' => 10813, 'euro' => 21620, 'yen' => 21622, 'pound_sterling' => 21624, 'swiss_franc' => 21626, 'danish_krone' => 21628, 'norwegian_krone' => 21630, 'swedish_krona' => 21632, 'australian_dollar' => 21634, 'canadian_dollar' => 21636} }
10
+ @CC.default(nil)
11
+ @value = @day = @name = @code = @unit = nil
12
+ @cli = Savon.client(wsdl: @url_service)
13
+ end
14
+
15
+ def check_date(day = nil)
16
+ unless day.nil?
17
+ dt = DateTime.new(day[6,4].to_i, day[3,2].to_i, day[0,2].to_i)
18
+ dt = dt.prev_day while dt.sunday? or dt.saturday?
19
+ @day = "#{dt.day.to_s.rjust(2,'0')}/#{dt.month.to_s.rjust(2,'0')}/#{dt.year}"
20
+ end
21
+ end
22
+
23
+ # params {currency: 'dollar', type: 'buy', date: 'DD/MM/YYYY'}
24
+ def value_currency2realBR(params)
25
+ @code = @CC.fetch("#{params[:type]}").fetch("#{params[:currency]}") if @CC.key?("#{params[:type]}") && @CC["#{params[:type]}"].key?("#{params[:currency]}")
26
+ unless @cli.nil? or @code.nil?
27
+ @value = @day = @name = @unit = nil
28
+ if params[:date].nil?
29
+ op = get_last_value_error
30
+ op = op.body.to_h[:get_ultimo_valor_xml_response][:get_ultimo_valor_xml_return] unless op.body.nil?
31
+ if op.class == Nori::StringWithAttributes ##Savon::Response
32
+ xml_file = Nokogiri::HTML(op.force_encoding("UTF-8"))
33
+ @unit = xml_file.css('unidade').text
34
+ @name = xml_file.css('nome').text
35
+ @day = "#{xml_file.css('ano').text}/#{xml_file.css('mes').text.rjust(2,'0')}/#{xml_file.css('dia').text.rjust(2,'0')}"
36
+ @value = xml_file.css('valor').text.gsub(',','.').to_f
37
+ end
38
+ else
39
+ self.check_date(params[:date])
40
+ op = get_value_error(@day)
41
+ @value = op.body.to_h[:multi_ref].to_f if op.class == Savon::Response
42
+ end
43
+ end
44
+ end
45
+
46
+ def get_last_value_error
47
+ @cli.call(:get_ultimo_valor_xml, message: {'in0' => @code})
48
+ rescue IOError => e
49
+ #erro na conexao
50
+ end
51
+
52
+ def get_value_error(params)
53
+ @cli.call(:get_valor, message: {'in0' => @code, 'in1' => params})
54
+ rescue IOError => e
55
+ #erro na conexao
56
+ end
57
+
58
+ # params {currency: 'dollar', type: 'buy', date: 'DD/MM/YYYY'}
59
+ def value_realBR2currency(params)
60
+ self.value_currency2realBR(params)
61
+ @value = @value**(-1) unless @value.nil? or @value.zero?
62
+ end
63
+
64
+ # params {value: 1.00, currency: 'dollar', type: 'buy', date: 'DD/MM/YYYY'}
65
+ def convert_currency2realBR(params)
66
+ self.value_currency2realBR(params)
67
+ @value *= params[:value].to_f unless @value.nil? or params[:value].nil?
68
+ end
69
+
70
+ # params {value: 1.00, currency: 'dollar', type: 'buy', date: 'DD/MM/YYYY'}
71
+ def convert_realBR2currency(params)
72
+ self.value_currency2realBR(params)
73
+ @value = params[:value].to_f/@value unless @value.nil? or params[:value].nil? or @value == 0
74
+ end
75
+
76
+ def get_name
77
+ @name
78
+ end
79
+
80
+ def get_value
81
+ @value
82
+ end
83
+
84
+ def get_day
85
+ @day
86
+ end
87
+
88
+ def get_unit_currency
89
+ @unit
90
+ end
91
+ end
92
+ end
metadata ADDED
@@ -0,0 +1,126 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: dolar_realbr
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.0
5
+ platform: ruby
6
+ authors:
7
+ - Eduardo Alencar
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-09-14 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: savon
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '2.0'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '2.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: brazilian-rails
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description: Exchange rate Real ( Brazil ), entre Dolar <-> Real e Euro <-> Real.
84
+ email:
85
+ - lebas66@gmail.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - ".gitignore"
91
+ - ".rspec"
92
+ - ".travis.yml"
93
+ - Gemfile
94
+ - LICENSE.txt
95
+ - README.md
96
+ - Rakefile
97
+ - bin/console
98
+ - bin/setup
99
+ - dolar_realbr.gemspec
100
+ - lib/dolar_realbr.rb
101
+ - lib/dolar_realbr/version.rb
102
+ homepage: https://facebook.com/Solution4Mac
103
+ licenses:
104
+ - MIT
105
+ metadata: {}
106
+ post_install_message:
107
+ rdoc_options: []
108
+ require_paths:
109
+ - lib
110
+ required_ruby_version: !ruby/object:Gem::Requirement
111
+ requirements:
112
+ - - ">="
113
+ - !ruby/object:Gem::Version
114
+ version: '0'
115
+ required_rubygems_version: !ruby/object:Gem::Requirement
116
+ requirements:
117
+ - - ">="
118
+ - !ruby/object:Gem::Version
119
+ version: '0'
120
+ requirements: []
121
+ rubyforge_project:
122
+ rubygems_version: 2.4.6
123
+ signing_key:
124
+ specification_version: 4
125
+ summary: Exchange rate Real ( Brazil ), entre Dolar <-> Real e Euro <-> Real.
126
+ test_files: []