Class: Ubiquity::Iconik::API::Client::Requests::BaseRequest
- Inherits:
-
Object
- Object
- Ubiquity::Iconik::API::Client::Requests::BaseRequest
- Defined in:
- lib/ubiquity/iconik/api/client/requests/base_request.rb
Constant Summary collapse
- HTTP_METHOD =
:get
- HTTP_BASE_PATH =
'/API/'
- HTTP_PATH =
''
- HTTP_SUCCESS_CODE =
'200'
- DEFAULT_PARAMETER_SEND_IN_VALUE =
:body
- PARAMETERS =
[ ]
Attribute Readers collapse
-
#arguments ⇒ Object
Returns the value of attribute arguments.
- #body ⇒ Object
-
#client ⇒ Object
Returns the value of attribute client.
- #parameters ⇒ Object
-
#path ⇒ Object
The URI Path.
- #query ⇒ Object
Instance Attribute Summary collapse
-
#default_parameter_send_in_value ⇒ Object
Returns the value of attribute default_parameter_send_in_value.
-
#initial_arguments ⇒ Object
Returns the value of attribute initial_arguments.
-
#initial_options ⇒ Object
Returns the value of attribute initial_options.
-
#initialized ⇒ Object
Returns the value of attribute initialized.
-
#missing_required_arguments ⇒ Object
Returns the value of attribute missing_required_arguments.
-
#options ⇒ Object
Returns the value of attribute options.
-
#processed_parameters ⇒ Object
Returns the value of attribute processed_parameters.
-
#response ⇒ Object
Returns the value of attribute response.
Attribute Readers collapse
- #base_path ⇒ Object
- #body_arguments ⇒ Object
- #eval_http_path? ⇒ Boolean
- #execute ⇒ Object
- #http_client ⇒ Object
- #http_method ⇒ Object
- #http_path ⇒ Object
- #http_response ⇒ Object
- #http_success_code ⇒ Object
- #logger ⇒ Object
- #path_arguments ⇒ Object
- #query_arguments ⇒ Object
- #relative_path ⇒ Object
- #success? ⇒ Boolean
- #uri_request_path ⇒ Object
Class Method Summary collapse
- .normalize_argument_hash_keys(hash) ⇒ Object
- .normalize_parameter_name(name) ⇒ Object
-
.process_parameter(param, args = { }, args_out = { }, missing_required_arguments = [ ], processed_parameters = { }, default_parameter_send_in_value = DEFAULT_PARAMETER_SEND_IN_VALUE, options = { }) ⇒ Object
A method to expose parameter processing.
- .process_parameters(params, args, options = { }) ⇒ Object
Instance Method Summary collapse
- #after_initialize ⇒ Object
- #after_process_parameters ⇒ Object
- #before_process_parameters ⇒ Object
-
#initialize(args = { }, options = { }) ⇒ BaseRequest
constructor
A new instance of BaseRequest.
- #initialize_attributes ⇒ Object
- #process_parameters(params = parameters, args = @initial_arguments, options = @options) ⇒ Object
Constructor Details
#initialize(args = { }, options = { }) ⇒ BaseRequest
Returns a new instance of BaseRequest.
86 87 88 89 90 91 92 93 94 |
# File 'lib/ubiquity/iconik/api/client/requests/base_request.rb', line 86 def initialize(args = { }, = { }) @initial_arguments = args.dup @initial_options = .dup @options = .dup initialize_attributes if .fetch(:initialize_attributes, true) after_initialize end |
Instance Attribute Details
#arguments ⇒ Object
Returns the value of attribute arguments.
21 22 23 |
# File 'lib/ubiquity/iconik/api/client/requests/base_request.rb', line 21 def arguments @arguments end |
#body ⇒ Object
164 165 166 167 168 169 170 171 172 173 174 |
# File 'lib/ubiquity/iconik/api/client/requests/base_request.rb', line 164 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 |
#client ⇒ Object
Returns the value of attribute client.
21 22 23 |
# File 'lib/ubiquity/iconik/api/client/requests/base_request.rb', line 21 def client @client end |
#default_parameter_send_in_value ⇒ Object
Returns the value of attribute default_parameter_send_in_value.
21 22 23 |
# File 'lib/ubiquity/iconik/api/client/requests/base_request.rb', line 21 def default_parameter_send_in_value @default_parameter_send_in_value end |
#initial_arguments ⇒ Object
Returns the value of attribute initial_arguments.
21 22 23 |
# File 'lib/ubiquity/iconik/api/client/requests/base_request.rb', line 21 def initial_arguments @initial_arguments end |
#initial_options ⇒ Object
Returns the value of attribute initial_options.
21 22 23 |
# File 'lib/ubiquity/iconik/api/client/requests/base_request.rb', line 21 def @initial_options end |
#initialized ⇒ Object
Returns the value of attribute initialized.
21 22 23 |
# File 'lib/ubiquity/iconik/api/client/requests/base_request.rb', line 21 def initialized @initialized end |
#missing_required_arguments ⇒ Object
Returns the value of attribute missing_required_arguments.
21 22 23 |
# File 'lib/ubiquity/iconik/api/client/requests/base_request.rb', line 21 def missing_required_arguments @missing_required_arguments end |
#options ⇒ Object
Returns the value of attribute options.
21 22 23 |
# File 'lib/ubiquity/iconik/api/client/requests/base_request.rb', line 21 def @options end |
#parameters ⇒ Object
196 197 198 |
# File 'lib/ubiquity/iconik/api/client/requests/base_request.rb', line 196 def parameters @parameters ||= self.class::PARAMETERS.dup end |
#path ⇒ Object
The URI Path
205 206 207 208 209 210 211 |
# File 'lib/ubiquity/iconik/api/client/requests/base_request.rb', line 205 def path @path ||= begin _path = File.join(base_path, (eval_http_path? ? eval(%("#{http_path}"), binding, __FILE__, __LINE__) : http_path)) _path.concat('/') if !_path.end_with?('/') && http_path.end_with?('/') _path end end |
#processed_parameters ⇒ Object
Returns the value of attribute processed_parameters.
21 22 23 |
# File 'lib/ubiquity/iconik/api/client/requests/base_request.rb', line 21 def processed_parameters @processed_parameters end |
#query ⇒ Object
220 221 222 223 224 |
# File 'lib/ubiquity/iconik/api/client/requests/base_request.rb', line 220 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 |
#response ⇒ Object
Returns the value of attribute response.
21 22 23 |
# File 'lib/ubiquity/iconik/api/client/requests/base_request.rb', line 21 def response @response end |
Class Method Details
.normalize_argument_hash_keys(hash) ⇒ Object
26 27 28 29 |
# File 'lib/ubiquity/iconik/api/client/requests/base_request.rb', line 26 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) ⇒ Object
31 32 33 |
# File 'lib/ubiquity/iconik/api/client/requests/base_request.rb', line 31 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 = { }) ⇒ Object
A method to expose parameter processing
will be placed into this array will be set if the :send_in key is not found
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/ubiquity/iconik/api/client/requests/base_request.rb', line 60 def self.process_parameter(param, args = { }, args_out = { }, missing_required_arguments = [ ], processed_parameters = { }, default_parameter_send_in_value = DEFAULT_PARAMETER_SEND_IN_VALUE, = { }) args = normalize_argument_hash_keys(args) || { } if .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 } end |
.process_parameters(params, args, options = { }) ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/ubiquity/iconik/api/client/requests/base_request.rb', line 35 def self.process_parameters(params, args, = { }) args = normalize_argument_hash_keys(args) || { } args_out = [:arguments_out] || { } default_parameter_send_in_value = [:default_parameter_send_in_value] || DEFAULT_PARAMETER_SEND_IN_VALUE processed_parameters = [:processed_parameters] || { } missing_required_arguments = [: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 |
Instance Method Details
#after_initialize ⇒ Object
96 97 98 |
# File 'lib/ubiquity/iconik/api/client/requests/base_request.rb', line 96 def after_initialize process_parameters if initialized end |
#after_process_parameters ⇒ Object
142 143 144 |
# File 'lib/ubiquity/iconik/api/client/requests/base_request.rb', line 142 def after_process_parameters # TO BE IMPLEMENTED IN CHILD CLASS end |
#base_path ⇒ Object
156 157 158 |
# File 'lib/ubiquity/iconik/api/client/requests/base_request.rb', line 156 def base_path @base_path ||= self.class::HTTP_BASE_PATH end |
#before_process_parameters ⇒ Object
138 139 140 |
# File 'lib/ubiquity/iconik/api/client/requests/base_request.rb', line 138 def before_process_parameters # TO BE IMPLEMENTED IN CHILD CLASS end |
#body_arguments ⇒ Object
160 161 162 |
# File 'lib/ubiquity/iconik/api/client/requests/base_request.rb', line 160 def body_arguments @body_arguments ||= arguments.dup.delete_if { |k,_| processed_parameters[k][:send_in] != :body } end |
#eval_http_path? ⇒ Boolean
184 185 186 |
# File 'lib/ubiquity/iconik/api/client/requests/base_request.rb', line 184 def eval_http_path? @eval_http_path end |
#execute ⇒ Object
242 243 244 |
# File 'lib/ubiquity/iconik/api/client/requests/base_request.rb', line 242 def execute @response = http_client.call_method(http_method, { :path => path, :query => query, :body => body }, ) if client end |
#http_client ⇒ Object
234 235 236 |
# File 'lib/ubiquity/iconik/api/client/requests/base_request.rb', line 234 def http_client client.http_client end |
#http_method ⇒ Object
192 193 194 |
# File 'lib/ubiquity/iconik/api/client/requests/base_request.rb', line 192 def http_method @http_method ||= self.class::HTTP_METHOD end |
#http_path ⇒ Object
188 189 190 |
# File 'lib/ubiquity/iconik/api/client/requests/base_request.rb', line 188 def http_path @http_path ||= self.class::HTTP_PATH.dup end |
#http_response ⇒ Object
238 239 240 |
# File 'lib/ubiquity/iconik/api/client/requests/base_request.rb', line 238 def http_response @http_response ||= http_client.response.dup rescue nil end |
#http_success_code ⇒ Object
148 149 150 |
# File 'lib/ubiquity/iconik/api/client/requests/base_request.rb', line 148 def http_success_code @http_success_code end |
#initialize_attributes ⇒ Object
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 |
# File 'lib/ubiquity/iconik/api/client/requests/base_request.rb', line 100 def initialize_attributes @client = [:client] @missing_required_arguments = [ ] @default_parameter_send_in_value = [:default_parameter_send_in_value] || self.class::DEFAULT_PARAMETER_SEND_IN_VALUE @processed_parameters = { } @arguments = { } @eval_http_path = .fetch(:eval_http_path, true) @base_path = [:base_path] @parameters = [:parameters] @http_method = [:http_method] @http_path = [:http_path] ||= [:path_raw] @http_success_code = [:http_success_code] ||= HTTP_SUCCESS_CODE @path = [:path] @path_arguments = nil @query = [:query] @query_arguments = nil @body = [:body] @body_arguments = nil @relative_path = nil @response = nil @initialized = true end |
#logger ⇒ Object
180 181 182 |
# File 'lib/ubiquity/iconik/api/client/requests/base_request.rb', line 180 def logger @logger ||= client.logger end |
#path_arguments ⇒ Object
213 214 215 216 217 218 |
# File 'lib/ubiquity/iconik/api/client/requests/base_request.rb', line 213 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) ⇒ Object
130 131 132 133 134 135 136 |
# File 'lib/ubiquity/iconik/api/client/requests/base_request.rb', line 130 def process_parameters(params = parameters, args = @initial_arguments, = @options) before_process_parameters unless .fetch(:skip_before_process_parameters, false) self.class.process_parameters(params, args, .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 .fetch(:skip_after_process_parameters, false) end |
#query_arguments ⇒ Object
226 227 228 |
# File 'lib/ubiquity/iconik/api/client/requests/base_request.rb', line 226 def query_arguments @query_arguments ||= arguments.dup.delete_if { |k,_| processed_parameters[k][:send_in] != :query } end |
#relative_path ⇒ Object
200 201 202 |
# File 'lib/ubiquity/iconik/api/client/requests/base_request.rb', line 200 def relative_path @relative_path ||= (path.start_with?('/') ? path[1..-1] : path) end |
#success? ⇒ Boolean
246 247 248 249 250 251 |
# File 'lib/ubiquity/iconik/api/client/requests/base_request.rb', line 246 def success? _response = client.http_client.response _response && ( http_success_code.is_a?(Array) ? http_success_code.include?(_response.code) : http_success_code == _response.code ) end |
#uri_request_path ⇒ Object
230 231 232 |
# File 'lib/ubiquity/iconik/api/client/requests/base_request.rb', line 230 def uri_request_path [ path ].concat( [*query].delete_if { |v| v.respond_to?(:empty?) and v.empty? } ).join('?') end |