nubefact 0.0.5 → 0.0.6

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: f2252343e627455539a50af6333cee4c19e20ef0
4
- data.tar.gz: 11531c9c00b2040508b476e461f215cf1cfc6126
3
+ metadata.gz: 2c353c016ec464eca5eebcd165e631145b636dd6
4
+ data.tar.gz: 2a51fbba354ffec4c21007cfab6af48d062accf4
5
5
  SHA512:
6
- metadata.gz: 92860839372d361f7c14361e33de2cc4bee2b7aba46b3030022503c509a79a9b4d9d44892ac15b96895c71b4b869287e6d555328312b726d1e2c403ef822eb58
7
- data.tar.gz: c2ee49792897db3f3add27a4bbd3db92a9e9a1391d366de73e4207925b6d9eb405954fc482b073e017400b10735c4179674d55292560dede20c0ef2a31913a3c
6
+ metadata.gz: a3f2a6fe5dc6b362e77f8df8b1c5087ae6dd69772106bb122821e247aa5c222b75fb05397724b736508869821ce77fe7a6bfb22d4cd7c7522207dcbef373d88a
7
+ data.tar.gz: 6c17f910c34d8455c11ccd5a03c75c90e859699f87a2ef0fdf5a6b728038fe9a64ad1249256f73cf730cf7a2938d90677039b7d0dcaaec33434beb5beebf21da
@@ -1,3 +1,3 @@
1
1
  module NubeFact
2
- VERSION = "0.0.5"
2
+ VERSION = "0.0.6"
3
3
  end
data/lib/util/sunat.rb CHANGED
@@ -1,3 +1,4 @@
1
+ require 'date'
1
2
  require 'nokogiri'
2
3
  require 'open-uri'
3
4
 
@@ -11,21 +12,57 @@ require 'open-uri'
11
12
  module NubeFact::Sunat
12
13
  extend self
13
14
 
14
- def dollar_rate(time = Time.now)
15
- @dollar ||= update_dollar_rate
15
+ def dollar_rate(date = Date.today)
16
+ begin
17
+ dollar_from_sunat(date)
18
+ rescue => e
19
+ # only rely on preciodolar for current day
20
+ raise e unless date == Date.today
21
+
22
+ dollar_from_preciodolar(date)
23
+ end
16
24
  end
17
25
 
18
- def update_dollar_rate(time = Time.now)
19
- dollar_from_sunat(time) rescue dollar_from_preciodolar(time)
20
- end
26
+ def dollar_from_sunat(date = Date.today)
27
+ raise InvalidDate if date.year < 2000 || date.year > Time.now.year
21
28
 
22
- def dollar_from_sunat(time = Time.now)
23
- doc = Nokogiri::HTML(open("http://www.sunat.gob.pe/cl-at-ittipcam/tcS01Alias?mes=#{time.month}&anho=#{time.year}"))
24
- begin
25
- rate = doc.at("td.H3 strong:contains(#{time.day})").parent.parent.at('td:last').text.strip
26
- rescue
27
- warn "Not able to get for the specific day #{time}, using the latest available"
28
- rate = doc.at ('td.tne10:last').text.strip
29
+ doc = Nokogiri::HTML(open("http://www.sunat.gob.pe/cl-at-ittipcam/tcS01Alias?mes=#{date.month}&anho=#{date.year}")) do |config|
30
+ config.noblanks
31
+ end
32
+
33
+ result = {}
34
+ day = nil
35
+ doc.css('td.H3, td.tne10').each do |td|
36
+ if day
37
+ result[day] << td.text.strip
38
+ day = nil if result[day].count == 2
39
+ else
40
+ day = td.at(:strong).text.strip.to_i
41
+ result[day] = []
42
+ end
43
+ end
44
+
45
+ # result is {day: [buy, sell], day: [buy, sell]}
46
+
47
+ if result[date.day]
48
+ rate = result[date.day].last # venta
49
+ else
50
+ # try to get the nearest day.
51
+ i = 0
52
+ while i < date.day
53
+ if result[i]
54
+ rate = result[i].last
55
+ end
56
+ i += 1
57
+ end
58
+
59
+ unless rate
60
+ # not possible to get the previous date, lests why with previous month
61
+ prev_month = date.to_datetime.prev_month
62
+ prev_month = Date.new(prev_month.year, prev_month.month, -1)
63
+ warn "Checking with previous month #{prev_month}"
64
+ rate = dollar_from_sunat(prev_month)
65
+ end
29
66
  end
30
67
 
31
68
  BigDecimal.new(rate)
@@ -48,4 +85,6 @@ module NubeFact::Sunat
48
85
 
49
86
  result.last["sell"]
50
87
  end
88
+
89
+ class InvalidDate < StandardError; end;
51
90
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: nubefact
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.5
4
+ version: 0.0.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Arnold Roa