Module: Netether

Defined in:
lib/netether.rb,
lib/netether/version.rb,
lib/netether/netether.rb,
lib/netether/utilities.rb

Defined Under Namespace

Classes: InvalidURIError, NetkiPartner, WalletName

Constant Summary collapse

VERSION =
"0.0.6"
KNOWN_PREFIXES =
[ 'bitcoin', 'litecoin', 'dogecoin' ]

Class Method Summary collapse

Class Method Details

._parse_bip_21(querystring) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# 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) ⇒ Object



28
29
30
31
# 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) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# 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) ⇒ Object

Request Utility Functionality



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# 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') ⇒ Object

Obtain a WalletName object by querying the Netki Open API.



54
55
56
57
58
59
60
61
62
63
64
65
66
# 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