Module: Cadinsor::Extensions
- Defined in:
- lib/cadinsor/extensions.rb,
lib/cadinsor/extensions/request_error.rb
Defined Under Namespace
Classes: RequestError
Instance Method Summary collapse
- #cadinsor_flatten_params(parameter_hash, key_prefix = "") ⇒ Object
- #cadinsor_rescue(message) ⇒ Object
- #cadinsor_validate_app(app_id) ⇒ Object
- #cadinsor_validate_key(api_key) ⇒ Object
- #cadinsor_validate_signature(params, signature, app_id_param_name, signature_param_name) ⇒ Object
- #check_request_with_cadinsor(options = {}) ⇒ Object
Instance Method Details
#cadinsor_flatten_params(parameter_hash, key_prefix = "") ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/cadinsor/extensions.rb', line 15 def cadinsor_flatten_params(parameter_hash, key_prefix = "") flattened_hash = {} parameter_hash.keys.each do |key| if parameter_hash[key].is_a? Hash key_prefix == "" ? flatten_prefix = key.to_s : flatten_prefix = key_prefix + "_" + key.to_s flattened_hash.merge! cadinsor_flatten_params(parameter_hash[key], flatten_prefix) else key_prefix == "" ? flattened_hash.merge!({key.to_s => parameter_hash[key]}) : flattened_hash.merge!({(key_prefix.to_s + "_" + key.to_s) => parameter_hash[key]}) end end flattened_hash end |
#cadinsor_rescue(message) ⇒ Object
35 36 37 38 39 40 41 |
# File 'lib/cadinsor/extensions.rb', line 35 def cadinsor_rescue() @cadinsor_error_message = respond_to do |format| format.json {render "cadinsor/application/cadinsor_error_response", format: 'json'} format.xml {render "/cadinsor/application/cadinsor_error_response", format: 'xml'} end end |
#cadinsor_validate_app(app_id) ⇒ Object
43 44 45 46 47 |
# File 'lib/cadinsor/extensions.rb', line 43 def cadinsor_validate_app(app_id) raise Cadinsor::Extensions::RequestError.new "Invalid Request. Client App id is not present in request." if app_id.to_s == "" app = Cadinsor::ClientApp.find_by_id(app_id) raise Cadinsor::Extensions::RequestError.new "Invalid Request. Client App is not valid." if !app end |
#cadinsor_validate_key(api_key) ⇒ Object
28 29 30 31 32 33 |
# File 'lib/cadinsor/extensions.rb', line 28 def cadinsor_validate_key(api_key) raise Cadinsor::Extensions::RequestError.new "Invalid Request. Key not present in request." if api_key.to_s == "" key = Cadinsor::ApiKey.find_by_key(api_key) raise Cadinsor::Extensions::RequestError.new "Invalid Request. Key is not valid." if !key raise Cadinsor::Extensions::RequestError.new "Invalid Request. Key has expired. Please use a new key." if key.expired? end |
#cadinsor_validate_signature(params, signature, app_id_param_name, signature_param_name) ⇒ Object
49 50 51 52 53 54 55 56 57 58 |
# File 'lib/cadinsor/extensions.rb', line 49 def cadinsor_validate_signature(params, signature, app_id_param_name, signature_param_name) raise Cadinsor::Extensions::RequestError.new "Invalid Request. Request signature is not present." if signature.to_s == "" req_string = "" params.keys.sort.each do |key| req_string = req_string + params[key] unless [signature_param_name.to_s, "controller", "action", "format"].include? key.to_s end req_string = req_string + Cadinsor::ClientApp.find_by_id(params[app_id_param_name.to_s]).secret request_hash = Digest::SHA2.hexdigest(req_string) raise Cadinsor::Extensions::RequestError.new "Invalid Request. Request Signature is not valid." if signature != request_hash end |
#check_request_with_cadinsor(options = {}) ⇒ Object
4 5 6 7 8 9 10 11 12 13 |
# File 'lib/cadinsor/extensions.rb', line 4 def check_request_with_cadinsor( = {}) [:target_params] ? target_params = [:target_params] : target_params = params target_params = cadinsor_flatten_params(target_params) api_key = target_params[Cadinsor::Engine.config.api_key_param_name.to_s] signature = target_params[Cadinsor::Engine.config.request_signature_param_name.to_s] client_app_id = target_params[Cadinsor::Engine.config.client_app_id_param_name.to_s] cadinsor_validate_key(api_key) if [:ignore_api_key_check] != true cadinsor_validate_app(client_app_id) cadinsor_validate_signature(target_params, signature, Cadinsor::Engine.config.client_app_id_param_name.to_s, Cadinsor::Engine.config.request_signature_param_name.to_s) end |