kitco 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. data/README.md +27 -0
  2. data/bin/kitco +46 -0
  3. data/lib/kitco.rb +44 -0
  4. metadata +63 -0
data/README.md ADDED
@@ -0,0 +1,27 @@
1
+ Kitco Gem
2
+ =========
3
+
4
+ This gem retrieves data (via HTTParty) from Kitco Charts about Gold and other precious metals.
5
+
6
+ Use at your own discretion
7
+
8
+ Install
9
+ --------
10
+ gem install kitco
11
+
12
+
13
+ Examples
14
+ --------
15
+ require 'kitco'
16
+
17
+ puts Kitco.gold
18
+ puts Kitco.silver
19
+
20
+ Command Line Utility
21
+ --------------------
22
+
23
+ kitco gold
24
+
25
+ For more details on run
26
+
27
+ kitco --help
data/bin/kitco ADDED
@@ -0,0 +1,46 @@
1
+ #!/usr/bin/env ruby
2
+ require 'kitco'
3
+ require 'optparse'
4
+
5
+ options = { :color => true }
6
+ symbols = [:gold, :sliver, :palladium, :rhodium]
7
+
8
+ optparse = OptionParser.new do |opts|
9
+ opts.banner << "usage: kitco [options] symbol\n"
10
+ opts.banner << "where symbol is #{ symbols.collect { |s| s.upcase }.join(' ') } (case-insensitive)\n\nOptions:"
11
+
12
+ opts.on '-h', '--help', 'Display this message' do |arg|
13
+ options[:help] = arg
14
+ end
15
+
16
+ opts.on '-c', '--no-color', 'No color in output' do |arg|
17
+ options[:color] = arg
18
+ end
19
+ end
20
+
21
+ optparse.parse!
22
+
23
+ at_exit { puts optparse unless $!.nil? }
24
+ exit(1) if options[:help] || ARGV.size < 1
25
+
26
+ symbol = ARGV.pop.downcase.to_sym
27
+ exit(1) unless symbols.include? symbol
28
+
29
+ if Kitco.public_methods.include? symbol
30
+ begin
31
+ body = Kitco.send(symbol).parsed_response
32
+
33
+ puts "Results for #{symbol.upcase} (#{body[:time].strftime('%H:%M %m/%d/%y')})"
34
+ puts "High #{body[:high]}\tLow #{body[:low]}"
35
+ puts "Bid #{body[:bid]}\tAsk #{body[:ask]}"
36
+
37
+ if options[:color]
38
+ puts "Change: \e[#{body[:change] >= 0 ? '32m+' : '31m-'}#{body[:change]} (#{body[:percentage] * 100}%)\e[0m"
39
+ else
40
+ puts "Change #{body[:change] >= 0 ? '32m+' : '31m-'}#{body[:change]} (#{body[:percentage] * 100}%)"
41
+ end
42
+
43
+ rescue Exception => e
44
+ puts "Failure: #{e}"
45
+ end
46
+ end
data/lib/kitco.rb ADDED
@@ -0,0 +1,44 @@
1
+ require 'httparty'
2
+ require 'base64'
3
+ require 'time'
4
+
5
+ class Kitco
6
+ include HTTParty
7
+
8
+ base_uri "charts.kitco.com/KitcoCharts"
9
+ parser(lambda { |body, format|
10
+ # they don't send an XML content-type if its a bad request
11
+ return Crack::XML.parse(body) if body.include?('xml')
12
+
13
+ results = {}
14
+ data = Base64.decode64(body).split(',')
15
+
16
+ results[:time] = Time.parse data.shift
17
+ results[:percentage] = data.pop.to_f / 100.0
18
+
19
+ data.collect! { |n| n.to_f }
20
+ results.merge! Hash[ [:bid, :ask, :change, :low, :high].zip(data) ]
21
+ })
22
+
23
+ def self.gold
24
+ request :gold
25
+ end
26
+
27
+ def self.silver
28
+ request :silver
29
+ end
30
+
31
+ def self.palladium
32
+ request :palladium
33
+ end
34
+
35
+ def self.rhodium
36
+ request :rhodium
37
+ end
38
+
39
+ protected
40
+
41
+ def self.request symbol
42
+ get '/RequestHandler', :query => {:requestName => 'getSymbolSnapshot', :Symbol => symbol.upcase }
43
+ end
44
+ end
metadata ADDED
@@ -0,0 +1,63 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: kitco
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - mikeycgto
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2011-08-10 00:00:00.000000000 -04:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: httparty
17
+ requirement: &12403280 !ruby/object:Gem::Requirement
18
+ none: false
19
+ requirements:
20
+ - - ! '>='
21
+ - !ruby/object:Gem::Version
22
+ version: 0.7.8
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: *12403280
26
+ description: An API for accessing data from Kitco Charts. Also includes a command
27
+ line utility
28
+ email: mikeycgto@gmail.com
29
+ executables:
30
+ - kitco
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - README.md
35
+ - lib/kitco.rb
36
+ - bin/kitco
37
+ has_rdoc: true
38
+ homepage: https://github.com/mikeycgto/kitco
39
+ licenses: []
40
+ post_install_message:
41
+ rdoc_options: []
42
+ require_paths:
43
+ - lib
44
+ required_ruby_version: !ruby/object:Gem::Requirement
45
+ none: false
46
+ requirements:
47
+ - - ! '>='
48
+ - !ruby/object:Gem::Version
49
+ version: '0'
50
+ required_rubygems_version: !ruby/object:Gem::Requirement
51
+ none: false
52
+ requirements:
53
+ - - ! '>='
54
+ - !ruby/object:Gem::Version
55
+ version: '0'
56
+ requirements: []
57
+ rubyforge_project:
58
+ rubygems_version: 1.5.2
59
+ signing_key:
60
+ specification_version: 3
61
+ summary: An API for accessing data from Kitco Charts. Also includes a command line
62
+ utility!
63
+ test_files: []