djanowski-dolarhoy 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (6) hide show
  1. data/LICENSE +19 -0
  2. data/README +15 -0
  3. data/Rakefile +28 -0
  4. data/bin/dolarhoy +37 -0
  5. data/lib/dolarhoy/currency.rb +39 -0
  6. metadata +65 -0
data/LICENSE ADDED
@@ -0,0 +1,19 @@
1
+ Copyright (c) 2008 Damian Janowski
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ of this software and associated documentation files (the "Software"), to deal
5
+ in the Software without restriction, including without limitation the rights
6
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ copies of the Software, and to permit persons to whom the Software is
8
+ furnished to do so, subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included in
11
+ all copies or substantial portions of the Software.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
+ THE SOFTWARE.
data/README ADDED
@@ -0,0 +1,15 @@
1
+ Usage:
2
+
3
+ $ dolarhoy
4
+ 1.000 guaranies 0.672 0.780
5
+ Dolar australiano 2.340 2.460
6
+ Dolar canadiense 2.810 3.020
7
+ Dolar estadounidense en $ 3.440 3.470
8
+ Euro 4.590 4.730
9
+ Franco suizo 2.990 3.250
10
+ Libra esterlina 5.110 5.420
11
+ Peso chileno 0.005 0.006
12
+ Peso mexicano 0.215 0.267
13
+ Peso uruguayo 0.148 0.168
14
+ Real 1.640 1.800
15
+ Yen 0.035 0.039
data/Rakefile ADDED
@@ -0,0 +1,28 @@
1
+ require 'rake'
2
+ require 'rake/gempackagetask'
3
+ require 'rake/clean'
4
+ require 'spec/rake/spectask'
5
+
6
+ gem_spec_file = 'dolarhoy.gemspec'
7
+
8
+ gem_spec = eval(File.read(gem_spec_file)) rescue nil
9
+
10
+ Rake::GemPackageTask.new(gem_spec) do |pkg|
11
+ pkg.need_zip = false
12
+ pkg.need_tar = false
13
+ rm_f FileList['pkg/**/*.*']
14
+ end if gem_spec
15
+
16
+ desc "Generate the gemspec file."
17
+ task :gemspec do
18
+ require 'erb'
19
+
20
+ File.open(gem_spec_file, 'w') do |f|
21
+ f.write ERB.new(File.read("#{gem_spec_file}.erb")).result(binding)
22
+ end
23
+ end
24
+
25
+ desc "Builds and installs the gem."
26
+ task :install => :repackage do
27
+ `sudo gem install pkg/#{gem_spec.name}-#{gem_spec.version}.gem`
28
+ end
data/bin/dolarhoy ADDED
@@ -0,0 +1,37 @@
1
+ #! /usr/bin/env ruby
2
+ require 'rubygems'
3
+ require 'iconv'
4
+ require File.join(File.dirname(__FILE__), '..', 'lib', 'dolarhoy', 'currency')
5
+
6
+ gem 'hpricot'
7
+ require 'hpricot'
8
+
9
+ html = if ARGV.first
10
+ File.read(ARGV.first)
11
+ else
12
+ require 'net/http'
13
+ require 'uri'
14
+
15
+ response = Net::HTTP.start(URI.parse('http://www.dolarhoy.com').host) do |http|
16
+ http.get('/indexx.php', 'Referer' => 'http://www.dolarhoy.com')
17
+ end
18
+
19
+ response.body
20
+ end
21
+
22
+ doc = Hpricot(Iconv.iconv('UTF-8', 'ISO-8859-1', html).first)
23
+
24
+ currencies = []
25
+
26
+ doc.search("body > div table[@bgcolor='#000000']") do |table|
27
+ values = table.search("font[@size='3']")
28
+ next unless values.size > 1
29
+
30
+ currencies << Currency.new(
31
+ table.search("td:first").inner_text,
32
+ values[0].inner_text,
33
+ values[1].inner_text
34
+ )
35
+ end
36
+
37
+ puts currencies.sort.join("\n")
@@ -0,0 +1,39 @@
1
+ class Currency
2
+ include Comparable
3
+
4
+ attr_accessor :name, :buy, :sell
5
+
6
+ def initialize(name, buy, sell)
7
+ @name = sanitize_name(name)
8
+ @buy = parse(buy)
9
+ @sell = parse(sell)
10
+ end
11
+
12
+ def <=>(other)
13
+ name <=> other.name
14
+ end
15
+
16
+ def parse(text)
17
+ text[/([\d\.,]+)/, 1].to_f
18
+ end
19
+
20
+ def format(number)
21
+ "%.3f" % number
22
+ end
23
+
24
+ def sanitize_name(name)
25
+ name = name.dup
26
+ name.gsub!('Á', 'A')
27
+ name.gsub!('É', 'E')
28
+ name.gsub!('Í', 'I')
29
+ name.gsub!('Ó', 'O')
30
+ name.gsub!('Ú', 'U')
31
+ name.gsub!("\r", " ")
32
+ name.gsub!("\n", " ")
33
+ name[/^([\w\s\.\$]+)/, 1].strip.capitalize.squeeze(' ')
34
+ end
35
+
36
+ def to_s
37
+ "#{name.ljust(28)} #{format buy} #{format sell}"
38
+ end
39
+ end
metadata ADDED
@@ -0,0 +1,65 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: djanowski-dolarhoy
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Damian Janowski
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-01-09 00:00:00 -08:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: hpricot
17
+ version_requirement:
18
+ version_requirements: !ruby/object:Gem::Requirement
19
+ requirements:
20
+ - - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: "0.6"
23
+ version:
24
+ description:
25
+ email: damian.janowski@gmail.com
26
+ executables:
27
+ - dolarhoy
28
+ extensions: []
29
+
30
+ extra_rdoc_files: []
31
+
32
+ files:
33
+ - lib/dolarhoy/currency.rb
34
+ - bin/dolarhoy
35
+ - README
36
+ - LICENSE
37
+ - Rakefile
38
+ has_rdoc: false
39
+ homepage:
40
+ post_install_message:
41
+ rdoc_options: []
42
+
43
+ require_paths:
44
+ - lib
45
+ required_ruby_version: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - ">="
48
+ - !ruby/object:Gem::Version
49
+ version: "0"
50
+ version:
51
+ required_rubygems_version: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ version: "0"
56
+ version:
57
+ requirements: []
58
+
59
+ rubyforge_project:
60
+ rubygems_version: 1.2.0
61
+ signing_key:
62
+ specification_version: 2
63
+ summary: A command-line tool for DolarHoy.com.
64
+ test_files: []
65
+