blockchain 1.0.2 → 1.1.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,39 +1,39 @@
1
- require 'json'
2
- require_relative 'util'
3
-
4
- module Blockchain
5
-
6
- def self.create_wallet(password, api_code, priv: nil, label: nil, email: nil)
7
-
8
- params = { 'password' => password, 'api_code' => api_code }
9
-
10
- if !priv.nil?
11
- params['priv'] = priv
12
- end
13
- if !label.nil?
14
- params['label'] = label
15
- end
16
- if !email.nil?
17
- params['email'] = email
18
- end
19
-
20
- response = Blockchain::call_api('api/v2/create_wallet', method: 'post', data: params)
21
- json_response = JSON.parse(response)
22
- return CreateWalletResponse.new(json_response['guid'],
23
- json_response['address'],
24
- json_response['link'])
25
- end
26
-
27
- class CreateWalletResponse
28
- attr_reader :identifier
29
- attr_reader :address
30
- attr_reader :link
31
-
32
- def initialize(identifier, address, link)
33
- @identifier = identifier
34
- @address = address
35
- @link = link
36
- end
37
- end
38
-
1
+ require 'json'
2
+ require_relative 'util'
3
+
4
+ module Blockchain
5
+
6
+ def self.create_wallet(password, api_code, priv: nil, label: nil, email: nil)
7
+
8
+ params = { 'password' => password, 'api_code' => api_code }
9
+
10
+ if !priv.nil?
11
+ params['priv'] = priv
12
+ end
13
+ if !label.nil?
14
+ params['label'] = label
15
+ end
16
+ if !email.nil?
17
+ params['email'] = email
18
+ end
19
+
20
+ response = Blockchain::call_api('api/v2/create_wallet', method: 'post', data: params)
21
+ json_response = JSON.parse(response)
22
+ return CreateWalletResponse.new(json_response['guid'],
23
+ json_response['address'],
24
+ json_response['link'])
25
+ end
26
+
27
+ class CreateWalletResponse
28
+ attr_reader :identifier
29
+ attr_reader :address
30
+ attr_reader :link
31
+
32
+ def initialize(identifier, address, link)
33
+ @identifier = identifier
34
+ @address = address
35
+ @link = link
36
+ end
37
+ end
38
+
39
39
  end
@@ -1,47 +1,47 @@
1
- require 'json'
2
- require_relative 'util'
3
-
4
- module Blockchain
5
-
6
- class Currency
7
- attr_reader :last
8
- attr_reader :buy
9
- attr_reader :sell
10
- attr_reader :symbol
11
- attr_reader :p15min
12
-
13
- def initialize(last, buy, sell, symbol, p15min)
14
- @last = last
15
- @buy = buy
16
- @sell = sell
17
- @symbol = symbol
18
- @p15min = p15min
19
- end
20
- end
21
-
22
- def self.get_ticker(api_code = nil)
23
- params = {}
24
- if !api_code.nil? then params['api_code'] = api_code end
25
- response = Blockchain::call_api('ticker', data: params)
26
- json_response = JSON.parse(response)
27
-
28
- ticker = {}
29
- json_response.each do |key,value|
30
- json_ccy = json_response[key]
31
- ccy = Currency.new(json_ccy['last'],
32
- json_ccy['buy'],
33
- json_ccy['sell'],
34
- json_ccy['symbol'],
35
- json_ccy['15m'])
36
- ticker[key] = ccy
37
- end
38
- return ticker
39
- end
40
-
41
- def self.to_btc(ccy, value, api_code = nil)
42
- params = { 'currency' => ccy, 'value' => value}
43
- if !api_code.nil? then params['api_code'] = api_code end
44
- return Blockchain::call_api('tobtc', data: params).to_f
45
- end
46
-
1
+ require 'json'
2
+ require_relative 'util'
3
+
4
+ module Blockchain
5
+
6
+ class Currency
7
+ attr_reader :last
8
+ attr_reader :buy
9
+ attr_reader :sell
10
+ attr_reader :symbol
11
+ attr_reader :p15min
12
+
13
+ def initialize(last, buy, sell, symbol, p15min)
14
+ @last = last
15
+ @buy = buy
16
+ @sell = sell
17
+ @symbol = symbol
18
+ @p15min = p15min
19
+ end
20
+ end
21
+
22
+ def self.get_ticker(api_code = nil)
23
+ params = {}
24
+ if !api_code.nil? then params['api_code'] = api_code end
25
+ response = Blockchain::call_api('ticker', data: params)
26
+ json_response = JSON.parse(response)
27
+
28
+ ticker = {}
29
+ json_response.each do |key,value|
30
+ json_ccy = json_response[key]
31
+ ccy = Currency.new(json_ccy['last'],
32
+ json_ccy['buy'],
33
+ json_ccy['sell'],
34
+ json_ccy['symbol'],
35
+ json_ccy['15m'])
36
+ ticker[key] = ccy
37
+ end
38
+ return ticker
39
+ end
40
+
41
+ def self.to_btc(ccy, value, api_code = nil)
42
+ params = { 'currency' => ccy, 'value' => value}
43
+ if !api_code.nil? then params['api_code'] = api_code end
44
+ return Blockchain::call_api('tobtc', data: params).to_f
45
+ end
46
+
47
47
  end
