-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhsx.rb
62 lines (44 loc) · 1.34 KB
/
hsx.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
require 'nokogiri'
require 'open-uri'
class Hsx
def initialize(name)
search_for = "HSX.com MovieStock : #{name}"
search_for = URI.escape(search_for)
search_url = "http://www.google.com/search?q=#{search_for}&btnI=3564"
@search_data = Nokogiri::HTML(open(search_url))
end
def price
price = @search_data.at_css('.security_summary p.value').text.strip
rem_price = @search_data.at_css('.security_summary p.value span').text.strip
price = price.gsub(rem_price,'').strip
price.gsub(/[^0-9.]/, "").to_f
end
def gross
gross = 0
@search_data.css('.data_column.last tr').each do |block|
if block.at_css('td.label').text == "Gross:"
gross = block.at_css('td:nth-child(2)').text.gsub(/\D/,'').to_i
return gross
end
end
return gross
end
def theaters
theaters = 0
@search_data.css('.data_column.last tr').each do |block|
if block.at_css('td.label').text == "Theaters:"
theaters = block.at_css('td:nth-child(2)').text.to_i
return theaters
end
end
return theaters
end
end
def format_n(number)
number.to_s.gsub(/(\d)(?=\d{3}+(?:\.|$))(\d{3}\..*)?/,'\1,\2')
end
puts "movie title?"
hsx = Hsx.new(gets.chomp)
puts "trading price: $#{hsx.price}"
puts "gross: $#{format_n(hsx.gross)}"
puts "theaters: #{format_n(hsx.theaters)}"