module Netether
Constants
- KNOWN_PREFIXES
- VERSION
Public Class Methods
_parse_bip_21(querystring)
click to toggle source
github.com/bitcoin/bips/blob/master/bip-0021.mediawiki
# File lib/netether/utilities.rb, line 34 def self._parse_bip_21(querystring) param_pairs = querystring.split('&') # '&' reserved as separator in bip21 param_pairs.inject({}) do |hsh, pair| parts = pair.split('=') # '=' reserved as separator in bip21 raise InvalidURIError.new("unbalanced parameter #{pair}") unless ( parts.size == 2 && parts[0].size > 0 && parts[1].size > 0) raise InvalidURIError.new("duplicate parameter #{parts[0]}") unless hsh[parts[0]].nil? hsh[parts[0]] = parts[1] hsh end end
_parse_bip_72(querystring)
click to toggle source
github.com/bitcoin/bips/blob/master/bip-0072.mediawiki
# File lib/netether/utilities.rb, line 28 def self._parse_bip_72(querystring) params = _parse_bip_21(querystring) { r: params['r'], params: params } end
parse_bitcoin_uri(uri, tolerate_errors: false)
click to toggle source
# File lib/netether/utilities.rb, line 7 def self.parse_bitcoin_uri(uri, tolerate_errors: false) parts = uri.split(':', 2) unless KNOWN_PREFIXES.include?(parts.shift) raise InvalidURIError.new("unknown URI prefix") end # parts => [base58][?[bitcoinparam, [&bitcoinparam, ...]] base58address, query = parts.first.split('?', 2) response = { address: base58address } begin response.merge!(_parse_bip_72(query)) rescue InvalidURIError => e raise e unless tolerate_errors end response end
process_request(api_key, partner_id, uri, method, bodyData=nil)
click to toggle source
Request Utility Functionality
# File lib/netether/netether.rb, line 10 def self.process_request(api_key, partner_id, uri, method, bodyData=nil) raise "Invalid HTTP Method" unless ['GET','POST','PUT','DELETE'].include? method # Setup Headers headers = {} headers["Content-Type"] = "application/json" headers["Authorization"] = api_key if api_key headers["X-Partner-ID"] = partner_id if partner_id # Setup Request Options opts = {} opts[:header] = headers opts[:body] = bodyData if bodyData client = HTTPClient.new _uri = URI.parse(uri) response = client.request(method, _uri, opts) # Short Circuit Return if 204 Response on DELETE return {} if response.code == 204 && method == "DELETE" # We should have response content at this point raise "Empty Response Received" if response.content.nil? || response.content.empty? # Verify we have the correct content type raise "Non-JSON Content Type" if response.headers['Content-Type'] != 'application/json' # Make Sure We Can Decode JSON Response begin ret_data = JSON.parse(response.content) rescue JSON::ParserError => e raise "Invalid JSON Response Received" end # Process Error if response.code >= 300 || !ret_data['success'] return ret_data['message'] end return ret_data end
wallet_lookup(uri, currency, api_url='https://api.netki.com')
click to toggle source
Obtain a WalletName
object by querying the Netki Open API.
# File lib/netether/netether.rb, line 54 def self.wallet_lookup(uri, currency, api_url='https://api.netki.com') wallet_name = URI.parse(uri).host || uri.to_s response = process_request(nil, nil, "#{api_url}/api/wallet_lookup/#{wallet_name}/#{currency.downcase}", 'GET') netki_address = response['wallet_address'] if !netki_address.nil? && netki_address != 0 return netki_address else return false, "No Address Found" end end