class Nem::Client

@attr [Hash] options connection options

Constants

DEFAULTS
LOCAL_ONLY_PATHES

Attributes

options[R]

Public Class Methods

new(options = {}) click to toggle source

@param [hash] options HTTP Client connection information @option options [Symbol] :url URL @option options [Symbol] :scheme default http (http only) @option options [Symbol] :host default 127.0.0.1 @option options [Symbol] :port default 7890 @option options [Symbol] :timeout default 5

# File lib/nem/client.rb, line 35
def initialize(options = {})
  @options = parse_options(options)
end

Public Instance Methods

request(method, path, params = {}) { |hash| ... } click to toggle source

@param [Symbol] method HTTP Method(GET or POST) @param [String] path API Path @param [Hash] params API Parameters @return [Hash] Hash converted API Response

# 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| ... } click to toggle source

@param [Symbol] method HTTP Method(GET or POST) @param [String] path API Path @param [Hash] params API Parameters @return [Hash] Hash converted API Response @raise [Nem::Error] NIS error

# 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

Private Instance Methods

connection() click to toggle source
# File lib/nem/client.rb, line 91
def connection
  @connection ||= Faraday.new(url: @options[:url]) do |f|
    f.options[:timeout] = @options[:timeout]
    f.request :json
    # f.response :logger do | logger |
    #   logger.filter(/(privateKey=)(\w+)/,'\1[FILTERED]')
    # end
    f.adapter Faraday.default_adapter
  end.tap { |c| c.extend(Local) }
end
error(method, path, params, message) click to toggle source
# File lib/nem/client.rb, line 151
def error(method, path, params, message)
  Nem.logger.debug "host:%s\tmethod:%s\tpath:%s\tparams:%s\tmessage:%s" % [
    connection.url_prefix,
    method,
    path,
    params.to_hash,
    message
  ]
end
error_handling(hash) click to toggle source
# File lib/nem/client.rb, line 77
def error_handling(hash)
  error_klass = case hash[:error]
                when 'Not Found' then Nem::NotFoundError
                when 'Bad Request' then Nem::BadRequestError
                when 'Internal Server Error' then Nem::InternalServerError
                else Nem::Error
  end
  error_klass.new(hash[:message])
end
local_only?(path) click to toggle source
# File lib/nem/client.rb, line 87
def local_only?(path)
  LOCAL_ONLY_PATHES.include?(path)
end
log(method, path, params) click to toggle source
# File lib/nem/client.rb, line 142
def log(method, path, params)
  Nem.logger.debug "host:%s\tmethod:%s\tpath:%s\tparams:%s" % [
    connection.url_prefix,
    method,
    path,
    params.to_hash
  ]
end
parse_body(body) click to toggle source
# File lib/nem/client.rb, line 102
def parse_body(body)
  JSON.parse(body, symbolize_names: true)
end
parse_options(options = {}) click to toggle source
# File lib/nem/client.rb, line 106
def parse_options(options = {})
  defaults = DEFAULTS.dup
  options  = options.dup

  defaults[:url] = defaults[:url].call if defaults[:url].respond_to?(:call)

  defaults.keys.each do |key|
    options[key] = options[key.to_s] if options.key?(key.to_s)
  end

  url = options[:url] || defaults[:url]

  if url
    uri = URI(url)
    if uri.scheme == 'http'
      defaults[:scheme] = uri.scheme
      defaults[:host]   = uri.host
      defaults[:port]   = uri.port
    else
      raise ArgumentError, "invalid URI scheme '#{uri.scheme}'"
    end
  end

  defaults.keys.each do |key|
    options[key] = defaults[key] if options[key].nil?
  end

  options[:url] = URI::Generic.build(
    scheme: options[:scheme],
    host:   options[:host],
    port:   options[:port]
  ).to_s

  options
end