blocktrail 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 7e73c584f0de2d9f87d47d34b21d7b172ac0de20
4
+ data.tar.gz: 7960133d5b375727a74520d64539d5be8b7d5904
5
+ SHA512:
6
+ metadata.gz: 4cd5f9c2f62c3fad9c2bff579884e99b0090775d41fe8e9626b741f5a322391d81176d1f4046a1acc6a264c757dcc3bfc5d34cc62031e8bd7c298082c0a3f41e
7
+ data.tar.gz: 599388a820d983676f0967de4bd7e78d1f1f83ada69edfdd85e8628ce1e6f66e41fcdb811339030893d24b5dd035884cc583337987cdf3ade904e689c8845abf
@@ -0,0 +1,10 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ .env
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --require spec_helper
data/Gemfile ADDED
@@ -0,0 +1,5 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
4
+
5
+ gem 'dotenv', '~> 2.2'
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 Yuri Skurikhin
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,42 @@
1
+ BlockTrail Ruby SDK
2
+ =====================
3
+ This is the BlockTrail Ruby SDK. This SDK contains methods for easily interacting with the BlockTrail API.
4
+
5
+ IMPORTANT! FLOATS ARE EVIL!!
6
+ ----------------------------
7
+ As is best practice with financial data, The API returns all values as an integer, the Bitcoin value in Satoshi's.
8
+
9
+ The BlockTrail SDK has some easy to use functions to do this for you, we recommend using these
10
+ and we also **strongly** recommend doing all Bitcoin calculation and storing of data in integers
11
+ and only convert to/from Bitcoin float values for displaying it to the user.
12
+
13
+ Installation
14
+ ------------
15
+ Add this line to your application's Gemfile:
16
+
17
+ ```ruby
18
+ gem 'blocktrail'
19
+ ```
20
+
21
+ Or install it yourself as:
22
+
23
+ ```
24
+ $ gem install blocktrail
25
+ ```
26
+
27
+ Usage
28
+ -----
29
+
30
+ To use the BlockTrail API, you need your API_KEY as well as a API_SECRET. The gem reads both values from the environment variables. Alternatively you can specify the values by configuring the `Blocktrail::Client` like this:
31
+
32
+ Please visit our official documentation at https://www.blocktrail.com/api/docs/ for the usage.
33
+
34
+ Support and Feedback
35
+ --------------------
36
+
37
+ If you find a bug, please submit the issue in Github directly.
38
+ [BlockTrail-Ruby-SDK Issues](https://github.com/yunixon/blocktrail/issues)
39
+
40
+ License
41
+ -------
42
+ The BlockTrail Ruby SDK is released under the terms of the MIT license. See LICENCE.md for more information or see http://opensource.org/licenses/MIT.
@@ -0,0 +1,5 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+ task :default => :spec
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "blocktrail"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,29 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'blocktrail/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "blocktrail"
8
+ spec.version = Blocktrail::VERSION
9
+ spec.authors = ["Yuri Skurikhin"]
10
+ spec.email = ["yunixon@gmail.com"]
11
+
12
+ spec.summary = %q{Ruby bindings for the Blocktrail API.}
13
+ spec.description = %q{Ruby bindings for the Blocktrail API.}
14
+ spec.homepage = "https://github.com/yunixon/blocktrail"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
18
+ f.match(%r{^(test|spec|features)/})
19
+ end
20
+ spec.bindir = "exe"
21
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
22
+ spec.require_paths = ["lib"]
23
+
24
+ spec.add_dependency "rest-client", "~> 2.0"
25
+
26
+ spec.add_development_dependency "bundler", "~> 1.14"
27
+ spec.add_development_dependency "rake", "~> 10.0"
28
+ spec.add_development_dependency "rspec"
29
+ end
@@ -0,0 +1,7 @@
1
+ require 'blocktrail/version'
2
+ require 'blocktrail/client'
3
+ require 'blocktrail/exceptions'
4
+
5
+ module Blocktrail
6
+ SDK_USER_AGENT = "blocktrail-sdk-ruby"
7
+ end
@@ -0,0 +1,138 @@
1
+ require 'rest-client'
2
+ require 'digest/md5'
3
+ require 'json'
4
+ require 'blocktrail/exceptions'
5
+
6
+ module Blocktrail
7
+ class Client
8
+ attr_accessor :api_key, :api_secret, :api_version, :testnet, :debug
9
+
10
+ def initialize(api_key, api_secret, api_version = 'v1', testnet = true, debug = true)
11
+ @api_key = api_key
12
+ @api_secret = api_secret
13
+ @api_version = api_version
14
+ @testnet = testnet
15
+ @debug = debug
16
+ end
17
+
18
+ def api_key
19
+ @api_key || ENV['API_KEY']
20
+ end
21
+
22
+ def api_secret
23
+ @api_secret ||= ENV['API_SECRET']
24
+ end
25
+
26
+ def default_headers
27
+ {
28
+ 'Content-Type' => 'application/json',
29
+ 'User-Agent': "#{Blocktrail::SDK_USER_AGENT}/#{Blocktrail::VERSION}",
30
+ 'Date': Time.now.utc.iso8601,
31
+ 'Content-MD5': Digest::MD5.hexdigest('')
32
+ }
33
+ end
34
+
35
+ def default_params
36
+ {
37
+ 'api_key': api_key
38
+ }
39
+ end
40
+
41
+ def get(*args)
42
+ request(:get, *args)
43
+ end
44
+
45
+ def post(*args)
46
+ request(:post, *args)
47
+ end
48
+
49
+ def put(*args)
50
+ request(:put, *args)
51
+ end
52
+
53
+ def delete(*args)
54
+ request(:delete, *args)
55
+ end
56
+
57
+ def head(*args)
58
+ request(:head, *args)
59
+ end
60
+
61
+ def options(*args)
62
+ request(:options, *args)
63
+ end
64
+
65
+ # Data API
66
+
67
+ def address(address)
68
+ get("/address/#{address}")
69
+ end
70
+
71
+ def address_transactions(address, page = 1, limit = 20, sort_dir = 'asc')
72
+ get("/address/#{address}/transactions", headers: { params: { page: page, limit: limit, sort_dir: sort_dir } })
73
+ end
74
+
75
+ def address_unconfirmed_transactions(address, page = 1, limit = 20, sort_dir = 'asc')
76
+ get("/address/#{address}/unconfirmed-transactions", headers: { params: { page: page, limit: limit, sort_dir: sort_dir } })
77
+ end
78
+
79
+ def address_unspent_outputs(address, page = 1, limit = 20, sort_dir = 'asc')
80
+ get("/address/#{address}/unspent-outputs", headers: { params: { page: page, limit: limit, sort_dir: sort_dir } })
81
+ end
82
+
83
+ def all_blocks(page = 1, limit = 20, sort_dir = 'asc')
84
+ get("/all-blocks", headers: { params: { page: page, limit: limit, sort_dir: sort_dir } })
85
+ end
86
+
87
+ def block_latest
88
+ get("/block/latest")
89
+ end
90
+
91
+ def block(block)
92
+ get("/block/#{block}")
93
+ end
94
+
95
+ def block_transactions(block, page = 1, limit = 20, sort_dir = 'asc')
96
+ get("/block/#{block}/transactions", headers: { params: { page: page, limit: limit, sort_dir: sort_dir } })
97
+ end
98
+
99
+ def transaction(txhash)
100
+ get("/transaction/#{txhash}")
101
+ end
102
+
103
+ def all_webhooks(page = 1, limit = 20)
104
+ get("/webhooks", headers: { params: { page: page, limit: limit } })
105
+ end
106
+
107
+ def webhook(identifier)
108
+ get("/webhook/#{identifier}")
109
+ end
110
+
111
+ def webhook_events(identifier, page = 1, limit = 20)
112
+ get("/webhook/#{identifier}/events", headers: { params: { page: page, limit: limit } })
113
+ end
114
+
115
+ def price
116
+ get("/price")
117
+ end
118
+
119
+ private
120
+
121
+ def request(method, url, payload = {}, headers = {})
122
+ url = "https://api.blocktrail.com/#{api_version}/#{testnet ? 't' : ''}btc#{url}"
123
+ headers = default_headers.merge(headers).merge({ params: default_params })
124
+ payload = payload.to_json
125
+
126
+ response = RestClient::Request.execute(method: method, url: url, payload: payload, headers: headers)
127
+ if debug
128
+ puts 'Request: ' + response.request.inspect
129
+ puts 'Status code: ' + response.code.inspect
130
+ puts 'Headers: ' + response.headers.inspect
131
+ puts 'Content: ' + response.body.inspect
132
+ end
133
+ response.empty? ? nil : JSON.parse(response)
134
+ rescue RestClient::ExceptionWithResponse => error
135
+ raise Blocktrail::Exceptions.build_exception(error)
136
+ end
137
+ end
138
+ end
@@ -0,0 +1,60 @@
1
+ module Blocktrail
2
+ EXCEPTION_INVALID_CREDENTIALS = "Your credentials are incorrect."
3
+ EXCEPTION_GENERIC_HTTP_ERROR = "An HTTP Error has occurred!"
4
+ EXCEPTION_GENERIC_SERVER_ERROR = "An Server Error has occurred!"
5
+ EXCEPTION_EMPTY_RESPONSE = "The HTTP Response was empty."
6
+ EXCEPTION_UNKNOWN_ENDPOINT_SPECIFIC_ERROR = "The endpoint returned an unknown error."
7
+ EXCEPTION_MISSING_ENDPOINT = "The endpoint you've tried to access does not exist. Check your URL."
8
+ EXCEPTION_OBJECT_NOT_FOUND = "The object you've tried to access does not exist."
9
+
10
+ module Exceptions
11
+ def self.build_exception(error)
12
+ case error.http_code
13
+ when 400, 403
14
+ data = JSON.parse(error.response)
15
+ if data.present? && data['msg'] && data['code']
16
+ Blocktrail::Exceptions::EndpointSpecificError.new(data['msg'], data['code'])
17
+ else
18
+ Blocktrail::Exceptions::UnknownEndpointSpecificError.new(Blocktrail::EXCEPTION_UNKNOWN_ENDPOINT_SPECIFIC_ERROR)
19
+ end
20
+ when 401
21
+ Blocktrail::Exceptions::InvalidCredentials.new(Blocktrail::EXCEPTION_INVALID_CREDENTIALS, 401)
22
+ when 404
23
+ if error.response.body == 'Endpoint Not Found'
24
+ Blocktrail::Exceptions::MissingEndpoint.new(Blocktrail::EXCEPTION_MISSING_ENDPOINT, 404)
25
+ else
26
+ Blocktrail::Exceptions::ObjectNotFound.new(Blocktrail::EXCEPTION_OBJECT_NOT_FOUND, 404)
27
+ end
28
+ when 500
29
+ Blocktrail::Exceptions::GenericServerError.new(Blocktrail::EXCEPTION_GENERIC_SERVER_ERROR, error.http_code)
30
+ else
31
+ Blocktrail::Exceptions::GenericHTTPError.new(Blocktrail::EXCEPTION_GENERIC_HTTP_ERROR, error.http_code)
32
+ end
33
+ rescue
34
+ Blocktrail::Exceptions::Exception.new(error.message)
35
+ end
36
+
37
+ class Exception < StandardError
38
+ attr_reader :code
39
+
40
+ def initialize(message, code = nil)
41
+ @code = code
42
+ super(message)
43
+ end
44
+
45
+ def message
46
+ self.code.present? ? "[#{self.code}] #{self.message}" : self.message
47
+ end
48
+ end
49
+
50
+ class InvalidFormat < Exception; end
51
+ class EmptyResponse < Exception; end
52
+ class EndpointSpecificError < Exception; end
53
+ class UnknownEndpointSpecificError < Exception; end
54
+ class InvalidCredentials < Exception; end
55
+ class MissingEndpoint < Exception; end
56
+ class ObjectNotFound < Exception; end
57
+ class GenericHTTPError < Exception; end
58
+ class GenericServerError < Exception; end
59
+ end
60
+ end
@@ -0,0 +1,3 @@
1
+ module Blocktrail
2
+ VERSION = '0.1.0'
3
+ end
metadata ADDED
@@ -0,0 +1,113 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: blocktrail
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Yuri Skurikhin
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-03-19 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rest-client
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '2.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '2.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.14'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.14'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description: Ruby bindings for the Blocktrail API.
70
+ email:
71
+ - yunixon@gmail.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - ".rspec"
78
+ - Gemfile
79
+ - LICENSE.txt
80
+ - README.md
81
+ - Rakefile
82
+ - bin/console
83
+ - bin/setup
84
+ - blocktrail.gemspec
85
+ - lib/blocktrail.rb
86
+ - lib/blocktrail/client.rb
87
+ - lib/blocktrail/exceptions.rb
88
+ - lib/blocktrail/version.rb
89
+ homepage: https://github.com/yunixon/blocktrail
90
+ licenses:
91
+ - MIT
92
+ metadata: {}
93
+ post_install_message:
94
+ rdoc_options: []
95
+ require_paths:
96
+ - lib
97
+ required_ruby_version: !ruby/object:Gem::Requirement
98
+ requirements:
99
+ - - ">="
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ required_rubygems_version: !ruby/object:Gem::Requirement
103
+ requirements:
104
+ - - ">="
105
+ - !ruby/object:Gem::Version
106
+ version: '0'
107
+ requirements: []
108
+ rubyforge_project:
109
+ rubygems_version: 2.5.1
110
+ signing_key:
111
+ specification_version: 4
112
+ summary: Ruby bindings for the Blocktrail API.
113
+ test_files: []