Class: RubyAem::Client
- Inherits:
-
Object
- Object
- RubyAem::Client
- Defined in:
- lib/ruby_aem/client.rb
Overview
Client class makes Swagger AEM API calls and handles the response as configured in conf/spec.yaml .
Instance Method Summary collapse
-
#add_optional_param(key, value, params, info) ⇒ Object
Add optional param into params list.
-
#call(clazz, action, info) ⇒ Object
Make an API call using the relevant Swagger AEM API client.
-
#handle(data, status_code, headers, responses, info) ⇒ Object
Handle a response based on status code and a given list of response specifications.
-
#initialize(apis, spec) ⇒ Object
constructor
Initialise a client.
Constructor Details
#initialize(apis, spec) ⇒ Object
Initialise a client.
35 36 37 38 |
# File 'lib/ruby_aem/client.rb', line 35 def initialize(apis, spec) @apis = apis @spec = spec end |
Instance Method Details
#add_optional_param(key, value, params, info) ⇒ Object
Add optional param into params list.
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/ruby_aem/client.rb', line 86 def add_optional_param(key, value, params, info) # if there is no value in optional param spec, # then only add optional param that is set in info if !value if info.key? key.to_sym params[-1][key.to_sym] = info[key.to_sym] end # if value is provided in optional param spec, # then apply variable interpolation the same way as required param else if value.class == String if value == '__FILE__' File.open("#{info[:file_path]}/#{info[:package_name]}-#{info[:package_version]}.zip", 'r') { |file| params[-1][key.to_sym] = file } else params[-1][key.to_sym] = value % info end else params[-1][key.to_sym] = value end end end |
#call(clazz, action, info) ⇒ Object
Make an API call using the relevant Swagger AEM API client. Clazz and action parameters are used to identify the action, API, and params from ruby_aem specification, alongside the response handlers.
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/ruby_aem/client.rb', line 48 def call(clazz, action, info) component = clazz.name.downcase.sub('rubyaem::', '') action_spec = @spec[component]['actions'][action] api = @apis[action_spec['api'].to_sym] operation = action_spec['operation'] params = [] required_params = action_spec['params']['required'] || {} required_params.each { |key, value| params.push(value % info) } params.push({}) optional_params = action_spec['params']['optional'] || {} optional_params.each { |key, value| add_optional_param(key, value, params, info) } base_responses = @spec[component]['responses'] || {} action_responses = action_spec['responses'] || {} responses = base_responses.merge(action_responses) begin method = RubyAem::Swagger.operation_to_method(operation) data, status_code, headers = api.send("#{method}_with_http_info", *params) handle(data, status_code, headers, responses, info) rescue SwaggerAemClient::ApiError => err handle(err.response_body, err.code, err.response_headers, responses, info) end end |
#handle(data, status_code, headers, responses, info) ⇒ Object
Handle a response based on status code and a given list of response specifications. If none of the response specifications contains the status code, a failure result will then be returned.
120 121 122 123 124 125 126 127 128 |
# File 'lib/ruby_aem/client.rb', line 120 def handle(data, status_code, headers, responses, info) if responses.key?(status_code) response_spec = responses[status_code] handler = response_spec['handler'] result = Handlers.send(handler, data, status_code, headers, response_spec, info) else result = Result.new('failure', "Unexpected response\nstatus code: #{status_code}\nheaders: #{headers}\ndata: #{data}") end end |