Skip to content

Commit

Permalink
Resolved issue #3. Error handling for some exception cases
Browse files Browse the repository at this point in the history
  • Loading branch information
Oscar Rodriguez authored and Oscar Rodriguez committed Feb 15, 2019
1 parent 50b8431 commit 4c9df79
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 37 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# BcnNi
This gem provides NIO (Córdoba oro Nicaragüense) versus USD (United States dollar) money exchange rates consuming the official Central Bank of Nicaragüa (BCN) SOAP Service
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

## Basic usage
```ruby
Expand Down Expand Up @@ -67,7 +67,7 @@ Add this line to your application's Gemfile:
gem 'bcn_ni', git: 'https://github.com/mldoscar/bcn_ni', branch: 'master'

# From ruby gems
gem 'bcn_ni', '>= 0.1.3'
gem 'bcn_ni', '>= 0.1.4'

# Using gem install
gem install bcn_ni
Expand Down
8 changes: 4 additions & 4 deletions bcn_ni.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ Gem::Specification.new do |s|
s.authors = ["Oscar Rodriguez"]
s.email = ["[email protected]"]
s.homepage = "https://github.com/mldoscar/bcn_ni"
s.summary = "This gem provides NIO (Córdoba oro Nicaragüense) versus USD (United States dollar) money exchange rates consuming the official Central Bank of Nicaragüa (BCN) SOAP Service"
s.description = "This tool pretends to be helpful for developers who can request exchange rates in a easier way"
s.summary = "This tool pretends to be helpful for developers who can request exchange rates from Nicaragua in a easier way"
s.description = "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"
s.license = "MIT"

s.files = Dir["{app,config,db,lib}/**/*", "MIT-LICENSE", "Rakefile", "README.md", "CODE_OF_CONDUCT.md"]

s.add_dependency "activesupport", "~> 5.1", ">= 5.1.3"
s.add_dependency "nokogiri", "~> 1.10"
s.add_dependency "activesupport", ">= 5.2"
s.add_dependency "nokogiri", ">= 1.6"

s.add_development_dependency "rake", "~> 12.3"
s.add_development_dependency "rspec", "~> 3.8"
Expand Down
20 changes: 16 additions & 4 deletions lib/bcn_ni/core.rb
Original file line number Diff line number Diff line change
@@ -1,15 +1,27 @@
module BcnNi
require File.expand_path(File.dirname(__FILE__)) + '/helpers/request'

@@exceptions = []

def self.exchange_month(year, month, args = {})
request = BcnNi::Request.new(args)
engine = BcnNi::Request.new(args)

result = engine.exchange_month(year, month)
@@exceptions = engine.exceptions

return request.exchange_month(year, month)
return result
end

def self.exchange_day(year, month, day, args = {})
request = BcnNi::Request.new(args)
engine = BcnNi::Request.new(args)

result = engine.exchange_day(year, month, day)
@@exceptions = engine.exceptions

return result
end

return request.exchange_day(year, month, day)
def self.exceptions
return @@exceptions
end
end
90 changes: 64 additions & 26 deletions lib/bcn_ni/helpers/request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,25 @@

module BcnNi
class Request
@request_url = ""
@request_url = nil
@request_mode = nil
@exceptions = nil

def request_url
return @request_url
end

def request_mode
return @request_mode
end

def exceptions
return @exceptions
end

def initialize(args = {})
@request_mode = (args[:request_mode] || :scrapping).to_sym
@exceptions = []

case @request_mode
when :scrapping
Expand All @@ -21,37 +35,43 @@ def initialize(args = {})
# See 'https://servicios.bcn.gob.ni/Tc_Servicio/ServicioTC.asmx' for more info about how to build a RAW SOAP request
@request_url = "https://servicios.bcn.gob.ni/Tc_Servicio/ServicioTC.asmx?WSDL"
else
raise Exception.new('Request mode not implemented')
raise NotImplementedError, 'Request mode not implemented'
end
end

def request_url
return @request_url
end

def request_mode
return @request_mode
end

def exchange_month(year, month)
case request_mode
when :scrapping
return scra__exchange_month(year, month)
when :soap
return soap__exchange_month(year, month)
else
raise Exception.new('Request mode not implemented')
begin
# Evaluate scrapping mode to call the correct method for processing the request
case request_mode
when :scrapping
return scra__exchange_month(year, month)
when :soap
return soap__exchange_month(year, month)
end
rescue Exception => e
# Save the exception into the exception list for future error messages or debugging
@exceptions.push e

# Return an empty value according to the called method
return []
end
end

def exchange_day(year, month, day)
case request_mode
when :scrapping
return scra__exchange_day(year, month, day)
when :soap
return soap__exchange_day(year, month, day)
else
raise Exception.new('Request mode not implemented')
begin
# Evaluate scrapping mode to call the correct method for processing the request
case request_mode
when :scrapping
return scra__exchange_day(year, month, day)
when :soap
return soap__exchange_day(year, month, day)
end
rescue Exception => e
# Save the exception into the exception list for future error messages or debugging
@exceptions.push e

# Return an empty value according to the called method
return nil
end
end

Expand All @@ -69,9 +89,27 @@ def scra__exchange_month(year, month)

# Generate the full url
full_url = request_url + '?' + args.to_param

# This loop prevents a random EOFError (main cause is not known yet)
retries = 0
response = nil
while true
# Raise an error if too many retries
raise StopIteration if retries >= 5

begin
response = open(full_url, { ssl_verify_mode: OpenSSL::SSL::VERIFY_NONE })
# Exit loop if response has been assigned
break
rescue EOFError => e
# Sum retry and sleep the thread for a while
retries += 1
sleep(2.seconds)
end
end

# Fetch and parse HTML document
doc = Nokogiri::HTML(open(full_url, { ssl_verify_mode: OpenSSL::SSL::VERIFY_NONE }))
# Parse the HTML document
doc = Nokogiri::HTML(response)

result = []
# Iterate table
Expand Down
2 changes: 1 addition & 1 deletion lib/bcn_ni/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module BcnNi
VERSION = '0.1.3'
VERSION = '0.1.4'
end
2 changes: 2 additions & 0 deletions spec/bcn_ni/bcn_ni_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

expected_rate = 30.3537

expect(BcnNi.exceptions).to be_empty
expect(exchange_rate).to eq expected_rate
end

Expand Down Expand Up @@ -48,6 +49,7 @@
{:date=> Date.parse('2017-09-30'), :value=>30.4146}
]

expect(BcnNi.exceptions).to be_empty
expect(exchange_table).to eq expected_table
end
end

0 comments on commit 4c9df79

Please sign in to comment.