class Ubiquity::Envoi::API::Client::Requests::BaseRequest
Constants
- DEFAULT_PARAMETER_SEND_IN_VALUE
- HTTP_BASE_PATH
- HTTP_METHOD
- HTTP_PATH
- HTTP_SUCCESS_CODE
- PARAMETERS
Attributes
arguments[RW]
body[W]
client[RW]
default_parameter_send_in_value[RW]
initial_arguments[RW]
initial_options[RW]
initialized[RW]
missing_required_arguments[RW]
options[RW]
parameters[W]
path[W]
processed_parameters[RW]
query[W]
response[RW]
Public Class Methods
new(args = { }, options = { })
click to toggle source
# File lib/ubiquity/envoi/api/client/requests/base_request.rb, line 74 def initialize(args = { }, options = { }) @initial_arguments = args.dup @initial_options = options.dup @options = options.dup initialize_attributes if options.fetch(:initialize_attributes, true) after_initialize end
normalize_argument_hash_keys(hash)
click to toggle source
# File lib/ubiquity/envoi/api/client/requests/base_request.rb, line 24 def self.normalize_argument_hash_keys(hash) return hash unless hash.is_a?(Hash) Hash[ hash.dup.map { |k,v| [ normalize_parameter_name(k), v ] } ] end
normalize_parameter_name(name)
click to toggle source
# File lib/ubiquity/envoi/api/client/requests/base_request.rb, line 29 def self.normalize_parameter_name(name) (name || '').respond_to?(:to_s) ? name.to_s.gsub('_', '').gsub('-', '').downcase : name end
process_parameter(param, args = { }, args_out = { }, missing_required_arguments = [ ], processed_parameters = { }, default_parameter_send_in_value = DEFAULT_PARAMETER_SEND_IN_VALUE, options = { })
click to toggle source
# File lib/ubiquity/envoi/api/client/requests/base_request.rb, line 33 def self.process_parameter(param, args = { }, args_out = { }, missing_required_arguments = [ ], processed_parameters = { }, default_parameter_send_in_value = DEFAULT_PARAMETER_SEND_IN_VALUE, options = { }) args = normalize_argument_hash_keys(args) || { } if options.fetch(:normalize_argument_hash_keys, false) _k = param.is_a?(Hash) ? param : { :name => param, :required => false, :send_in => default_parameter_send_in_value } _k[:send_in] ||= default_parameter_send_in_value proper_parameter_name = _k[:name] param_name = normalize_parameter_name(proper_parameter_name) arg_key = (has_key = args.has_key?(param_name)) ? param_name : ( (_k[:aliases] || [ ]).map { |a| normalize_parameter_name(a) }.find { |a| has_key = args.has_key?(a) } || param_name ) value = has_key ? args[arg_key] : _k[:default_value] is_set = has_key || _k.has_key?(:default_value) processed_parameters[proper_parameter_name] = _k.merge(:value => value, :is_set => is_set) unless is_set missing_required_arguments << proper_parameter_name if _k[:required] else args_out[proper_parameter_name] = value end { :arguments_out => args_out, :processed_parameters => processed_parameters, :missing_required_arguments => missing_required_arguments } rescue => e raise e, "Error Processing Parameter: #{param.inspect} Args: #{args.inspect}. #{e.message}" end
process_parameters(params, args, options = { })
click to toggle source
# File lib/ubiquity/envoi/api/client/requests/base_request.rb, line 61 def self.process_parameters(params, args, options = { }) args = normalize_argument_hash_keys(args) || { } args_out = options[:arguments_out] || { } default_parameter_send_in_value = options[:default_parameter_send_in_value] || DEFAULT_PARAMETER_SEND_IN_VALUE processed_parameters = options[:processed_parameters] || { } missing_required_arguments = options[:missing_required_arguments] || [ ] params.each do |param| process_parameter(param, args, args_out, missing_required_arguments, processed_parameters, default_parameter_send_in_value) end { :arguments_out => args_out, :processed_parameters => processed_parameters, :missing_required_arguments => missing_required_arguments } end
Public Instance Methods
after_initialize()
click to toggle source
# File lib/ubiquity/envoi/api/client/requests/base_request.rb, line 84 def after_initialize process_parameters if initialized end
after_process_parameters()
click to toggle source
# File lib/ubiquity/envoi/api/client/requests/base_request.rb, line 132 def after_process_parameters # TO BE IMPLEMENTED IN CHILD CLASS end
Also aliased as: post_process_arguments
base_path()
click to toggle source
# File lib/ubiquity/envoi/api/client/requests/base_request.rb, line 147 def base_path @base_path ||= self.class::HTTP_BASE_PATH end
before_process_parameters()
click to toggle source
# File lib/ubiquity/envoi/api/client/requests/base_request.rb, line 127 def before_process_parameters # TO BE IMPLEMENTED IN CHILD CLASS end
Also aliased as: pre_process_arguments
body()
click to toggle source
# File lib/ubiquity/envoi/api/client/requests/base_request.rb, line 155 def body # body_arguments.empty? ? @body : body_arguments _body = @body _body_arguments = body_arguments return _body unless _body_arguments if _body.is_a?(Hash) && _body_arguments.is_a?(Hash) return _body.merge(_body_arguments) end _body_arguments end
body_arguments()
click to toggle source
# File lib/ubiquity/envoi/api/client/requests/base_request.rb, line 151 def body_arguments @body_arguments ||= arguments.dup.delete_if { |k,_| processed_parameters[k][:send_in] != :body } end
eval_http_path?()
click to toggle source
# File lib/ubiquity/envoi/api/client/requests/base_request.rb, line 171 def eval_http_path? @eval_http_path end
execute()
click to toggle source
# File lib/ubiquity/envoi/api/client/requests/base_request.rb, line 231 def execute @response = http_client.call_method(http_method, { :path => relative_path, :query => query, :body => body }, options) if client end
http_client()
click to toggle source
def response
client.response if client
end
# File lib/ubiquity/envoi/api/client/requests/base_request.rb, line 223 def http_client client.http_client end
http_method()
click to toggle source
# File lib/ubiquity/envoi/api/client/requests/base_request.rb, line 179 def http_method @http_method ||= self.class::HTTP_METHOD end
http_path()
click to toggle source
# File lib/ubiquity/envoi/api/client/requests/base_request.rb, line 175 def http_path @http_path ||= self.class::HTTP_PATH end
http_response()
click to toggle source
# File lib/ubiquity/envoi/api/client/requests/base_request.rb, line 227 def http_response @http_response ||= http_client.response.dup rescue nil end
http_success_code()
click to toggle source
@!group Attribute Readers
# File lib/ubiquity/envoi/api/client/requests/base_request.rb, line 139 def http_success_code @http_success_code end
initialize_attributes()
click to toggle source
# File lib/ubiquity/envoi/api/client/requests/base_request.rb, line 88 def initialize_attributes @client = options[:client] @missing_required_arguments = [ ] @default_parameter_send_in_value = options[:default_parameter_send_in_value] || self.class::DEFAULT_PARAMETER_SEND_IN_VALUE @processed_parameters = { } @arguments = { } @eval_http_path = options.fetch(:eval_http_path, true) @base_path = options[:base_path] @parameters = options[:parameters] @http_method = options[:http_method] @http_path = options[:http_path] ||= options[:path_raw] @http_success_code = options[:http_success_code] || HTTP_SUCCESS_CODE @path = options[:path] @path_arguments = nil @path_only = nil @query = options[:query] @query_arguments = nil @body = options[:body] @body_arguments = nil @response = nil @relative_path = nil @initialized = true end
Also aliased as: reset_attributes
parameters()
click to toggle source
# File lib/ubiquity/envoi/api/client/requests/base_request.rb, line 183 def parameters @parameters ||= self.class::PARAMETERS.dup end
path()
click to toggle source
The URI Path
# File lib/ubiquity/envoi/api/client/requests/base_request.rb, line 192 def path @path ||= File.join(base_path, (eval_http_path? ? eval(%("#{http_path}"), binding, __FILE__, __LINE__) : http_path)) end
path_arguments()
click to toggle source
# File lib/ubiquity/envoi/api/client/requests/base_request.rb, line 196 def path_arguments @path_arguments ||= Hash[ arguments.dup.delete_if { |k, _| processed_parameters[k][:send_in] != :path }. map { |k,v| [ k, CGI.escape(v.respond_to?(:to_s) ? v.to_s : '').gsub('+', '%20') ] } ] end
process_parameters(params = parameters, args = @initial_arguments, _options = @options)
click to toggle source
# File lib/ubiquity/envoi/api/client/requests/base_request.rb, line 121 def process_parameters(params = parameters, args = @initial_arguments, _options = @options) before_process_parameters unless _options.fetch(:skip_before_process_parameters, false) self.class.process_parameters(params, args, _options.merge(:processed_parameters => processed_parameters, :missing_required_arguments => missing_required_arguments, :default_parameter_send_in_value => default_parameter_send_in_value, :arguments_out => arguments)) after_process_parameters unless _options.fetch(:skip_after_process_parameters, false) end
query()
click to toggle source
# File lib/ubiquity/envoi/api/client/requests/base_request.rb, line 203 def query @query ||= begin query_arguments.is_a?(Hash) ? query_arguments.map { |k,v| "#{CGI.escape(k.to_s).gsub('+', '%20')}=#{CGI.escape(v.respond_to?(:to_s) ? v.to_s : v).gsub('+', '%20')}" }.join('&') : query_arguments end end
query_arguments()
click to toggle source
# File lib/ubiquity/envoi/api/client/requests/base_request.rb, line 209 def query_arguments @query_arguments ||= arguments.dup.delete_if { |k,_| processed_parameters[k][:send_in] != :query } end
relative_path()
click to toggle source
# File lib/ubiquity/envoi/api/client/requests/base_request.rb, line 187 def relative_path @relative_path ||= (path.start_with?('/') ? path[1..-1] : path) end
success?()
click to toggle source
# File lib/ubiquity/envoi/api/client/requests/base_request.rb, line 235 def success? _response = http_response and ([*http_success_code].include?(http_response.code)) _response end
uri_request_path()
click to toggle source
# File lib/ubiquity/envoi/api/client/requests/base_request.rb, line 213 def uri_request_path [ path ].concat( [*query].delete_if { |v| v.respond_to?(:empty?) and v.empty? } ).join('?') end