@@ -1,16 +1,16 @@
1
- require 'json'
2
- require_relative 'util'
3
-
4
- module Blockchain
5
-
6
- def self.pushtx(tx, api_code = nil)
7
- params = { 'tx' => tx }
8
-
9
- if !api_code.nil?
10
- params['api_code'] = api_code
11
- end
12
-
13
- Blockchain::call_api('pushtx', method: 'post', data: params)
14
- end
15
-
1
+ require 'json'
2
+ require_relative 'util'
3
+
4
+ module Blockchain
5
+
6
+ def self.pushtx(tx, api_code = nil)
7
+ params = { 'tx' => tx }
8
+
9
+ if !api_code.nil?
10
+ params['api_code'] = api_code
11
+ end
12
+
13
+ Blockchain::call_api('pushtx', method: 'post', data: params)
14
+ end
15
+
16
16
  end
@@ -1,32 +1,86 @@
1
- require 'json'
2
- require_relative 'util'
3
-
4
- module Blockchain
5
-
6
- class ReceiveResponse
7
- attr_reader :fee_percent
8
- attr_reader :destination
9
- attr_reader :input_address
10
- attr_reader :callback_url
11
-
12
- def initialize(fee_percent, dest, input, callback)
13
- @fee_percent = fee_percent
14
- @destination = dest
15
- @input_address = input
16
- @callback_url = callback
17
- end
18
- end
19
-
20
- def self.receive(dest_addr, callback, api_code = nil)
21
- params = { 'method' => 'create', 'address' => dest_addr, 'callback' => callback }
22
- if !api_code.nil? then params['api_code'] = api_code end
23
- resp = Blockchain::call_api('api/receive', method: 'post', data: params)
24
- json_resp = JSON.parse(resp)
25
- receive_response = ReceiveResponse.new(json_resp['fee_percent'],
26
- json_resp['destination'],
27
- json_resp['input_address'],
28
- json_resp['callback_url'])
29
- return receive_response
30
- end
31
-
1
+ require 'json'
2
+
3
+ require_relative 'util'
4
+
5
+ module Blockchain
6
+
7
+ class ReceiveResponse
8
+ attr_reader :fee_percent
9
+ attr_reader :destination
10
+ attr_reader :input_address
11
+ attr_reader :callback_url
12
+
13
+ def initialize(fee_percent, dest, input, callback)
14
+ @fee_percent = fee_percent
15
+ @destination = dest
16
+ @input_address = input
17
+ @callback_url = callback
18
+ end
19
+ end
20
+
21
+ def self.receive(dest_addr, callback, api_code = nil)
22
+ params = { 'method' => 'create', 'address' => dest_addr, 'callback' => callback }
23
+ if !api_code.nil? then params['api_code'] = api_code end
24
+ resp = Blockchain::call_api('api/receive', method: 'post', data: params)
25
+ json_resp = JSON.parse(resp)
26
+ receive_response = ReceiveResponse.new(json_resp['fee_percent'],
27
+ json_resp['destination'],
28
+ json_resp['input_address'],
29
+ json_resp['callback_url'])
30
+ return receive_response
31
+ end
32
+
33
+ module V2
34
+ class ReceiveResponse
35
+ attr_reader :address
36
+ attr_reader :index
37
+ attr_reader :callback_url
38
+
39
+ def initialize(address, index, callback)
40
+ @address = address
41
+ @index = index
42
+ @callback_url = callback
43
+ end
44
+ end
45
+
46
+ class LogEntry
47
+ attr_reader :callback_url
48
+ attr_reader :called_at
49
+ attr_reader :raw_response
50
+ attr_reader :response_code
51
+
52
+ def initialize(callback_url, called_at, raw_response, response_code)
53
+ @callback_url = callback_url
54
+ @called_at = called_at
55
+ @raw_response = raw_response
56
+ @response_code = response_code
57
+ end
58
+ end
59
+
60
+ def self.receive(xpub, callback, api_key)
61
+ params = { 'xpub' => xpub, 'callback' => callback, 'key' => api_key }
62
+ resp = Blockchain::call_api('v2/receive', method: 'get', data: params, base_url: 'https://api.blockchain.info/')
63
+ json_resp = JSON.parse(resp)
64
+ receive_response = ReceiveResponse.new(json_resp['address'],
65
+ json_resp['index'],
66
+ json_resp['callback'])
67
+ receive_response
68
+ end
69
+
70
+ def self.callback_log(callback, api_key = nil)
71
+ params = {'callback' => callback }
72
+ params['key'] = api_key unless api_key.nil?
73
+ resp = Blockchain::call_api('v2/receive/callback_log', method: 'get', data: params, base_url: 'https://api.blockchain.info/')
74
+ json_resp = JSON.parse(resp)
75
+ receive_response = json_resp.map do |entry|
76
+ LogEntry.new(entry['callback'],
77
+ entry['called_at'],
78
+ entry['raw_response'],
79
+ entry['response_code'])
80
+ end
81
+ receive_response
82
+ end
83
+ end
84
+
85
+
32
86
  end
