helloblock 0.0.1

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.
Files changed (46) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +17 -0
  3. data/Gemfile +4 -0
  4. data/LICENSE.txt +22 -0
  5. data/README.md +56 -0
  6. data/Rakefile +1 -0
  7. data/helloblock.gemspec +32 -0
  8. data/lib/helloblock.rb +20 -0
  9. data/lib/helloblock/api_interface/api_parameters.rb +10 -0
  10. data/lib/helloblock/api_interface/endpoints.rb +23 -0
  11. data/lib/helloblock/configuration.rb +22 -0
  12. data/lib/helloblock/http/connection.rb +28 -0
  13. data/lib/helloblock/http/request.rb +26 -0
  14. data/lib/helloblock/resources/address.rb +15 -0
  15. data/lib/helloblock/resources/block.rb +7 -0
  16. data/lib/helloblock/resources/faucet.rb +17 -0
  17. data/lib/helloblock/resources/query.rb +74 -0
  18. data/lib/helloblock/resources/transaction.rb +15 -0
  19. data/lib/helloblock/resources/wallet.rb +7 -0
  20. data/lib/helloblock/spec_helper.rb +11 -0
  21. data/lib/helloblock/utils.rb +17 -0
  22. data/lib/helloblock/version.rb +3 -0
  23. data/spec/fixture/vcr_cassettes/batch_address_unspents.yml +49 -0
  24. data/spec/fixture/vcr_cassettes/batch_addresses.yml +47 -0
  25. data/spec/fixture/vcr_cassettes/batch_transactions.yml +47 -0
  26. data/spec/fixture/vcr_cassettes/batch_transactions_addresses.yml +49 -0
  27. data/spec/fixture/vcr_cassettes/batch_unspents.yml +49 -0
  28. data/spec/fixture/vcr_cassettes/faucet.yml +45 -0
  29. data/spec/fixture/vcr_cassettes/latest_blocks.yml +47 -0
  30. data/spec/fixture/vcr_cassettes/latest_transactions.yml +47 -0
  31. data/spec/fixture/vcr_cassettes/propagate.yml +49 -0
  32. data/spec/fixture/vcr_cassettes/single_address.yml +47 -0
  33. data/spec/fixture/vcr_cassettes/single_address_unspents.yml +49 -0
  34. data/spec/fixture/vcr_cassettes/single_block.yml +47 -0
  35. data/spec/fixture/vcr_cassettes/single_transaction.yml +47 -0
  36. data/spec/fixture/vcr_cassettes/wallet.yml +47 -0
  37. data/spec/fixture/vcr_cassettes/withdrawal.yml +45 -0
  38. data/spec/integration/api_calls_work_spec.rb +176 -0
  39. data/spec/lib/address_spec.rb +77 -0
  40. data/spec/lib/connection_spec.rb +28 -0
  41. data/spec/lib/faucet_spec.rb +30 -0
  42. data/spec/lib/helloblock_spec.rb +51 -0
  43. data/spec/lib/request_spec.rb +41 -0
  44. data/spec/lib/transaction_spec.rb +106 -0
  45. data/spec/spec_helper.rb +13 -0
  46. metadata +251 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: d5705b196e7b8ac0e977d9958c959f8126f21fd5
