sbif 0.2.1 → 0.2.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. data/History.txt +8 -0
  2. data/lib/sbif.rb +25 -12
  3. metadata +2 -2
@@ -12,3 +12,11 @@
12
12
 
13
13
  * Fixed compability with Ruby 1.8.7
14
14
 
15
+ === 0.2.2 / 2011-11-13
16
+
17
+ * Handling exception when there is no connection to the site. We return "---" for now
18
+
19
+
20
+ === 0.2.3 / 2011-11-15
21
+
22
+ * Return nil if there is not data
@@ -4,7 +4,7 @@ require 'date'
4
4
 
5
5
  class SBIF
6
6
 
7
- VERSION = "0.2.1"
7
+ VERSION = "0.2.3"
8
8
  SITE = "http://api.sbif.cl/api-sbif/recursos"
9
9
  INDICATORS = %w(uf utm dolar euro)
10
10
 
@@ -25,11 +25,20 @@ class SBIF
25
25
  private
26
26
  def get_data(currency, date)
27
27
  date = format_date(:year => date[:year], :month => date[:month], :day => date[:day])
28
- content = open("#{SITE}/#{currency}/#{date}?apikey=#{self.api_key}&formato=json")
29
- result = parse_server_response(content)
30
- values = JSON.parse(result).values[0]
31
- if values.size == 1
32
- to_float(values[0]["Valor"])
28
+ begin
29
+ content = open("#{SITE}/#{currency}/#{date}?apikey=#{self.api_key}&formato=json")
30
+ rescue
31
+ content = ""
32
+ end
33
+ values = parse_server_response(content)
34
+ if values.size < 1
35
+ nil
36
+ elsif values.size == 1
37
+ begin
38
+ to_float(values[0]["Valor"])
39
+ rescue
40
+ nil
41
+ end
33
42
  else
34
43
  results = {}
35
44
  values.each do |v|
@@ -40,12 +49,16 @@ class SBIF
40
49
  end
41
50
 
42
51
  def parse_server_response(content)
43
- case
44
- when content.kind_of?(StringIO)
45
- content.string
52
+ result = ""
53
+ case
54
+ when content.kind_of?(StringIO)
55
+ result = content.string
46
56
  when content.kind_of?(Tempfile)
47
- content.read
57
+ result = content.read
58
+ else
59
+ result = JSON.generate({"Fecha" => "", "Valor" => "0"})
48
60
  end
61
+ JSON.parse(result).values[0]
49
62
  end
50
63
 
51
64
  def format_date(params = {})
@@ -56,9 +69,9 @@ class SBIF
56
69
  end
57
70
  date = date.join("/")
58
71
  end
59
-
72
+
60
73
  def to_float(string)
61
74
  string.gsub(".","").gsub(",",".").to_f
62
75
  end
63
76
 
64
- end
77
+ end
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: sbif
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.2.1
5
+ version: 0.2.3
6
6
  platform: ruby
7
7
  authors:
8
8
  - Patricio Bruna
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2011-11-12 00:00:00 Z
13
+ date: 2011-11-15 00:00:00 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: hoe