@@ -1,60 +1,60 @@
1
- require 'json'
2
- require_relative 'util'
3
-
4
- module Blockchain
5
-
6
- def self.get_statistics(api_code = nil)
7
- params = { 'format' => 'json' }
8
- if !api_code.nil? then params['api_code'] = api_code end
9
- response = Blockchain::call_api('stats', method: 'get', data: params)
10
- json_response = JSON.parse(response)
11
- return StatisticsResponse.new(json_response)
12
- end
13
-
14
- class StatisticsResponse
15
- attr_reader :trade_volume_btc
16
- attr_reader :miners_revenue_usd
17
- attr_reader :btc_mined
18
- attr_reader :trade_volume_usd
19
- attr_reader :difficulty
20
- attr_reader :minutes_between_blocks
21
- attr_reader :number_of_transactions
22
- attr_reader :hash_rate
23
- attr_reader :timestamp
24
- attr_reader :mined_blocks
25
- attr_reader :blocks_size
26
- attr_reader :total_fees_btc
27
- attr_reader :total_btc_sent
28
- attr_reader :estimated_btc_sent
29
- attr_reader :total_btc
30
- attr_reader :total_blocks
31
- attr_reader :next_retarget
32
- attr_reader :estimated_transaction_volume_usd
33
- attr_reader :miners_revenue_btc
34
- attr_reader :market_price_usd
35
-
36
- def initialize(s)
37
- @trade_volume_btc = s['trade_volume_btc']
38
- @miners_revenue_usd = s['miners_revenue_usd']
39
- @btc_mined = s['n_btc_mined']
40
- @trade_volume_usd = s['trade_volume_usd']
41
- @difficulty = s['difficulty']
42
- @minutes_between_blocks = s['minutes_between_blocks']
43
- @number_of_transactions = s['n_tx']
44
- @hash_rate = s['hash_rate']
45
- @timestamp = s['timestamp']
46
- @mined_blocks = s['n_blocks_mined']
47
- @blocks_size = s['blocks_size']
48
- @total_fees_btc = s['total_fees_btc']
49
- @total_btc_sent = s['total_btc_sent']
50
- @estimated_btc_sent = s['estimated_btc_sent']
51
- @total_btc = s['totalbc']
52
- @total_blocks = s['n_blocks_total']
53
- @next_retarget = s['nextretarget']
54
- @estimated_transaction_volume_usd = s['estimated_transaction_volume_usd']
55
- @miners_revenue_btc = s['miners_revenue_btc']
56
- @market_price_usd = s['market_price_usd']
57
- end
58
- end
59
-
1
+ require 'json'
2
+ require_relative 'util'
3
+
4
+ module Blockchain
5
+
6
+ def self.get_statistics(api_code = nil)
7
+ params = { 'format' => 'json' }
8
+ if !api_code.nil? then params['api_code'] = api_code end
9
+ response = Blockchain::call_api('stats', method: 'get', data: params)
10
+ json_response = JSON.parse(response)
11
+ return StatisticsResponse.new(json_response)
12
+ end
13
+
14
+ class StatisticsResponse
15
+ attr_reader :trade_volume_btc
16
+ attr_reader :miners_revenue_usd
17
+ attr_reader :btc_mined
18
+ attr_reader :trade_volume_usd
19
+ attr_reader :difficulty
20
+ attr_reader :minutes_between_blocks
21
+ attr_reader :number_of_transactions
22
+ attr_reader :hash_rate
23
+ attr_reader :timestamp
24
+ attr_reader :mined_blocks
25
+ attr_reader :blocks_size
26
+ attr_reader :total_fees_btc
27
+ attr_reader :total_btc_sent
28
+ attr_reader :estimated_btc_sent
29
+ attr_reader :total_btc
30
+ attr_reader :total_blocks
31
+ attr_reader :next_retarget
32
+ attr_reader :estimated_transaction_volume_usd
33
+ attr_reader :miners_revenue_btc
34
+ attr_reader :market_price_usd
35
+
36
+ def initialize(s)
37
+ @trade_volume_btc = s['trade_volume_btc']
38
+ @miners_revenue_usd = s['miners_revenue_usd']
39
+ @btc_mined = s['n_btc_mined']
40
+ @trade_volume_usd = s['trade_volume_usd']
41
+ @difficulty = s['difficulty']
42
+ @minutes_between_blocks = s['minutes_between_blocks']
43
+ @number_of_transactions = s['n_tx']
44
+ @hash_rate = s['hash_rate']
45
+ @timestamp = s['timestamp']
46
+ @mined_blocks = s['n_blocks_mined']
47
+ @blocks_size = s['blocks_size']
48
+ @total_fees_btc = s['total_fees_btc']
49
+ @total_btc_sent = s['total_btc_sent']
50
+ @estimated_btc_sent = s['estimated_btc_sent']
51
+ @total_btc = s['totalbc']
52
+ @total_blocks = s['n_blocks_total']
53
+ @next_retarget = s['nextretarget']
54
+ @estimated_transaction_volume_usd = s['estimated_transaction_volume_usd']
55
+ @miners_revenue_btc = s['miners_revenue_btc']
56
+ @market_price_usd = s['market_price_usd']
57
+ end
58
+ end
59
+
60
60
  end
