Class: BlockIo::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/block_io/client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args = {}) ⇒ Client

Returns a new instance of Client.

Raises:

  • (Exception)


7
8
9
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
# File 'lib/block_io/client.rb', line 7

def initialize(args = {})
  # api_key
  # pin
  # version
  # hostname
  # proxy
  # pool_size
  # keys
  
  raise "Must provide an API Key." unless args.key?(:api_key) and args[:api_key].to_s.size > 0
  
  @api_key = args[:api_key]
  @encryption_key = Helper.pinToAesKey(args[:pin] || "") if args.key?(:pin)
  @version = args[:version] || 2
  @hostname = args[:hostname] || "block.io"
  @proxy = args[:proxy] || {}
  @keys = args[:keys] || []
  @use_low_r = args[:use_low_r]
  @raise_exception_on_error = args[:raise_exception_on_error] || false

  raise Exception.new("Keys must be provided as an array.") unless @keys.is_a?(Array)
  raise Exception.new("Keys must be BlockIo::Key objects.") unless @keys.all?{|key| key.is_a?(BlockIo::Key)}

  # make a hash of the keys we've been given
  @keys = @keys.inject({}){|h,v| h[v.public_key] = v; h}
  
  raise Exception.new("Must specify hostname, port, username, password if using a proxy.") if @proxy.keys.size > 0 and [:hostname, :port, :username, :password].any?{|x| !@proxy.key?(x)}

  @conn = ConnectionPool.new(:size => args[:pool_size] || 5) { http = HTTP.headers(:accept => "application/json", :user_agent => "gem:block_io:#{VERSION}");
    http = http.via(args.dig(:proxy, :hostname), args.dig(:proxy, :port), args.dig(:proxy, :username), args.dig(:proxy, :password)) if @proxy.key?(:hostname);
    http = http.persistent("https://#{@hostname}");
    http }
  
  # this will get populated after a successful API call
  @network = nil

end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(m, *args) ⇒ Object

Raises:

  • (Exception)


45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/block_io/client.rb', line 45

def method_missing(m, *args)
  
  method_name = m.to_s

  raise Exception.new("Must provide arguments as a Hash.") unless args.size <= 1 and args.all?{|x| x.is_a?(Hash)}
  raise Exception.new("Parameter keys must be symbols. For instance: :label => 'default' instead of 'label' => 'default'") unless args[0].nil? or args[0].keys.all?{|x| x.is_a?(Symbol)}
  raise Exception.new("Cannot pass PINs to any calls. PINs can only be set when initiating this library.") if !args[0].nil? and args[0].key?(:pin)
  raise Exception.new("Do not specify API Keys here. Initiate a new BlockIo object instead if you need to use another API Key.") if !args[0].nil? and args[0].key?(:api_key)
  
  if BlockIo::WITHDRAW_METHODS.key?(method_name) then
    # it's a withdrawal call
    withdraw(args[0], method_name)
  elsif BlockIo::SWEEP_METHODS.key?(method_name) then
    # we're sweeping from an address
    sweep(args[0], method_name)
  elsif BlockIo::FINALIZE_SIGNATURE_METHODS.key?(method_name) then
    # we're finalize the transaction signatures
    finalize_signature(args[0], method_name)
  else
    api_call({:method_name => method_name, :params => args[0] || {}})
  end
  
end

Instance Attribute Details

#api_keyObject (readonly)

Returns the value of attribute api_key.



5
6
7
# File 'lib/block_io/client.rb', line 5

def api_key
  @api_key
end

#networkObject (readonly)

Returns the value of attribute network.



5
6
7
# File 'lib/block_io/client.rb', line 5

def network
  @network
end

#versionObject (readonly)

Returns the value of attribute version.



5
6
7
# File 'lib/block_io/client.rb', line 5

def version
  @version
end