Skip to content

Commit

Permalink
historical price added
Browse files Browse the repository at this point in the history
  • Loading branch information
ankitweinvest committed Sep 14, 2017
1 parent d06e509 commit 73a7b92
Show file tree
Hide file tree
Showing 5 changed files with 514 additions and 1 deletion.
1 change: 1 addition & 0 deletions coinmarketcap.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ Gem::Specification.new do |spec|
spec.require_paths = ["lib"]

spec.add_runtime_dependency 'httparty'
spec.add_runtime_dependency 'nokogiri'
spec.add_development_dependency 'bundler', '~> 1.14'
spec.add_development_dependency 'rake', '~> 10.0'
spec.add_development_dependency 'rspec', '~> 3.0'
Expand Down
28 changes: 28 additions & 0 deletions lib/coinmarketcap.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
require 'coinmarketcap/version'
require 'open-uri'
require 'httparty'
require 'nokogiri'

module Coinmarketcap

Expand All @@ -19,4 +21,30 @@ def self.global(currency = 'USD')
HTTParty.get("https://api.coinmarketcap.com/v1/global/?convert=#{currency}")
end

def self.get_historical_price(id, start_date, end_date) #20170908
prices = []
doc = Nokogiri::HTML(open("https://coinmarketcap.com/currencies/#{id}/historical-data/?start=#{start_date}&end=#{end_date}"))
rows = doc.css('tr')
if rows.count == 31
doc = Nokogiri::HTML(open("https://coinmarketcap.com/assets/#{id}/historical-data/?start=#{start_date}&end=#{end_date}"))
rows = doc.css('tr')
end
rows.shift
rows.each do |row|
begin
price_bundle = {}
each_row = Nokogiri::HTML(row.to_s).css('td')
price_bundle[:date] = Date.parse(each_row[0].text)
price_bundle[:open] = each_row[0].text
price_bundle[:high] = each_row[1].text
price_bundle[:low] = each_row[2].text
price_bundle[:close] = each_row[3].text
prices << price_bundle
rescue => error
next
end
end
prices
end

end
2 changes: 1 addition & 1 deletion lib/coinmarketcap/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Coinmarketcap
VERSION = "0.1.2"
VERSION = "0.2.0"
end
12 changes: 12 additions & 0 deletions spec/coinmarketcap_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,18 @@
expect(Coinmarketcap::VERSION).not_to be nil
end

describe "#get_historical_price" do
context 'with valid id and start and end dates' do
it "should receive an non empty array" do
VCR.use_cassette('historical_price_response') do
data = Coinmarketcap.get_historical_price('bitcoin', '20170908', '20170914')
expect(data).to be_a Array
expect(data.count).to be > 0
end
end
end
end

describe "#coins" do
context 'without limit' do
it "should receive a 200 response with all coins" do
Expand Down

0 comments on commit 73a7b92

Please sign in to comment.