@@ -1,34 +1,35 @@
1
- require 'net/http'
2
-
3
- module Blockchain
4
-
5
- class APIException < StandardError
6
- end
7
-
8
- BASE_URL = "https://blockchain.info/"
9
- TIMEOUT_SECONDS = 10
10
-
11
- def self.call_api(resource, method: 'get', data: nil)
12
- url = URI.parse(BASE_URL + resource)
13
- http = Net::HTTP.new(url.host, url.port)
14
- http.use_ssl = true
15
- http.read_timeout = TIMEOUT_SECONDS
16
-
17
- request = nil
18
- if method == 'get'
19
- url.query = data.nil? ? nil : URI.encode_www_form(data)
20
- request = Net::HTTP::Get.new(url.request_uri)
21
- elsif method == 'post'
22
- request = Net::HTTP::Post.new(url.request_uri)
23
- request.content_type = 'application/x-www-form-urlencoded'
24
- if data != nil then request.set_form_data(data) else {} end
25
- end
26
-
27
- response = http.request(request)
28
- if response.code != '200'
29
- raise APIException, response.body
30
- end
31
- return response.body
32
- end
33
-
1
+ require 'net/http'
2
+
3
+ module Blockchain
4
+
5
+ class APIException < StandardError
6
+ end
7
+
8
+ BASE_URL = "https://blockchain.info/"
9
+ TIMEOUT_SECONDS = 10
10
+
11
+ def self.call_api(resource, method: 'get', data: nil, base_url: nil)
12
+ base_url ||= BASE_URL
13
+ url = URI.parse(base_url + resource)
14
+ http = Net::HTTP.new(url.host, url.port)
15
+ http.use_ssl = true
16
+ http.read_timeout = TIMEOUT_SECONDS
17
+
18
+ request = nil
19
+ if method == 'get'
20
+ url.query = data.nil? ? nil : URI.encode_www_form(data)
21
+ request = Net::HTTP::Get.new(url.request_uri)
22
+ elsif method == 'post'
23
+ request = Net::HTTP::Post.new(url.request_uri)
24
+ request.content_type = 'application/x-www-form-urlencoded'
25
+ if data != nil then request.set_form_data(data) else {} end
26
+ end
27
+
28
+ response = http.request(request)
29
+ if response.code != '200'
30
+ raise APIException, response.body
31
+ end
32
+ return response.body
33
+ end
34
+
34
35
  end