Class: Middlecoin::Core::MiddlecoinAPI

Inherits:
Object
  • Object
show all
Defined in:
lib/middlecoin/core/middlecoinapi.rb

Overview

Bitcoin Address

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#reportObject

Returns the value of attribute report.



31
32
33
# File 'lib/middlecoin/core/middlecoinapi.rb', line 31

def report
  @report
end

Instance Method Details

#fetch(uri_str, limit = 10) ⇒ Object

Simple function for following redirects, found it on stackexchange

Raises:

  • (ArgumentError)


34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/middlecoin/core/middlecoinapi.rb', line 34

def fetch(uri_str, limit = 10)
  raise ArgumentError, 'HTTP redirect too deep' if limit == 0

  url = URI.parse(uri_str)
  req = Net::HTTP::Get.new(url.path, { 'User-Agent' => "Agent" })
  
  response = Net::HTTP.start(url.host, url.port) do |http| 
  	http.request(req) 
  end

  case response
  	when Net::HTTPSuccess
  		then response
  	when Net::HTTPRedirection 
  		then fetch(response['location'], limit - 1)
  	else
    	response.error!
  end
end

#fetch_reportObject

Downloads and caches the report JSON from middlecoin.com



55
56
57
58
59
60
61
62
# File 'lib/middlecoin/core/middlecoinapi.rb', line 55

def fetch_report
	begin
		report = fetch("#{Middlecoin::MIDDLECOIN_URL}/json")
		@report = JSON.parse(report.body)["report"]
	rescue => e
		raise Middlecoin::MiddlecoinAPIError, "Unable to collect JSON report from middlecoin.com"
	end
end

#lookup(btc_address) ⇒ Object

Looks up a specific btc address in the JSON report from middlecoin.com



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/middlecoin/core/middlecoinapi.rb', line 66

def lookup btc_address
	@report.each do |address|
		if address[0].eql?(btc_address)
			result = {
				:address => address[0],
				:lastHourShares => address[1]["lastHourShares"],
				:immatureBalance => address[1]["immatureBalance"],
				:lastHourRejectedShares => address[1]["lastHourRejectedShares"],
				:paidOut => address[1]["paidOut"],
				:unexchangedBalance => address[1]["unexchangedBalance"],
				:megahashesPerSecond => address[1]["megahashesPerSecond"],
				:bitcoinBalance => address[1]["bitcoinBalance"],
				:rejectedMegahashesPerSecond => address[1]["rejectedMegahashesPerSecond"]
			}

			return result
		end
	end

	return nil
end