lib/netki/netki.rb in netki-0.0.1 vs lib/netki/netki.rb in netki-0.0.2

- old
+ new

@@ -1,9 +1,10 @@ -require "rubygems" -require "bundler/setup" -require "httpclient" -require "json" +require 'rubygems' +require 'bundler/setup' +require 'httpclient' +require 'json' +require_relative 'utilities' module Netki # Request Utility Functionality def self.process_request(api_key, partner_id, uri, method, bodyData=nil) @@ -11,12 +12,12 @@ raise "Invalid HTTP Method" unless ['GET','POST','PUT','DELETE'].include? method # Setup Headers headers = {} headers["Content-Type"] = "application/json" - headers["Authorization"] = api_key - headers["X-Partner-ID"] = partner_id + 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 @@ -55,40 +56,58 @@ end return ret_data end + # Obtain a WalletName object by querying the Netki Open API. + 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') + + domain_parts = response['wallet_name'].split('.') + wallet_name = domain_parts.shift + + parsed = begin + parse_bitcoin_uri(response['wallet_address']).merge( + {_raw: response['wallet_address']}) + rescue InvalidURIError => e + response['wallet_address'] + end + WalletName.new( + domain_parts.join('.'), wallet_name, + { response['currency'] => parsed } + ) + end + ## # The WalletName object represents a Netki Wallet Name object. # class WalletName ## - # :args: domain_name, name, wallets, external_id, id - def initialize(domain_name, name, wallets={}, external_id=nil, id=nil) #:nodoc: - @id = id + # :args: domain_name, name, wallets, external_id, id, + def initialize(domain_name, name, wallets={}, external_id: nil, id: nil) @domain_name = domain_name @name = name - @wallets = wallets + + @wallets = wallets.inject({}) do |hsh, (currency, value)| + hsh[currency] = value.is_a?(Hash) ? value : { address: value } + hsh + end @external_id = external_id + @id = id end - attr_reader :id - attr_reader :domain_name - attr_reader :name - attr_reader :external_id + attr_accessor :domain_name, :name, :id, :external_id - attr_writer :id - attr_writer :domain_name - attr_writer :name - attr_writer :external_id - # :section: Getters # Get Address for Existing Currency def get_address(currency) - @wallets[currency] + @wallets[currency][:address] end # Get Wallet Name Array of Used Currencies def used_currencies @wallets.keys @@ -96,11 +115,11 @@ # :section: Currency Address Operations # Set the address or URI for the given currency for this wallet name def set_currency_address(currency, address) - @wallets[currency] = address + @wallets[currency][:address] = address end # Remove a used currency from this wallet name def remove_currency(currency) @wallets.delete(currency) if @wallets.has_key? currency @@ -117,22 +136,25 @@ # :section: Actions # Save the currency WalletName object to the remote service def save wallet_data = [] - @wallets.each do |currency, wallet_address| - wallet_data.push({ - currency: currency, - wallet_address: wallet_address - }) + @wallets.each do |currency, wallet| + # NOTE: Unsure if remote service supports storing metadata (params/bip70 req)? + wallet_data.push( + { + currency: currency, + wallet_address: wallet[:_raw] ? wallet[:_raw] : wallet[:address] + } + ) end wn_data = { - domain_name: @domain_name, - name: @name, - wallets: wallet_data, - external_id: @external_id + domain_name: @domain_name, + name: @name, + wallets: wallet_data, + external_id: @external_id || 'null' } wn_api_data = {} wn_api_data['wallet_names'] = [wn_data,] @@ -284,11 +306,14 @@ ## # Create a new Wallet Name object using this factory method. # * domain_name -> The pre-configured domain name you would like to add this new wallet name to # * name -> The DNS name that you would like this new wallet name to have (ie.. name.domain_name) - # * wallets -> This is a hash where the key is the currency (ie.. btc, ltc, dgc, tusd) and the value is the wallet address or URL of the BIP32 / BIP70 address server + # * wallets -> This is a hash where the key is the currency (ie.. btc, ltc, dgc, tusd) and the value is: + # the wallet address OR + # URL of the BIP32 / BIP70 address server OR + # a hash containing an :address and other metadata # * external_id -> Any unique external ID that you may want to use to track this specific wallet name # def create_new_walletname(domain_name, name, wallets={}, external_id=nil) new_wn = WalletName.new(domain_name, name, wallets, external_id) new_wn.set_api_opts(@api_url, @partner_id, @api_key) @@ -321,6 +346,6 @@ wn_list.push(wn_obj) end wn_list end end -end \ No newline at end of file +end