dolarhoy 0.1.3 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. data/bin/dolarhoy +16 -11
  2. data/lib/dolarhoy/currency.rb +20 -11
  3. metadata +29 -11
@@ -1,35 +1,40 @@
1
1
  #! /usr/bin/env ruby
2
- require 'iconv'
3
- require 'yaml'
2
+
3
+ require "nokogiri"
4
4
  require "dolarhoy/currency"
5
- require 'hpricot'
6
5
 
7
6
  html = if ARGV.first
8
7
  File.read(ARGV.first)
9
8
  else
10
9
  require 'net/http'
11
10
  require 'uri'
12
-
11
+
13
12
  response = Net::HTTP.start(URI.parse('http://www.dolarhoy.com').host) do |http|
14
13
  http.get('/indexx.php', 'Referer' => 'http://www.dolarhoy.com')
15
14
  end
16
-
17
- response.body
15
+
16
+ if RUBY_VERSION > "1.9"
17
+ response.body.force_encoding("ISO-8859-1").encode("UTF-8")
18
+ else
19
+ response.body
20
+ end
18
21
  end
19
22
 
20
- doc = Hpricot(Iconv.iconv('UTF-8', 'ISO-8859-1', html).first)
23
+ doc = Nokogiri::HTML(html)
21
24
 
22
25
  currencies = []
23
26
 
24
- doc.search("body > div table[@bgcolor='#000000']") do |table|
25
- values = table.search("font[@size='3']")
27
+ doc.css("body > div table[@bgcolor='#000000']").each do |table|
28
+ values = table.css("font[@size='3']")
26
29
  next unless values.size > 1
27
30
 
28
- currencies << Currency.new(
29
- table.search("td:first").inner_text,
31
+ currency = Currency.new(
32
+ table.at_css("td:first").inner_text,
30
33
  values[0].inner_text,
31
34
  values[1].inner_text
32
35
  )
36
+
37
+ currencies << currency if currency.aliased?
33
38
  end
34
39
 
35
40
  puts currencies.sort.join("\n")
@@ -1,5 +1,7 @@
1
1
  # encoding: UTF-8
2
2
 
3
+ require "yaml"
4
+
3
5
  class Currency
4
6
  include Comparable
5
7
 
@@ -12,7 +14,7 @@ class Currency
12
14
  end
13
15
 
14
16
  def <=>(other)
15
- name <=> other.name
17
+ self.alias <=> other.alias
16
18
  end
17
19
 
18
20
  def parse(text)
@@ -32,26 +34,33 @@ class Currency
32
34
  name.gsub!('Ú', 'U')
33
35
  name.gsub!("\r", " ")
34
36
  name.gsub!("\n", " ")
35
- name = name[/^([\w\s\.\$]+)/, 1].strip.capitalize.squeeze(' ')
36
- reverse_aliases[name] || name
37
+ name[/^([\w\s\.\$]+)/, 1].strip.capitalize.squeeze(' ')
37
38
  end
38
39
 
39
40
  def to_s
40
- "#{name} #{format buy} #{format sell}"
41
+ "#{self.alias} #{format buy} #{format sell} #{format average}"
41
42
  end
42
43
 
43
44
  def aliases
44
- @aliases ||= YAML.load_file(File.expand_path(File.join(File.dirname(__FILE__), '..', 'aliases.yml')))
45
+ @@aliases ||= YAML.load_file(File.expand_path(File.join(File.dirname(__FILE__), '..', 'aliases.yml')))
45
46
  end
46
47
 
47
48
  def reverse_aliases
48
- unless @reverse_aliases
49
- @reverse_aliases = {}
50
- aliases.each do |code,names|
51
- [*names].each {|name| @reverse_aliases[name] = code }
52
- end
49
+ @@reverse_aliases ||= aliases.inject({}) do |reverse, (code, names)|
50
+ [*names].each { |name| reverse[name] = code }
51
+ reverse
53
52
  end
53
+ end
54
+
55
+ def aliased?
56
+ !! self.alias
57
+ end
58
+
59
+ def alias
60
+ reverse_aliases[name]
61
+ end
54
62
 
55
- @reverse_aliases
63
+ def average
64
+ (buy + sell) / 2.0
56
65
  end
57
66
  end
metadata CHANGED
@@ -1,7 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dolarhoy
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ hash: 23
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 2
9
+ - 0
10
+ version: 0.2.0
5
11
  platform: ruby
6
12
  authors:
7
13
  - Damian Janowski
@@ -9,19 +15,25 @@ autorequire:
9
15
  bindir: bin
10
16
  cert_chain: []
11
17
 
12
- date: 2009-01-09 00:00:00 -03:00
18
+ date: 2009-01-09 00:00:00 -02:00
13
19
  default_executable:
14
20
  dependencies:
15
21
  - !ruby/object:Gem::Dependency
16
- name: hpricot
17
- type: :runtime
18
- version_requirement:
19
- version_requirements: !ruby/object:Gem::Requirement
22
+ name: nokogiri
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
20
26
  requirements:
21
27
  - - ">="
22
28
  - !ruby/object:Gem::Version
23
- version: "0.6"
24
- version:
29
+ hash: 3
30
+ segments:
31
+ - 1
32
+ - 4
33
+ - 2
34
+ version: 1.4.2
35
+ type: :runtime
36
+ version_requirements: *id001
25
37
  description:
26
38
  email: damian.janowski@gmail.com
27
39
  executables:
@@ -47,21 +59,27 @@ rdoc_options: []
47
59
  require_paths:
48
60
  - lib
49
61
  required_ruby_version: !ruby/object:Gem::Requirement
62
+ none: false
50
63
  requirements:
51
64
  - - ">="
52
65
  - !ruby/object:Gem::Version
66
+ hash: 3
67
+ segments:
68
+ - 0
53
69
  version: "0"
54
- version:
55
70
  required_rubygems_version: !ruby/object:Gem::Requirement
71
+ none: false
56
72
  requirements:
57
73
  - - ">="
58
74
  - !ruby/object:Gem::Version
75
+ hash: 3
76
+ segments:
77
+ - 0
59
78
  version: "0"
60
- version:
61
79
  requirements: []
62
80
 
63
81
  rubyforge_project:
64
- rubygems_version: 1.3.5
82
+ rubygems_version: 1.3.7
65
83
  signing_key:
66
84
  specification_version: 2
67
85
  summary: A command-line tool for DolarHoy.com.