bcn_ni 0.1.0 → 0.1.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/CODE_OF_CONDUCT.md +74 -0
- data/MIT-LICENSE +0 -0
- data/README.md +97 -70
- data/Rakefile +5 -0
- data/lib/bcn_ni.rb +2 -101
- data/lib/bcn_ni/core.rb +27 -0
- data/lib/bcn_ni/helpers/request.rb +229 -0
- data/lib/bcn_ni/version.rb +1 -1
- data/lib/tasks/bcn_ni_tasks.rake +0 -0
- metadata +50 -19
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: a1c66337b41a7c0920341442f6f5ee66ec69b4aeb6154c940bb9a58cd073dcea
|
4
|
+
data.tar.gz: fb5a527398cba7ca4f8bc8662f456701493a476876ff5956390ffd36f7e41dee
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 61512751cd5a396b6b236e06501681e269fc84f0e4e323276022c92a9d8890ad837b61cdd8027b82fe079de9c19415275e136c41349dfafc2173fb3a8ebfc03e
|
7
|
+
data.tar.gz: f8b98e011ddd4dc15f282d9c80cc5d6f60f99ae4ca6501ebee28c2cd534e0527a853c26c14ae3acc5bfefb8166af0062edc10ce7b0f64a4d449d244cad4cc032
|
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
In the interest of fostering an open and welcoming environment, we as
|
6
|
+
contributors and maintainers pledge to making participation in our project and
|
7
|
+
our community a harassment-free experience for everyone, regardless of age, body
|
8
|
+
size, disability, ethnicity, gender identity and expression, level of experience,
|
9
|
+
nationality, personal appearance, race, religion, or sexual identity and
|
10
|
+
orientation.
|
11
|
+
|
12
|
+
## Our Standards
|
13
|
+
|
14
|
+
Examples of behavior that contributes to creating a positive environment
|
15
|
+
include:
|
16
|
+
|
17
|
+
* Using welcoming and inclusive language
|
18
|
+
* Being respectful of differing viewpoints and experiences
|
19
|
+
* Gracefully accepting constructive criticism
|
20
|
+
* Focusing on what is best for the community
|
21
|
+
* Showing empathy towards other community members
|
22
|
+
|
23
|
+
Examples of unacceptable behavior by participants include:
|
24
|
+
|
25
|
+
* The use of sexualized language or imagery and unwelcome sexual attention or
|
26
|
+
advances
|
27
|
+
* Trolling, insulting/derogatory comments, and personal or political attacks
|
28
|
+
* Public or private harassment
|
29
|
+
* Publishing others' private information, such as a physical or electronic
|
30
|
+
address, without explicit permission
|
31
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
32
|
+
professional setting
|
33
|
+
|
34
|
+
## Our Responsibilities
|
35
|
+
|
36
|
+
Project maintainers are responsible for clarifying the standards of acceptable
|
37
|
+
behavior and are expected to take appropriate and fair corrective action in
|
38
|
+
response to any instances of unacceptable behavior.
|
39
|
+
|
40
|
+
Project maintainers have the right and responsibility to remove, edit, or
|
41
|
+
reject comments, commits, code, wiki edits, issues, and other contributions
|
42
|
+
that are not aligned to this Code of Conduct, or to ban temporarily or
|
43
|
+
permanently any contributor for other behaviors that they deem inappropriate,
|
44
|
+
threatening, offensive, or harmful.
|
45
|
+
|
46
|
+
## Scope
|
47
|
+
|
48
|
+
This Code of Conduct applies both within project spaces and in public spaces
|
49
|
+
when an individual is representing the project or its community. Examples of
|
50
|
+
representing a project or community include using an official project e-mail
|
51
|
+
address, posting via an official social media account, or acting as an appointed
|
52
|
+
representative at an online or offline event. Representation of a project may be
|
53
|
+
further defined and clarified by project maintainers.
|
54
|
+
|
55
|
+
## Enforcement
|
56
|
+
|
57
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
58
|
+
reported by contacting the project team at TODO: Write your email address. All
|
59
|
+
complaints will be reviewed and investigated and will result in a response that
|
60
|
+
is deemed necessary and appropriate to the circumstances. The project team is
|
61
|
+
obligated to maintain confidentiality with regard to the reporter of an incident.
|
62
|
+
Further details of specific enforcement policies may be posted separately.
|
63
|
+
|
64
|
+
Project maintainers who do not follow or enforce the Code of Conduct in good
|
65
|
+
faith may face temporary or permanent repercussions as determined by other
|
66
|
+
members of the project's leadership.
|
67
|
+
|
68
|
+
## Attribution
|
69
|
+
|
70
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
|
71
|
+
available at [http://contributor-covenant.org/version/1/4][version]
|
72
|
+
|
73
|
+
[homepage]: http://contributor-covenant.org
|
74
|
+
[version]: http://contributor-covenant.org/version/1/4/
|
data/MIT-LICENSE
CHANGED
File without changes
|
data/README.md
CHANGED
@@ -1,70 +1,97 @@
|
|
1
|
-
# BcnNi
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
#
|
9
|
-
|
10
|
-
|
11
|
-
#
|
12
|
-
|
13
|
-
#
|
14
|
-
|
15
|
-
#
|
16
|
-
#
|
17
|
-
#
|
18
|
-
#
|
19
|
-
#
|
20
|
-
#
|
21
|
-
#
|
22
|
-
#
|
23
|
-
#
|
24
|
-
#
|
25
|
-
#
|
26
|
-
#
|
27
|
-
#
|
28
|
-
#
|
29
|
-
#
|
30
|
-
#
|
31
|
-
#
|
32
|
-
#
|
33
|
-
#
|
34
|
-
#
|
35
|
-
#
|
36
|
-
#
|
37
|
-
#
|
38
|
-
#
|
39
|
-
#
|
40
|
-
#
|
41
|
-
#
|
42
|
-
#
|
43
|
-
#
|
44
|
-
#
|
45
|
-
#
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
```
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
1
|
+
# BcnNi
|
2
|
+
|
3
|
+
This gem provides NIO (Córdoba Oro Nicaragüense) against USD (United States Dollar) money exchange rates consuming the official Central Bank of Nicaragüa (BCN) SOAP Service or HTML page
|
4
|
+
|
5
|
+
## Basic usage
|
6
|
+
|
7
|
+
```ruby
|
8
|
+
# Returns the exchange rate for September 15th, 2017
|
9
|
+
day_rate = BcnNi.exchange_day(2017, 9, 15)
|
10
|
+
#
|
11
|
+
# => 30.3537
|
12
|
+
|
13
|
+
# Returns the exchange rate table for September, 2017
|
14
|
+
month_rate = BcnNi.exchange_month(2017, 9)
|
15
|
+
#
|
16
|
+
# => [
|
17
|
+
# {:date=>Fri, 01 Sep 2017, :value=>30.2969},
|
18
|
+
# {:date=>Sat, 02 Sep 2017, :value=>30.301},
|
19
|
+
# {:date=>Sun, 03 Sep 2017, :value=>30.305},
|
20
|
+
# {:date=>Mon, 04 Sep 2017, :value=>30.3091},
|
21
|
+
# {:date=>Tue, 05 Sep 2017, :value=>30.3131},
|
22
|
+
# {:date=>Wed, 06 Sep 2017, :value=>30.3172},
|
23
|
+
# {:date=>Thu, 07 Sep 2017, :value=>30.3212},
|
24
|
+
# {:date=>Fri, 08 Sep 2017, :value=>30.3253},
|
25
|
+
# {:date=>Sat, 09 Sep 2017, :value=>30.3293},
|
26
|
+
# {:date=>Sun, 10 Sep 2017, :value=>30.3334},
|
27
|
+
# {:date=>Mon, 11 Sep 2017, :value=>30.3374},
|
28
|
+
# {:date=>Tue, 12 Sep 2017, :value=>30.3415},
|
29
|
+
# {:date=>Wed, 13 Sep 2017, :value=>30.3455},
|
30
|
+
# {:date=>Thu, 14 Sep 2017, :value=>30.3496},
|
31
|
+
# {:date=>Fri, 15 Sep 2017, :value=>30.3537},
|
32
|
+
# {:date=>Sat, 16 Sep 2017, :value=>30.3577},
|
33
|
+
# {:date=>Sun, 17 Sep 2017, :value=>30.3618},
|
34
|
+
# {:date=>Mon, 18 Sep 2017, :value=>30.3658},
|
35
|
+
# {:date=>Tue, 19 Sep 2017, :value=>30.3699},
|
36
|
+
# {:date=>Wed, 20 Sep 2017, :value=>30.374},
|
37
|
+
# {:date=>Thu, 21 Sep 2017, :value=>30.378},
|
38
|
+
# {:date=>Fri, 22 Sep 2017, :value=>30.3821},
|
39
|
+
# {:date=>Sat, 23 Sep 2017, :value=>30.3861},
|
40
|
+
# {:date=>Sun, 24 Sep 2017, :value=>30.3902},
|
41
|
+
# {:date=>Mon, 25 Sep 2017, :value=>30.3943},
|
42
|
+
# {:date=>Tue, 26 Sep 2017, :value=>30.3983},
|
43
|
+
# {:date=>Wed, 27 Sep 2017, :value=>30.4024},
|
44
|
+
# {:date=>Thu, 28 Sep 2017, :value=>30.4065},
|
45
|
+
# {:date=>Fri, 29 Sep 2017, :value=>30.4105},
|
46
|
+
# {:date=>Sat, 30 Sep 2017, :value=>30.4146}
|
47
|
+
# ]
|
48
|
+
|
49
|
+
```
|
50
|
+
|
51
|
+
## Changing the request mode
|
52
|
+
|
53
|
+
For changing the request mode simply add `request_mode` argument at the end of the method args. **The default request mode is scrapping** (which incredibly resulted to be faster than SOAP and with 24/7 availability). You can change this like the example given below:
|
54
|
+
|
55
|
+
```ruby
|
56
|
+
# Scrapping mode
|
57
|
+
day_rate = BcnNi.exchange_day(2017, 9, 15, request_mode: :scrapping)
|
58
|
+
month_rate = BcnNi.exchange_month(2017, 9, request_mode: :scrapping)
|
59
|
+
# SOAP request mode
|
60
|
+
day_rate = BcnNi.exchange_day(2017, 9, 15, request_mode: :soap)
|
61
|
+
month_rate = BcnNi.exchange_month(2017, 9, request_mode: :soap)
|
62
|
+
```
|
63
|
+
|
64
|
+
## Installation
|
65
|
+
|
66
|
+
Add this line to your application's Gemfile:
|
67
|
+
|
68
|
+
```ruby
|
69
|
+
# From git repository
|
70
|
+
gem 'bcn_ni', git: 'https://github.com/mldoscar/bcn_ni', branch: 'master'
|
71
|
+
|
72
|
+
# From ruby gems
|
73
|
+
gem 'bcn_ni', '>= 0.1.5'
|
74
|
+
|
75
|
+
# Using gem install
|
76
|
+
gem install bcn_ni
|
77
|
+
```
|
78
|
+
|
79
|
+
And then execute:
|
80
|
+
|
81
|
+
```bash
|
82
|
+
$ bundle
|
83
|
+
```
|
84
|
+
|
85
|
+
## Changelog
|
86
|
+
|
87
|
+
```
|
88
|
+
2021.06.28 - Bugfix: Cambio en el URI para el request de scrapping, el BCN cambió de parámetros y ubicación de URL
|
89
|
+
```
|
90
|
+
|
91
|
+
## Contributing
|
92
|
+
|
93
|
+
You guys are free to send your pull requests for contributing. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.
|
94
|
+
|
95
|
+
## License
|
96
|
+
|
97
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
data/Rakefile
CHANGED
data/lib/bcn_ni.rb
CHANGED
@@ -1,103 +1,4 @@
|
|
1
|
-
require 'uri'
|
2
|
-
require 'net/http'
|
3
|
-
require 'net/https'
|
4
|
-
require 'json'
|
5
|
-
|
6
1
|
module BcnNi
|
7
|
-
|
8
|
-
|
9
|
-
# (see 'https://servicios.bcn.gob.ni/Tc_Servicio/ServicioTC.asmx')
|
10
|
-
# for more info about how to build a SOAP RAW request)
|
11
|
-
|
12
|
-
# Parse the URI
|
13
|
-
uri = URI.parse('https://servicios.bcn.gob.ni/Tc_Servicio/ServicioTC.asmx?WSDL')
|
14
|
-
|
15
|
-
# Create protocol to the URI
|
16
|
-
https = Net::HTTP.new(uri.host, uri.port)
|
17
|
-
https.use_ssl = true
|
18
|
-
|
19
|
-
# Create a new POST request as XML content type
|
20
|
-
req = Net::HTTP::Post.new(uri.path, initheader = {'Content-Type' => 'text/xml'})
|
21
|
-
|
22
|
-
# Set the request body as a RAW SOAP XML request
|
23
|
-
req.body = <<EOF
|
24
|
-
<?xml version="1.0" encoding="utf-8"?>
|
25
|
-
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
|
26
|
-
<soap:Body>
|
27
|
-
<RecuperaTC_Dia xmlns="http://servicios.bcn.gob.ni/">
|
28
|
-
<Ano>#{year}</Ano>
|
29
|
-
<Mes>#{month}</Mes>
|
30
|
-
<Dia>#{day}</Dia>
|
31
|
-
</RecuperaTC_Dia>
|
32
|
-
</soap:Body>
|
33
|
-
</soap:Envelope>
|
34
|
-
EOF
|
35
|
-
# Process the request
|
36
|
-
res = https.request(req)
|
37
|
-
|
38
|
-
# Get the XML response
|
39
|
-
xml_response = res.body
|
40
|
-
|
41
|
-
# Parse to a JSON hash
|
42
|
-
json_response = Hash.from_xml(xml_response)
|
43
|
-
|
44
|
-
# Get the result value
|
45
|
-
value_result = json_response['Envelope']['Body']['RecuperaTC_DiaResponse']['RecuperaTC_DiaResult']
|
46
|
-
|
47
|
-
# Parse the result value and finally return it
|
48
|
-
return value_result.to_f
|
49
|
-
|
50
|
-
end
|
51
|
-
|
52
|
-
def self.exchange_month(year, month)
|
53
|
-
# (see 'https://servicios.bcn.gob.ni/Tc_Servicio/ServicioTC.asmx')
|
54
|
-
# for more info about how to build a SOAP RAW request)
|
55
|
-
|
56
|
-
# Parse the URI
|
57
|
-
uri = URI.parse('https://servicios.bcn.gob.ni/Tc_Servicio/ServicioTC.asmx?WSDL')
|
58
|
-
|
59
|
-
# Create protocol to the URI
|
60
|
-
https = Net::HTTP.new(uri.host, uri.port)
|
61
|
-
https.use_ssl = true
|
62
|
-
|
63
|
-
# Create a new POST request as XML content type
|
64
|
-
req = Net::HTTP::Post.new(uri.path, initheader = {'Content-Type' => 'text/xml'})
|
65
|
-
|
66
|
-
# Set the request body as a RAW SOAP XML request
|
67
|
-
req.body = <<EOF
|
68
|
-
<?xml version="1.0" encoding="utf-8"?>
|
69
|
-
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
|
70
|
-
<soap:Body>
|
71
|
-
<RecuperaTC_Mes xmlns="http://servicios.bcn.gob.ni/">
|
72
|
-
<Ano>#{year}</Ano>
|
73
|
-
<Mes>#{month}</Mes>
|
74
|
-
</RecuperaTC_Mes>
|
75
|
-
</soap:Body>
|
76
|
-
</soap:Envelope>
|
77
|
-
EOF
|
78
|
-
# Process the request
|
79
|
-
res = https.request(req)
|
80
|
-
|
81
|
-
# Get the XML response
|
82
|
-
xml_response = res.body
|
83
|
-
|
84
|
-
# Parse to a JSON hash
|
85
|
-
json_response = Hash.from_xml(xml_response)
|
86
|
-
|
87
|
-
# Get the result array
|
88
|
-
exchange_table = json_response['Envelope']['Body']['RecuperaTC_MesResponse']['RecuperaTC_MesResult']['Detalle_TC']['Tc']
|
89
|
-
|
90
|
-
unless exchange_table.nil?
|
91
|
-
# Parse the table to a custom and better JSON
|
92
|
-
# The format example will be: {date: as Date, value: as Float}
|
93
|
-
parsed_table = exchange_table.map{|x| {date: Date.parse(x['Fecha']), value: x['Valor'].to_f} }
|
94
|
-
|
95
|
-
# Sort the parsed table and finally return it
|
96
|
-
return parsed_table.sort_by {|x| x[:date]}
|
97
|
-
else
|
98
|
-
return []
|
99
|
-
end
|
100
|
-
|
101
|
-
end
|
102
|
-
|
2
|
+
require 'bcn_ni/version'
|
3
|
+
require 'bcn_ni/core'
|
103
4
|
end
|
data/lib/bcn_ni/core.rb
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
module BcnNi
|
2
|
+
require File.expand_path(File.dirname(__FILE__)) + '/helpers/request'
|
3
|
+
|
4
|
+
@@exceptions = []
|
5
|
+
|
6
|
+
def self.exchange_month(year, month, args = {})
|
7
|
+
engine = BcnNi::Request.new(args)
|
8
|
+
|
9
|
+
result = engine.exchange_month(year, month)
|
10
|
+
@@exceptions = engine.exceptions
|
11
|
+
|
12
|
+
return result
|
13
|
+
end
|
14
|
+
|
15
|
+
def self.exchange_day(year, month, day, args = {})
|
16
|
+
engine = BcnNi::Request.new(args)
|
17
|
+
|
18
|
+
result = engine.exchange_day(year, month, day)
|
19
|
+
@@exceptions = engine.exceptions
|
20
|
+
|
21
|
+
return result
|
22
|
+
end
|
23
|
+
|
24
|
+
def self.exceptions
|
25
|
+
return @@exceptions
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,229 @@
|
|
1
|
+
require 'uri'
|
2
|
+
require 'net/http'
|
3
|
+
require 'net/https'
|
4
|
+
require 'nokogiri'
|
5
|
+
require 'open-uri'
|
6
|
+
require 'active_support/core_ext/hash'
|
7
|
+
require 'json'
|
8
|
+
|
9
|
+
module BcnNi
|
10
|
+
class Request
|
11
|
+
@request_url = nil
|
12
|
+
@request_mode = nil
|
13
|
+
@exceptions = nil
|
14
|
+
|
15
|
+
def request_url
|
16
|
+
return @request_url
|
17
|
+
end
|
18
|
+
|
19
|
+
def request_mode
|
20
|
+
return @request_mode
|
21
|
+
end
|
22
|
+
|
23
|
+
def exceptions
|
24
|
+
return @exceptions
|
25
|
+
end
|
26
|
+
|
27
|
+
def initialize(args = {})
|
28
|
+
@request_mode = (args[:request_mode] || :scrapping).to_sym
|
29
|
+
@exceptions = []
|
30
|
+
|
31
|
+
case @request_mode
|
32
|
+
when :scrapping
|
33
|
+
@request_url = "https://www.bcn.gob.ni/IRR/tipo_cambio_mensual/mes.php"
|
34
|
+
when :soap
|
35
|
+
# See 'https://servicios.bcn.gob.ni/Tc_Servicio/ServicioTC.asmx' for more info about how to build a RAW SOAP request
|
36
|
+
@request_url = "https://servicios.bcn.gob.ni/Tc_Servicio/ServicioTC.asmx?WSDL"
|
37
|
+
else
|
38
|
+
raise NotImplementedError, 'Request mode not implemented'
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
def exchange_month(year, month)
|
43
|
+
begin
|
44
|
+
# Evaluate scrapping mode to call the correct method for processing the request
|
45
|
+
case request_mode
|
46
|
+
when :scrapping
|
47
|
+
return scra__exchange_month(year, month)
|
48
|
+
when :soap
|
49
|
+
return soap__exchange_month(year, month)
|
50
|
+
end
|
51
|
+
rescue Exception => e
|
52
|
+
# Save the exception into the exception list for future error messages or debugging
|
53
|
+
@exceptions.push e
|
54
|
+
|
55
|
+
# Return an empty value according to the called method
|
56
|
+
return []
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
def exchange_day(year, month, day)
|
61
|
+
begin
|
62
|
+
# Evaluate scrapping mode to call the correct method for processing the request
|
63
|
+
case request_mode
|
64
|
+
when :scrapping
|
65
|
+
return scra__exchange_day(year, month, day)
|
66
|
+
when :soap
|
67
|
+
return soap__exchange_day(year, month, day)
|
68
|
+
end
|
69
|
+
rescue Exception => e
|
70
|
+
# Save the exception into the exception list for future error messages or debugging
|
71
|
+
@exceptions.push e
|
72
|
+
|
73
|
+
# Return an empty value according to the called method
|
74
|
+
return nil
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
private
|
79
|
+
def scra__exchange_month(year, month)
|
80
|
+
# Parse the date
|
81
|
+
start_date = Date.new(year, month, 1)
|
82
|
+
|
83
|
+
# Create the arg hash
|
84
|
+
args = {
|
85
|
+
mes: start_date.strftime('%d'),
|
86
|
+
anio: start_date.strftime('%Y')
|
87
|
+
}
|
88
|
+
|
89
|
+
# Generate the full url
|
90
|
+
full_url = request_url + '?' + args.to_param
|
91
|
+
|
92
|
+
# This loop prevents a random EOFError (main cause is not known yet)
|
93
|
+
retries = 0
|
94
|
+
response = nil
|
95
|
+
while true
|
96
|
+
# Raise an error if too many retries
|
97
|
+
raise StopIteration if retries >= 5
|
98
|
+
|
99
|
+
begin
|
100
|
+
response = open(full_url, { ssl_verify_mode: OpenSSL::SSL::VERIFY_NONE })
|
101
|
+
# Exit loop if response has been assigned
|
102
|
+
break
|
103
|
+
rescue EOFError => e
|
104
|
+
# Sum retry and sleep the thread for a while
|
105
|
+
retries += 1
|
106
|
+
sleep(2.seconds)
|
107
|
+
end
|
108
|
+
end
|
109
|
+
|
110
|
+
# Parse the HTML document
|
111
|
+
doc = Nokogiri::HTML(response)
|
112
|
+
|
113
|
+
result = []
|
114
|
+
# Iterate table
|
115
|
+
doc.css('table tr').each_with_index do |row, i|
|
116
|
+
next if i.zero?
|
117
|
+
|
118
|
+
# Parse the value
|
119
|
+
# (Date is not parsed because it's not given in a correct format, giving the month it in spanish)
|
120
|
+
# so, we must assume that dates are given in ascendent sort order
|
121
|
+
value = row.css('td')[1].content
|
122
|
+
|
123
|
+
# Push the result into the array
|
124
|
+
result.push({ date: start_date + (i - 1).day, value: value.strip.to_f })
|
125
|
+
end
|
126
|
+
|
127
|
+
return result
|
128
|
+
end
|
129
|
+
|
130
|
+
def scra__exchange_day(year, month, day)
|
131
|
+
# Parse the date
|
132
|
+
start_date = Date.new(year, month, day)
|
133
|
+
|
134
|
+
# Find the result into the month table
|
135
|
+
found_result = scra__exchange_month(year, month).find { |x| x[:date] == start_date }
|
136
|
+
|
137
|
+
# Return the result value
|
138
|
+
return (found_result || {})[:value]
|
139
|
+
end
|
140
|
+
|
141
|
+
def soap__exchange_day(year, month, day)
|
142
|
+
# Build body through a XML envelope
|
143
|
+
body = soap__envelope(soap__letter_exchange_day(year, month, day))
|
144
|
+
json_response = soap__request(body)
|
145
|
+
|
146
|
+
# Get the result value
|
147
|
+
value_result = json_response['Envelope']['Body']['RecuperaTC_DiaResponse']['RecuperaTC_DiaResult']
|
148
|
+
# Parse the result value and finally return it
|
149
|
+
return value_result.to_f
|
150
|
+
end
|
151
|
+
|
152
|
+
def soap__exchange_month(year, month)
|
153
|
+
# Build body through a XML envelope
|
154
|
+
body = soap__envelope(soap__letter_exchange_month(year, month))
|
155
|
+
json_response = soap__request(body)
|
156
|
+
|
157
|
+
# Get the result array
|
158
|
+
exchange_table = json_response['Envelope']['Body']['RecuperaTC_MesResponse']['RecuperaTC_MesResult']['Detalle_TC']['Tc']
|
159
|
+
|
160
|
+
if exchange_table.present?
|
161
|
+
# Parse the table to a custom and better JSON
|
162
|
+
# The format example will be: {date: as Date, value: as Float}
|
163
|
+
parsed_table = exchange_table.map{ |x| { date: Date.parse(x['Fecha']), value: x['Valor'].to_f } }
|
164
|
+
# Sort the parsed table and finally return it
|
165
|
+
return parsed_table.sort_by { |x| x[:date] }
|
166
|
+
else
|
167
|
+
return []
|
168
|
+
end
|
169
|
+
end
|
170
|
+
|
171
|
+
def soap__request(body)
|
172
|
+
# Parse the URI
|
173
|
+
uri = URI.parse(request_url)
|
174
|
+
# Create protocol to the URI
|
175
|
+
request_engine = Net::HTTP.new(uri.host, uri.port)
|
176
|
+
request_engine.use_ssl = true
|
177
|
+
request_engine.verify_mode = OpenSSL::SSL::VERIFY_NONE
|
178
|
+
request_engine.open_timeout = 15.seconds
|
179
|
+
|
180
|
+
# Create a new POST request as XML content type
|
181
|
+
req = Net::HTTP::Post.new(uri.path)
|
182
|
+
req['Content-Type'] = 'text/xml'
|
183
|
+
|
184
|
+
# Set the request body as a RAW SOAP XML request
|
185
|
+
req.body = body
|
186
|
+
# Process the request
|
187
|
+
res = request_engine.request(req)
|
188
|
+
# Get the XML response
|
189
|
+
xml_response = res.body
|
190
|
+
|
191
|
+
# Parse to a JSON hash
|
192
|
+
return Hash.from_xml(xml_response)
|
193
|
+
end
|
194
|
+
|
195
|
+
def soap__envelope(letter)
|
196
|
+
envelope = <<EOF
|
197
|
+
<?xml version="1.0" encoding="utf-8"?>
|
198
|
+
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
|
199
|
+
<soap:Body>
|
200
|
+
#{letter}
|
201
|
+
</soap:Body>
|
202
|
+
</soap:Envelope>
|
203
|
+
EOF
|
204
|
+
return envelope
|
205
|
+
end
|
206
|
+
|
207
|
+
def soap__letter_exchange_day(year, month, day)
|
208
|
+
letter = <<EOF
|
209
|
+
<RecuperaTC_Dia xmlns="http://servicios.bcn.gob.ni/">
|
210
|
+
<Ano>#{year}</Ano>
|
211
|
+
<Mes>#{month}</Mes>
|
212
|
+
<Dia>#{day}</Dia>
|
213
|
+
</RecuperaTC_Dia>
|
214
|
+
EOF
|
215
|
+
return letter
|
216
|
+
end
|
217
|
+
|
218
|
+
def soap__letter_exchange_month(year, month)
|
219
|
+
letter = <<EOF
|
220
|
+
<RecuperaTC_Mes xmlns="http://servicios.bcn.gob.ni/">
|
221
|
+
<Ano>#{year}</Ano>
|
222
|
+
<Mes>#{month}</Mes>
|
223
|
+
</RecuperaTC_Mes>
|
224
|
+
EOF
|
225
|
+
return letter
|
226
|
+
end
|
227
|
+
|
228
|
+
end
|
229
|
+
end
|
data/lib/bcn_ni/version.rb
CHANGED
data/lib/tasks/bcn_ni_tasks.rake
CHANGED
File without changes
|
metadata
CHANGED
@@ -1,62 +1,94 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: bcn_ni
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Oscar Rodriguez
|
8
|
-
autorequire:
|
8
|
+
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2021-06-28 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
|
-
name:
|
14
|
+
name: activesupport
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
17
|
- - "~>"
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version: 5.
|
19
|
+
version: '5.2'
|
20
20
|
type: :runtime
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
24
|
- - "~>"
|
25
25
|
- !ruby/object:Gem::Version
|
26
|
-
version: 5.
|
26
|
+
version: '5.2'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
|
-
name:
|
28
|
+
name: nokogiri
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
|
-
- - "
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1.6'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.6'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
32
46
|
- !ruby/object:Gem::Version
|
33
|
-
version: '
|
47
|
+
version: '12.3'
|
34
48
|
type: :development
|
35
49
|
prerelease: false
|
36
50
|
version_requirements: !ruby/object:Gem::Requirement
|
37
51
|
requirements:
|
38
|
-
- - "
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '12.3'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rspec
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '3.8'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
39
67
|
- !ruby/object:Gem::Version
|
40
|
-
version: '
|
41
|
-
description:
|
42
|
-
the
|
68
|
+
version: '3.8'
|
69
|
+
description: This gem provides NIO (Córdoba Oro Nicaragüense) against USD (United
|
70
|
+
States Dollar) money exchange rates consuming the official Central Bank of Nicaragüa
|
71
|
+
(BCN) SOAP Service or HTML page
|
43
72
|
email:
|
44
73
|
- mld.oscar@yahoo.com
|
45
74
|
executables: []
|
46
75
|
extensions: []
|
47
76
|
extra_rdoc_files: []
|
48
77
|
files:
|
78
|
+
- CODE_OF_CONDUCT.md
|
49
79
|
- MIT-LICENSE
|
50
80
|
- README.md
|
51
81
|
- Rakefile
|
52
82
|
- lib/bcn_ni.rb
|
83
|
+
- lib/bcn_ni/core.rb
|
84
|
+
- lib/bcn_ni/helpers/request.rb
|
53
85
|
- lib/bcn_ni/version.rb
|
54
86
|
- lib/tasks/bcn_ni_tasks.rake
|
55
87
|
homepage: https://github.com/mldoscar/bcn_ni
|
56
88
|
licenses:
|
57
89
|
- MIT
|
58
90
|
metadata: {}
|
59
|
-
post_install_message:
|
91
|
+
post_install_message:
|
60
92
|
rdoc_options: []
|
61
93
|
require_paths:
|
62
94
|
- lib
|
@@ -71,10 +103,9 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
71
103
|
- !ruby/object:Gem::Version
|
72
104
|
version: '0'
|
73
105
|
requirements: []
|
74
|
-
|
75
|
-
|
76
|
-
signing_key:
|
106
|
+
rubygems_version: 3.0.6
|
107
|
+
signing_key:
|
77
108
|
specification_version: 4
|
78
|
-
summary:
|
79
|
-
|
109
|
+
summary: This tool pretends to be helpful for developers who can request exchange
|
110
|
+
rates from Nicaragua in a easier way
|
80
111
|
test_files: []
|