class Nis::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/nis/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/nis/client.rb, line 43 def request(method, path, params = {}) log(method, path, params) if connection.remote? && local_only?(path) raise Nis::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) 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 [Nis::Error] NIS error
# File lib/nis/client.rb, line 62 def request!(method, path, params = {}) hash = request(method, path, params) raise Nis::Util.error_handling(hash) if hash && hash.key?(:error) block_given? ? yield(hash) : hash end
Private Instance Methods
connection()
click to toggle source
# File lib/nis/client.rb, line 74 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
local_only?(path)
click to toggle source
# File lib/nis/client.rb, line 70 def local_only?(path) LOCAL_ONLY_PATHES.include?(path) end
log(method, path, params)
click to toggle source
# File lib/nis/client.rb, line 125 def log(method, path, params) Nis.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/nis/client.rb, line 85 def parse_body(body) JSON.parse(body, symbolize_names: true) end
parse_options(options = {})
click to toggle source
# File lib/nis/client.rb, line 89 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