Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
2,511 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
--color | ||
--format progress |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,65 @@ | ||
require "bloomberg_quote/version" | ||
require "open-uri" | ||
require "nokogiri" | ||
require "json" | ||
|
||
module BloombergQuote | ||
# Your code goes here... | ||
class Quote | ||
def initialize(symbol) | ||
@symbol = symbol | ||
pull_data | ||
end | ||
|
||
def valid? | ||
return false unless @data | ||
@data.size > 0 | ||
end | ||
|
||
def data | ||
@data.nil? ? {} : @data | ||
end | ||
|
||
private | ||
def pull_data | ||
return if @symbol.empty? | ||
|
||
case @symbol | ||
when /:IND\Z/ | ||
pull_index_data | ||
end | ||
end | ||
|
||
def pull_index_data | ||
@data = {} | ||
|
||
fetch_page | ||
return unless @doc.css('div.ticker_header > span.price').text. | ||
gsub(',','') =~ /(\d+\.?(?:\d+))/ | ||
@data["Price"] = $1.to_f | ||
|
||
json = parse_json | ||
@data["Previous Close"] = json["prev_close"].to_f | ||
@data["Open"] = json["data_values"][0][1].to_f | ||
end | ||
|
||
def parse_json | ||
inline_scripts = @doc.xpath('//script[not(@src)]').map(&:text) | ||
inline_scripts.find do |js| | ||
js =~ /BLOOMBERG\.global_var\.chartData\s+?=\s+?(.+?);/ | ||
end | ||
JSON.parse($1) | ||
end | ||
|
||
def fetch_page | ||
@doc = Nokogiri::HTML(open_url) | ||
end | ||
|
||
def open_url | ||
open(quote_url) | ||
end | ||
|
||
def quote_url | ||
"http://www.bloomberg.com/quote/#{@symbol}" | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
require 'spec_helper' | ||
require 'bloomberg_quote' | ||
require 'fakeweb' | ||
|
||
describe BloombergQuote do | ||
describe 'Indexes' do | ||
describe '#valid?' do | ||
context 'when wrong symbol' do | ||
it { BloombergQuote::Quote.new('DUMMY:IND').valid?.should be_false } | ||
end | ||
|
||
context 'when right symbol' do | ||
it { BloombergQuote::Quote.new('TPX:IND').valid?.should be_true } | ||
end | ||
end | ||
|
||
describe '#data' do | ||
context 'when wrong symbol' do | ||
it { BloombergQuote::Quote.new('DUMMY:IND').data.should be_empty } | ||
end | ||
|
||
shared_examples_for 'TPX:IND data' do |options| | ||
subject { BloombergQuote::Quote.new('TPX:IND').data } | ||
it { should_not be_empty } | ||
it { should have_key('Price') } | ||
it { should have_key('Previous Close') } | ||
it { should have_key('Open') } | ||
its(['Price']) { should be_instance_of(Float) } | ||
its(['Previous Close']) { should be_instance_of(Float) } | ||
its(['Open']) { should be_instance_of(Float) } | ||
|
||
if options.has_key?(:use_fakeweb) && options[:use_fakeweb] | ||
its(['Price']) { should == 813.33 } | ||
its(['Previous Close']) { should == 819.27 } | ||
its(['Open']) { should == 814.37 } | ||
end | ||
end | ||
|
||
context 'when right symbol' do | ||
it_should_behave_like 'TPX:IND data', {} | ||
|
||
describe 'values' do | ||
before(:all) do | ||
FakeWeb.register_uri( | ||
:get, "http://www.bloomberg.com/quote/TPX:IND", | ||
:body => File.read('spec/fakeweb/tpx.html')) | ||
FakeWeb.allow_net_connect = false | ||
end | ||
|
||
after(:all) do | ||
FakeWeb.clean_registry | ||
end | ||
|
||
it_should_behave_like 'TPX:IND data', :use_fakeweb => true | ||
end | ||
end | ||
end | ||
end | ||
end |
Oops, something went wrong.