infura_ruby 1.1.0

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: bec78f6153262da586b44f024f9247c7508af7c7
4
+ data.tar.gz: cb2f8572bec68b2a76817af04e4645fd2e823cb3
5
+ SHA512:
6
+ metadata.gz: a3f091c2737ec85074f1b0659661ad27d35e0b0ba1208066f9f21be7224bc52f885507b0f6f9a2116e7c0b44f6c53ee599ce4161b30213ef63240628cee98397
7
+ data.tar.gz: 82edfdc537ea144ecff266f6d1273e74d31e493b41fbf14bfc8780aa1548a4381a1abc4ed5554cf77ad8724fdf9361ceb54adc6eb8ae6f07410061074d16acb5
data/.gitignore ADDED
@@ -0,0 +1,10 @@
1
+ *.gem
2
+ /.bundle/
3
+ /.yardoc
4
+ /Gemfile.lock
5
+ /_yardoc/
6
+ /coverage/
7
+ /doc/
8
+ /pkg/
9
+ /spec/reports/
10
+ /tmp/
data/README.md ADDED
@@ -0,0 +1,22 @@
1
+ # InfuraRuby
2
+
3
+ Ruby gem to wrap the [INFURA](https://github.com/ethereum/wiki/wiki/JSON-RPC() API which gives HTTP API access to ethereum and IPFS nodes. The API uses the same format as the [JSON RPC spec](https://github.com/ethereum/wiki/wiki/JSON-RPCi) for normal ethereum nodes.
4
+
5
+ For now, I only need the `getBalance` call an so that is all I will build. Feel free to add the rest of the functionality or I may get to it over time...
6
+
7
+ ## Usage
8
+
9
+ __Installation__
10
+ ```bash
11
+ gem install infura_ruby
12
+ ```
13
+
14
+ ```ruby
15
+ require 'infura_ruby'
16
+
17
+ # create a client object
18
+ infura = InfuraRuby.client(api_key: key)
19
+
20
+ # get the balance (in wei) of an address
21
+ infura.get_balance('0x81F631b8615EaB75d38DaC4d4bce4A5b63e10310') #=> 591686024850016
22
+ ```
@@ -0,0 +1,24 @@
1
+ lib = File.expand_path('../lib', __FILE__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require 'infura_ruby/version'
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "infura_ruby"
7
+ spec.version = InfuraRuby::VERSION
8
+ spec.authors = ["Julian Borrey"]
9
+ spec.email = ["julianborrey@gmail.com"]
10
+
11
+ spec.summary = "Library for the INFURA API."
12
+ spec.description = "Ruby client to the INFURA (infura.io) API. Allows HTTP "\
13
+ "access to ethereum and IPFS nodes."
14
+ spec.homepage = "https://github.com/jborrey/infura_ruby"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(/^spec/) }
18
+ spec.require_paths = ["lib"]
19
+
20
+ spec.add_dependency "faraday", "~> 0.11.0"
21
+ spec.add_development_dependency "bundler", "~> 1.13"
22
+ spec.add_development_dependency "rake", "~> 10.0"
23
+ spec.add_development_dependency "rspec", "~> 3.0"
24
+ end
@@ -0,0 +1,14 @@
1
+ require 'infura_ruby/version'
2
+ require 'infura_ruby/client'
3
+
4
+ module InfuraRuby
5
+ class InvalidApiKeyError < StandardError; end
6
+ class InvalidNetworkError < StandardError; end
7
+
8
+ class << self
9
+ # Generate a new client for the Infura api.
10
+ def client(api_key:, network: :main)
11
+ Client.new(api_key: api_key, network: network)
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,69 @@
1
+ require 'faraday'
2
+ require 'json'
3
+
4
+ module InfuraRuby
5
+ class Client
6
+ # Infura URLs for each network.
7
+ NETWORK_URLS = {
8
+ main: 'https://mainnet.infura.io',
9
+ test: 'https://ropsten.infura.io',
10
+ consensys: 'https://consensysnet.inufra.io'
11
+ }.freeze
12
+
13
+ JSON_RPC_METHODS = [
14
+ 'eth_getBalance'
15
+ ].freeze
16
+
17
+ def initialize(api_key:, network: :main)
18
+ validate_api_key(api_key)
19
+ validate_network(network)
20
+
21
+ @api_key = api_key
22
+ @network = network
23
+ end
24
+
25
+ # Returns balance of address in wei as integer.
26
+ def get_balance(address)
27
+ resp = conn.post do |req|
28
+ req.headers['Content-Type'] = 'application/json'
29
+ req.body = json_rpc(method: 'eth_getBalance', params: [address, 'latest']).to_json
30
+ end
31
+
32
+ resp_body = JSON.parse(resp.body)
33
+ wei_amount_hex_string = resp_body['result']
34
+ wei_amount_hex_string.to_i(16)
35
+ end
36
+
37
+ private
38
+
39
+ # TODO: this JSON RPC object should be a whole object / gem.
40
+ def json_rpc(method:, params:)
41
+ validate_json_rpc_method(method)
42
+
43
+ {
44
+ "jsonrpc" => "2.0",
45
+ "method" => method,
46
+ "params" => params,
47
+ "id" => 1
48
+ }
49
+ end
50
+
51
+ def validate_json_rpc_method(method)
52
+ raise NotImplementedError unless JSON_RPC_METHODS.include?(method)
53
+ end
54
+
55
+ def conn
56
+ @conn ||= Faraday.new(
57
+ url: "#{NETWORK_URLS[@network]}/#{@api_key}",
58
+ )
59
+ end
60
+
61
+ def validate_api_key(api_key)
62
+ raise InvalidApiKeyError unless /^[a-zA-Z0-9]{20}$/ =~ api_key
63
+ end
64
+
65
+ def validate_network(network)
66
+ raise InvalidNetworkError if NETWORK_URLS[network].nil?
67
+ end
68
+ end
69
+ end
@@ -0,0 +1,3 @@
1
+ module InfuraRuby
2
+ VERSION = '1.1.0'
3
+ end
metadata ADDED
@@ -0,0 +1,107 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: infura_ruby
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Julian Borrey
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-01-25 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: faraday
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 0.11.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 0.11.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.13'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.13'
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: '3.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3.0'
69
+ description: Ruby client to the INFURA (infura.io) API. Allows HTTP access to ethereum
70
+ and IPFS nodes.
71
+ email:
72
+ - julianborrey@gmail.com
73
+ executables: []
74
+ extensions: []
75
+ extra_rdoc_files: []
76
+ files:
77
+ - ".gitignore"
78
+ - README.md
79
+ - infura_ruby.gemspec
80
+ - lib/infura_ruby.rb
81
+ - lib/infura_ruby/client.rb
82
+ - lib/infura_ruby/version.rb
83
+ homepage: https://github.com/jborrey/infura_ruby
84
+ licenses:
85
+ - MIT
86
+ metadata: {}
87
+ post_install_message:
88
+ rdoc_options: []
89
+ require_paths:
90
+ - lib
91
+ required_ruby_version: !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - - ">="
94
+ - !ruby/object:Gem::Version
95
+ version: '0'
96
+ required_rubygems_version: !ruby/object:Gem::Requirement
97
+ requirements:
98
+ - - ">="
99
+ - !ruby/object:Gem::Version
100
+ version: '0'
101
+ requirements: []
102
+ rubyforge_project:
103
+ rubygems_version: 2.6.9
104
+ signing_key:
105
+ specification_version: 4
106
+ summary: Library for the INFURA API.
107
+ test_files: []