blockcypher-client 0.0.1 → 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 3b261213992eff9f18aff58b6a330da9e83fb261
4
- data.tar.gz: 7dbdbe0c9da75177f2769228a8a2991a720f0126
3
+ metadata.gz: f2a29f73f96f2b248dafb7fc35e2d880bbcf06aa
4
+ data.tar.gz: 2c1be189f0c326921bc641239d13e4bb277f736d
5
5
  SHA512:
6
- metadata.gz: 5a091672a0cb2fe2d449de35207f5ecfed8f875e4443e63d822a7a85bb083d7f85a2d2caf62b81368022a20d15f02551697fb2c98593b70d5f11364884b61e44
7
- data.tar.gz: 5162dc47d7177219e5993177f4ca48c1f9bf21e63b294eca1fd1bed1c6a8b6523b1a24cbb21a98bebf304145ed2a6f734a80f55ef1c5f22cf13f25f4c2c15b98
6
+ metadata.gz: f71139fc93454387d7760e6336c6f7be33718226da92abeb9b051dc36e0b5cca2a084262d03d4e7fa0bb3db8fc6e7ab84112a5db757fe6478f7bc0ab51545d21
7
+ data.tar.gz: 46aa746932879cfaaec25b416f65c679be48abff9f0b8c678093ec76ed422450c6f03078074868df030f10bec717ffe5fca530b9e2fbc5c3b5b8e95ba04fdeb1
data/README.md CHANGED
@@ -1,29 +1,31 @@
1
1
  # Blockcypher::Client
2
2
 
