Class: Nem::Client

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

Defined Under Namespace

Modules: Local

Constant Summary collapse

DEFAULTS =
{
  url:     -> { ENV['NEM_URL'] },
  scheme:  'http',
  host:    '127.0.0.1',
  port:    7890,
  timeout: 5
}.freeze
LOCAL_ONLY_PATHES =
[
  '/account/generate',
  '/local/account/transfers/incoming',
  '/local/account/transfers/outgoing',
  '/local/account/transfers/all',
  '/node/boot',
  '/transaction/prepare-announce',
  '/shutdown'
]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Client

Returns a new instance of Client.

Parameters:

  • options (hash) (defaults to: {})

    HTTP Client connection information

Options Hash (options):

  • :url (Symbol)

    URL

  • :scheme (Symbol)

    default http (http only)

  • :host (Symbol)

    default 127.0.0.1

  • :port (Symbol)

    default 7890

  • :timeout (Symbol)

    default 5



35
36
37
# File 'lib/nem/client.rb', line 35

def initialize(options = {})
  @options = parse_options(options)
end

Instance Attribute Details

#optionsHash

connection options

Returns:

  • (Hash)

    the current value of options



8
9
10
# File 'lib/nem/client.rb', line 8

def options
  @options
end

Instance Method Details

#request(method, path, params = {}) ⇒ Hash

Returns Hash converted API Response.

Parameters:

  • method (Symbol)

    HTTP Method(GET or POST)

  • path (String)

    API Path

  • params (Hash) (defaults to: {})

    API Parameters

Returns:

  • (Hash)

    Hash converted API Response



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/nem/client.rb', line 43

def request(method, path, params = {})
  log(method, path, params) if Nem.debug
  if connection.remote? && local_only?(path)
    raise Nem::Error, "The request (#{method} #{path}) is only permitted to local NIS."
  end
  if params.is_a?(Hash) && !params.empty?
    params.reject! { |_, value| value.nil? }
  end

  res = connection.send(method, path, params)
  # begin
  # rescue Faraday::ConnectionFailed => err
  #   error(method, path, params, err.message)
  #   retry
  # end

  body = res.body
  hash = parse_body(body) unless body.empty?
  block_given? ? yield(hash) : hash
end

#request!(method, path, params = {}) ⇒ Hash

Returns Hash converted API Response.

Parameters:

  • method (Symbol)

    HTTP Method(GET or POST)

  • path (String)

    API Path

  • params (Hash) (defaults to: {})

    API Parameters

Returns:

  • (Hash)

    Hash converted API Response

Raises:



69
70
71
72
73
# File 'lib/nem/client.rb', line 69

def request!(method, path, params = {})
  hash = request(method, path, params)
  raise error_handling(hash) if hash && hash.key?(:error)
  block_given? ? yield(hash) : hash
end