4
+ data.tar.gz: a16a2f4bcc24a6cb4f42a2cacb0dfa6875987526
5
+ SHA512:
6
+ metadata.gz: 5df3bdc542e11603862ca74da25cebec116923d61b8d5cd0c365047fb0e63768f7204290a568351797b514048a797d9349589fe4371d9e3c0e4637749a9b27fb
7
+ data.tar.gz: 970ea0b35c990cddeb516cca21ee03946a84feaff1ba02228dffb3e6f8e903950e88de61924fc049f5d7bd3ee25bfbd35afd3f374b3bdde4ba48bea2137665b9
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in helloblock.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Nathaniel Wroblewski
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,56 @@
1
+ # HelloBlock
2
+
3
+ Fluent ruby wrapper for the [HelloBlock](http://www.helloblock.io) API. For a simple, non-fluent wrapper that avoids the Singleton pattern, see [helloblock-ruby](http://github.com/nathanielwroblewski/helloblock-ruby).
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'helloblock'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install helloblock
18
+
19
+ ## Usage
20
+
21
+ Configure your HelloBlock session:
22
+
23
+ ```rb
24
+ HelloBlock.configure do |config|
25
+ config.api_key = ENV['APIKEY'] # your api key goes here
26
+ config.version = :v1 # defaults to latest version
27
+ config.network = :mainnet # defaults to testnet
28
+ end
29
+ ```
30
+
31
+ Query the HelloBlock API with familiar [ActiveRecord](https://github.com/rails/rails/tree/master/activerecord)-like syntax. NOTE: As of the current version, `#to_hash` must be called on each completed query as a kicker method.
32
+
33
+ Description | Method
34
+ ------------- | ------------- |
35
+ Fetch a single address | `HelloBlock::Address.find('1DQN9nop...')`|
36
+ Fetch batch addresses | `HelloBlock::Address.where(address: ['1DQN9nop...', ...])` |
37
+ Fetch unspents for address | `HelloBlock::Address.find('1DQN9nop...').unspents` |
38
+ Fetch unspents for addresses | `HelloBlock::Address.unspents.where(address: ['1DQN9nop...', ...])` |
39
+ Fetch a single transaction | `HelloBlock::Transaction.find('f37e6181...')` |
40
+ Fetch batch transactions | `HelloBlock::Transaction.where(transaction: ['f37e6181...', ...])` |
41
+ Fetch transactions by addresses | `HelloBlock::Transaction.where(address: ['1DQN9nop...', ...])` |
42
+ Fetch latest transactions | `HelloBlock::Transaction.last(5).offset(7)` |
43
+ Propagate a transaction | `HelloBlock::Transaction.create('01000...')` |
44
+ Fetch a block | `HelloBlock::Block.find('00000...')` |
45
+ Fetch latest block | `HelloBlock::Block.last(1)` |
46
+ Fetch wallet information | `HelloBlock::Wallet.where(addresses: [...], unspents: false)` |
47
+ Fetch unspents from faucet | `HelloBlock::Faucet.where(type: 3)` |
48
+ Withdraw from faucet | `HelloBlock::Faucet.withdraw(to: '1DQN9nop...', amount: 100_000)` |
49
+
50
+ ## Contributing
51
+
52
+ 1. Fork it
53
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
54
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
55
+ 4. Push to the branch (`git push origin my-new-feature`)
56
+ 5. Create new Pull Request
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,32 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'helloblock/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "helloblock"
8
+ spec.version = HelloBlock::VERSION
9
+ spec.authors = ["Nathaniel Wroblewski"]
10
+ spec.email = ["nathanielwroblewski@gmail.com"]
11
+ spec.description = %q{Fluent Ruby wrapper for the helloblock.io API}
12
+ spec.summary = %q{Fluent Ruby wrapper for the helloblock.io API}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency 'bundler', '~> 1.3'
22
+ spec.add_development_dependency 'rake'
23
+ spec.add_development_dependency 'rspec', '~> 2.14'
24
+ spec.add_development_dependency 'webmock', '~> 1.17'
25
+ spec.add_development_dependency 'vcr', '~> 2.8'
26
+ spec.add_development_dependency 'guard-rspec', '~> 4.2'
27
+
28
+ spec.add_runtime_dependency 'multi_json', '~> 1.9'
29
+ spec.add_runtime_dependency 'yajl-ruby', '~> 1.2'
30
+ spec.add_runtime_dependency 'faraday_middleware', '~> 0.9'
31
+ spec.add_runtime_dependency 'rash', '~> 0.4'
32
+ end
@@ -0,0 +1,20 @@
1
+ require 'helloblock/version'
2
+ require 'helloblock/api_interface/endpoints'
3
+ require 'helloblock/resources/query'
4
+ require 'helloblock/resources/address'
5
+ require 'helloblock/resources/transaction'
6
+ require 'helloblock/resources/block'
7
+ require 'helloblock/resources/wallet'
8
+ require 'helloblock/resources/faucet'
9
+ require 'helloblock/http/connection'
10
+ require 'helloblock/http/request'
11
+ require 'helloblock/configuration'
12
+ require 'helloblock/spec_helper'
13
+
14
+ module HelloBlock
15
+ extend HelloBlock::Endpoints
16
+ extend HelloBlock::Request
17
+ extend HelloBlock::Connection
18
+ extend HelloBlock::Configuration
19
+ extend HelloBlock::SpecHelper
20
+ end
@@ -0,0 +1,10 @@
1
+ module HelloBlock
2
+ module APIParameters
3
+ API_PARAMETERS = {
4
+ address: :addresses,
5
+ transaction: :txHashes,
6
+ propagate: :rawTxHex,
7
+ to: :toAddress
8
+ }
9
+ end
10
+ end
@@ -0,0 +1,23 @@
1
+ module HelloBlock
2
+ module Endpoints
3
+ ENDPOINTS = {
4
+ address: '/addresses/',
5
+ addresses_transactions: '/addresses/transactions',
6
+ unspents: '/unspents',
7
+ transaction: '/transactions/',
8
+ latest: 'latest',
9
+ wallet: '/wallet',
10
+ block: '/blocks/',
11
+ faucet: '/faucet',
12
+ withdraw: '/faucet/withdrawal'
13
+ }
14
+
15
+ def base_url
16
+ "http://#{network}.helloblock.io"
17
+ end
18
+
19
+ def version_path
20
+ "/#{version}"
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,22 @@
1
+ require 'helloblock/utils'
2
+
3
+ module HelloBlock
4
+ module Configuration
5
+
6
+ extend HelloBlock::Utils
7
+ mattr_accessor :api_key, :network, :version
8
+
9
+
10
+ def network
11
+ @network ||= :testnet
12
+ end
13
+
14
+ def version
15
+ @version ||= :v1
16
+ end
17
+
18
+ def configure
19
+ yield self if block_given?
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,28 @@
1
+ require 'faraday_middleware'
2
+
3
+ module HelloBlock
4
+ module Connection
5
+ def connection
6
+ @connection ||= Faraday.new(base_url, connection_options) do |connection|
7
+ connection.request :json
8
+ connection.response :json
9
+ connection.use FaradayMiddleware::Rashify
10
+
11
+ connection.adapter :net_http
12
+ end
13
+ end
14
+
15
+ def connection_options
16
+ @connection_options ||= {
17
+ headers: {
18
+ accept: 'application/json',
19
+ user_agent: "HelloBlock Gem #{HelloBlock::VERSION}"
20
+ },
21
+ request: {
22
+ open_timeout: 10,
23
+ timeout: 30
24
+ }
25
+ }
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,26 @@
1
+ require 'helloblock/api_interface/endpoints'
2
+
3
+ module HelloBlock
4
+ module Request
5
+
6
+ include HelloBlock::Endpoints
7
+
8
+ def get(path, params={})
9
+ request(:get, version_path + path, params, headers)
10
+ end
11
+
12
+ def post(path, params={})
13
+ request(:post, version_path + path, { body: params }, headers)
14
+ end
15
+
16
+ private
17
+
18
+ def request(method, path, params={}, headers={})
19
+ connection.send(method.to_sym, path, params, headers).body
20
+ end
21
+
22
+ def headers
23
+ { accept: '*/*', content_type: 'application/json; charset=UTF-8' }
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,15 @@
1
+ require 'helloblock/resources/query'
2
+ require 'helloblock/api_interface/endpoints'
3
+
4
+ module HelloBlock
5
+ class Address
6
+ extend HelloBlock::Query
7
+ include HelloBlock::Endpoints
8
+
9
+ def self.unspents
10
+ query[:path] += ENDPOINTS[:unspents]
11
+ query[:path].squeeze!('/')
12
+ self
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,7 @@
1
+ require 'helloblock/resources/query'
2
+
3
+ module HelloBlock
4
+ class Block
5
+ extend HelloBlock::Query
6
+ end
7
+ end
@@ -0,0 +1,17 @@
1
+ require 'helloblock/resources/query'
2
+ require 'helloblock/api_interface/endpoints'
3
+ require 'helloblock/api_interface/api_parameters'
4
+
5
+ module HelloBlock
6
+ class Faucet
7
+ extend HelloBlock::Query
8
+ include HelloBlock::Endpoints
9
+ include HelloBlock::APIParameters
10
+
11
+ def self.withdraw(conditions)
12
+ where(conditions.merge({post: true}))
13
+ query[:path] = ENDPOINTS[:withdraw]
14
+ self
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,74 @@
1
+ require 'helloblock/api_interface/endpoints'
2
+ require 'helloblock/api_interface/api_parameters'
3
+
4
+ module HelloBlock
5
+ module Query
6
+ include HelloBlock::Endpoints
7
+ include HelloBlock::APIParameters
8
+
9
+ def query
10
+ @query ||= default_query
11
+ end
12
+
13
+ def default_query
14
+ { path: ENDPOINTS[parent_class], params: {} }
15
+ end
16
+
17
+ def parent_class
18
+ self.to_s.split('::').last.downcase.to_sym
19
+ end
20
+
21
+ def find(id)
22
+ query[:path] += id
23
+ self
24
+ end
25
+
26
+ # where(transaction: [...]) => converts :transaction to API's :txHashes
27
+ def where(conditions)
28
+ conditions.each do |resource, ids|
29
+ api_resource = API_PARAMETERS[resource] || resource
30
+ query[:params][api_resource] = ids
31
+ end
32
+ determine_parent_resource
33
+ self
34
+ end
35
+
36
+ def last(limit)
37
+ query[:path] = ENDPOINTS[parent_class] + ENDPOINTS[:latest]
38
+ query[:params][:limit] = limit
39
+ self
40
+ end
41
+
42
+ def offset(number)
43
+ query[:params][:offset] = number
44
+ self
45
+ end
46
+
47
+ def to_hash
48
+ (query_copy = query.clone) and (@query = default_query)
49
+ method = query_copy[:params][:post] ? :post : :get
50
+ query_copy[:params].delete(:post)
51
+ HelloBlock.send(method, query_copy[:path], query_copy[:params])
52
+ end
53
+
54
+ def [](attribute)
55
+ to_hash[attribute]
56
+ end
57
+
58
+ def []=(attribute, value)
59
+ to_hash[attribute] = value
60
+ end
61
+
62
+ def inspect
63
+ to_hash.to_s
64
+ end
65
+
66
+ # exceptions: querying transactions with addresses actually hits
67
+ # /addresses/transactions endpoint
68
+ def determine_parent_resource
69
+ if query[:path] == ENDPOINTS[:transaction] && query[:params][:addresses]
70
+ query[:path] = ENDPOINTS[:addresses_transactions]
71
+ end
72
+ end
73
+ end
74
+ end
@@ -0,0 +1,15 @@
1
+ require 'helloblock/resources/query'
2
+ require 'helloblock/api_interface/api_parameters'
3
+
4
+ module HelloBlock
5
+ class Transaction
6
+ extend HelloBlock::Query
7
+ include HelloBlock::APIParameters
8
+
9
+ def self.create(raw_tx_hex)
10
+ api_parameter = API_PARAMETERS[:propagate]
11
+ query[:params][api_parameter] = raw_tx_hex
12
+ self
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,7 @@
1
+ require 'helloblock/resources/query'
2
+
3
+ module HelloBlock
4
+ class Wallet
5
+ extend HelloBlock::Query
6
+ end
7
+ end
@@ -0,0 +1,11 @@
1
+ module HelloBlock
2
+ module SpecHelper
3
+ def reset
4
+ @api_key = nil
5
+ @network = nil
6
+ @version = nil
7
+ @connection = nil
8
+ @connection_options = nil
9
+ end
10
+ end
11
+ end