3
- TODO: Write a gem description
3
+ Allow usage of the [Blockcypher API](http://dev.blockcypher.com/reference.html) through Ruby
4
4
 
5
5
  ## Installation
6
6
 
7
- Add this line to your application's Gemfile:
8
-
9
- gem 'blockcypher-client'
7
+ $ gem install blockcypher-client
10
8
 
11
- And then execute:
9
+ ## Testing
12
10
 
13
- $ bundle
11
+ At the moment integration tests are hitting the live chains
14
12
 
15
- Or install it yourself as:
16
-
17
- $ gem install blockcypher-client
13
+ $ rake
18
14
 
19
15
  ## Usage
20
16
 
21
- TODO: Write usage instructions here
17
+ ```ruby
18
+ # Creates new default client (coin = btc, chain = main)
19
+ client = Blockcypher::Client.new
20
+ json = client.chain # Return JSON info for the current chain
21
+ json['name'] # => 'BTC.main'
22
+
23
+ litecoin = Blockcypher::Client.new(coin: :ltc)
24
+ json = litecoin.transactions('4e6b95993b770e7d1084af128f971d6b44c5e32cbf3acc35eee84f69c6b4f9ea')
22
25
 
23
- ## Contributing
26
+ # Client for bitcoin on the test3 blockchain
27
+ bitcoin_test = Blockcypher::Client.new(chain: :test3)
24
28
 
25
- 1. Fork it ( http://github.com/<my-github-username>/blockcypher-client/fork )
26
- 2. Create your feature branch (`git checkout -b my-new-feature`)
27
- 3. Commit your changes (`git commit -am 'Add some feature'`)
28
- 4. Push to the branch (`git push origin my-new-feature`)
29
- 5. Create new Pull Request
29
+ # Client with API token
30
+ client = Blockcypher::Client.new(token: 'my_token')
31
+ ```
@@ -10,7 +10,7 @@ Gem::Specification.new do |spec|
10
10
  spec.email = ["simcap@fastmail.com"]
11
11
  spec.summary = %q{Basic Ruby wrapper to interact with the Blockcypher API}
12
12
  spec.description = %q{Basic Ruby wrapper to interact with the Blockcypher API}
13
- spec.homepage = ""
13
+ spec.homepage = "https://github.com/simcap/blockcypher-client"
14
14
  spec.license = "MIT"
15
15
 
16
16
  spec.files = `git ls-files -z`.split("\x0")
@@ -1,58 +1,26 @@
1
1
  require 'blockcypher/client/version'
2
+ require 'blockcypher/client/api'
2
3
 
3
4
  require 'httpclient'
4
5
  require 'json'
5
6
 
6
7
  module Blockcypher
7
- module Api
8
-
9
- def transactions(txs_hash)
10
- Call.new(base_uri).get("/txs/#{txs_hash}")
11
- end
12
- alias_method :txs, :transactions
13
-
14
- def blocks(block_hash)
15
- Call.new(base_uri).get("/blocks/#{block_hash}")
16
- end
17
-
18
- def address(add)
19
- Call.new(base_uri).get("/addrs/#{add}")
20
- end
21
-
22
- def current
23
- Call.new(base_uri).get
24
- end
25
- alias_method :status, :current
26
-
27
- class Call
28
-
29
- def initialize(base_uri)
30
- @http = HTTPClient.new(base_url: base_uri.to_s)
31
- end
32
-
33
- def get(path = '')
34
- parse_response(@http.get_content(path))
35
- end
36
-
37
- private
38
-
39
- def parse_response(response)
40
- JSON.parse(response)
41
- end
42
- end
43
- end
44
-
45
8
  class Client
46
9
  include Api
47
10
 
48
- attr_reader :base_uri
11
+ attr_reader :config
49
12
 
50
13
  def initialize(options = {})
51
- @chain = options[:chain] || :main
52
- @coin = options[:coin] || :btc
53
- @base_uri = URI("https://api.blockcypher.com/v1/#{@coin}/#{@chain}")
14
+ chain = options[:chain] || :main
15
+ coin = options[:coin] || :btc
16
+ @config = Config.new(
17
+ URI("https://api.blockcypher.com/v1/#{coin}/#{chain}"),
18
+ options[:token]
19
+ )
54
20
  end
55
-
56
21
  end
57
22
 
23
+ Config = Struct.new(:base_uri, :token) do
24
+ undef []=, base_uri=, token=
25
+ end
58
26
  end
@@ -0,0 +1,68 @@
1
+ module Blockcypher
2
+ class Client
3
+ module Api
4
+ def transactions(txs_hash)
5
+ new_call.get("/txs/#{txs_hash}")
6
+ end
7
+ alias_method :txs, :transactions
8
+
9
+ def blocks(block_hash)
10
+ new_call.get("/blocks/#{block_hash}")
11
+ end
12
+
13
+ def address(add)
14
+ new_call.get("/addrs/#{add}")
15
+ end
16
+
17
+ def create_address
18
+ new_call.post('/addrs')
19
+ end
20
+
21
+ def chain
22
+ new_call.get
23
+ end
24
+
25
+ private
26
+
27
+ def new_call
28
+ Call.new(config)
29
+ end
30
+
31
+ class Call
32
+ def initialize(config)
33
+ @config = config
34
+ @http = HTTPClient.new(
35
+ base_url: @config.base_uri.to_s,
36
+ default_header: { Accept: 'application/json' }
37
+ )
38
+ end
39
+
40
+ def post(path, options = {})
41
+ perform(:post, path, options)
42
+ end
43
+
44
+ def get(path = '')
45
+ perform(:get, path)
46
+ end
47
+
48
+ private
49
+
50
+ def perform(verb, path, options = {})
51
+ options.merge!(query: params)
52
+ response = @http.public_send("#{verb.to_s}_content", path, options)
53
+ parse_json(response)
54
+ rescue HTTPClient::BadResponseError => e
55
+ raise "Unexpected http status #{e.res.status} calling '#{e.res.header.request_uri.to_s}'"
56
+ end
57
+
58
+ def params
59
+ @config.token ? { token: @config.token } : {}
60
+ end
61
+
62
+ def parse_json(response)
63
+ JSON.parse(response)
64
+ end
65
+ end
66
+ end
67
+ end
68
+ end
@@ -1,5 +1,5 @@
1
1
  module Blockcypher
2
2
  class Client
3
- VERSION = "0.0.1"
3
+ VERSION = "0.0.2"
4
4
  end
5
5
  end
@@ -11,4 +11,12 @@ class AddressApiTest < MiniTest::Test
11
11
  assert_equal add, json['address']
12
12
  end
13
13
 
14
+ def test_generate_address
15
+ client = Blockcypher::Client.new
16
+ json = client.create_address
17
+ refute_nil json['private']
18
+ refute_nil json['public']
19
+ refute_nil json['address']
20
+ end
21
+
14
22
  end
@@ -0,0 +1,21 @@
1
+ require 'minitest_helper'
2
+
3
+ class CurrentChainApiTest < MiniTest::Test
4
+
5
+ def test_current_chain_for_different_chains
6
+ client = Blockcypher::Client.new
7
+ assert_equal 'BTC.main', client.chain['name']
8
+
9
+ client = Blockcypher::Client.new(chain: :test3)
10
+ assert_equal 'BTC.test3', client.chain['name']
11
+ end
12
+
13
+ def test_current_chain_for_different_coins
14
+ client = Blockcypher::Client.new(coin: :ltc)
15
+ assert_equal 'LTC.main', client.chain['name']
16
+
17
+ client = Blockcypher::Client.new(coin: :uro)
18
+ assert_equal 'URO.main', client.chain['name']
19
+ end
20
+
21
+ end
@@ -0,0 +1,13 @@
1
+ require 'minitest_helper'
2
+
3
+ class ErrorsApiTest < MiniTest::Test
4
+
5
+ def test_404_not_found
6
+ client = Blockcypher::Client.new
7
+ assert_raises RuntimeError do |error|
8
+ json = client.blocks('')
9
+ assert_match /404/, error.message
10
+ end
11
+ end
12
+
13
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: blockcypher-client
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - simcap
@@ -89,7 +89,6 @@ extensions: []
89
89
  extra_rdoc_files: []
90
90
  files:
91
91
  - ".gitignore"
92
- - ".travis.yml"
93
92
  - Gemfile
94
93
  - LICENSE.txt
95
94
  - README.md
@@ -97,14 +96,16 @@ files:
97
96
  - bin/blockcypher-client
98
97
  - blockcypher-client.gemspec
99
98
  - lib/blockcypher/client.rb
99
+ - lib/blockcypher/client/api.rb
100
100
  - lib/blockcypher/client/version.rb
101
101
  - test/integration/address_api_test.rb
102
102
  - test/integration/blocks_api_test.rb
103
- - test/integration/current_api_test.rb
103
+ - test/integration/current_chain_api_test.rb
104
+ - test/integration/errors_test.rb
104
105
  - test/integration/transactions_api_test.rb
105
106
  - test/minitest_helper.rb
106
107
  - test/test_blockcypher/client.rb
107
- homepage: ''
108
+ homepage: https://github.com/simcap/blockcypher-client
108
109
  licenses:
109
110
  - MIT
110
111
  metadata: {}
@@ -131,7 +132,8 @@ summary: Basic Ruby wrapper to interact with the Blockcypher API
131
132
  test_files:
132
133
  - test/integration/address_api_test.rb
133
134
  - test/integration/blocks_api_test.rb
134
- - test/integration/current_api_test.rb
135
+ - test/integration/current_chain_api_test.rb
136
+ - test/integration/errors_test.rb
135
137
  - test/integration/transactions_api_test.rb
136
138
  - test/minitest_helper.rb
137
139
  - test/test_blockcypher/client.rb
@@ -1,3 +0,0 @@
1
- language: ruby
2
- rvm:
3
- - 2.0.0
@@ -1,21 +0,0 @@
1
- require 'minitest_helper'
2
-
3
- class CurrentApiTest < MiniTest::Test
4
-
5
- def test_current_status_for_different_chains
6
- client = Blockcypher::Client.new
7
- assert_equal 'BTC.main', client.current['name']
8
-
9
- client = Blockcypher::Client.new(chain: :test3)
10
- assert_equal 'BTC.test3', client.current['name']
11
- end
12
-
13
- def test_current_status_for_different_coins
14
- client = Blockcypher::Client.new(coin: :ltc)
15
- assert_equal 'LTC.main', client.current['name']
16
-
17
- client = Blockcypher::Client.new(coin: :uro)
18
- assert_equal 'URO.main', client.current['name']
19
- end
20
-
21
- end