diff --git a/angus-remote.gemspec b/angus-remote.gemspec index 215d667..d33d98a 100644 --- a/angus-remote.gemspec +++ b/angus-remote.gemspec @@ -22,6 +22,7 @@ Gem::Specification.new do |spec| spec.add_dependency('angus-sdoc', '~> 0.0', '>= 0.0.6') spec.add_dependency('multipart-post', '>= 2.2') spec.add_dependency('persistent_http', '~> 2.0.3') + spec.add_dependency('activesupport') spec.add_development_dependency('ci_reporter', '~> 2.0.0') spec.add_development_dependency('fakefs') diff --git a/lib/angus/remote/remote_response.rb b/lib/angus/remote/remote_response.rb deleted file mode 100644 index 27beffd..0000000 --- a/lib/angus/remote/remote_response.rb +++ /dev/null @@ -1,35 +0,0 @@ -require_relative 'response/hash' - -module Angus - module Remote - - # A service's response - # - # Acts as an array to store information at HTTP level, like the status_code - class RemoteResponse < Struct - include Angus::Remote::Response::Hash - - attr_accessor :status - attr_accessor :status_code - attr_accessor :messages - attr_accessor :http_response_info - - def initialize(*args) - @http_response_info = {} - - super(*args) - end - - def to_hash - { - http_status_code: @http_response_info[:status_code], - body: @http_response_info[:body], - service_name: @http_response_info[:service_name], - operation_name: @http_response_info[:operation_name] - } - end - - end - - end -end diff --git a/lib/angus/remote/representation.rb b/lib/angus/remote/representation.rb deleted file mode 100644 index f48c172..0000000 --- a/lib/angus/remote/representation.rb +++ /dev/null @@ -1,18 +0,0 @@ -require_relative 'response/hash' - -module Angus - module Remote - - class Representation - include Angus::Remote::Response::Hash - - attr_accessor :elements - - def initialize - @http_response_info = {} - @elements = {} - end - end - - end -end \ No newline at end of file diff --git a/lib/angus/remote/response/builder.rb b/lib/angus/remote/response/builder.rb index c3ce613..1744c16 100644 --- a/lib/angus/remote/response/builder.rb +++ b/lib/angus/remote/response/builder.rb @@ -2,14 +2,11 @@ require 'date' require 'json' -require_relative '../message' -require_relative '../remote_response' - -require_relative 'hash' +require 'active_support/core_ext/string/inflections' +require_relative '../message' require_relative '../service_directory' - # TODO: move to another gem and possibly change its name require_relative '../../unmarshalling' @@ -57,16 +54,6 @@ def self.build_from_definition(status_code, body, service_code_name, service_ver json_response = JSON(body) fields = {} - - fields[:status] = json_response['status'] - fields[:status_code] = status_code - fields[:messages] = self.build_messages(json_response['messages']) - fields[:body] = body - fields[:service_code_name] = service_code_name - fields[:service_version] = service_version - fields[:operation_namespace] = operation_namespace - fields[:operation_code_name] = operation_code_name - representations_hash = self.representations_hash(representations) glossary_terms_hash = glossary.terms_hash @@ -76,15 +63,49 @@ def self.build_from_definition(status_code, body, service_code_name, service_ver fields[element.name.to_sym] = element_value end - response_class = Angus::Remote::RemoteResponse.new(*fields.keys, :elements) - response = response_class.new(*fields.values, fields.transform_keys(&:to_s)) + # Esto tira un warning cada vez que el nombre de la clase se repite: + # warning: redefining constant Struct::SomethingResponse + # Evaluar si conviene hacer "repsonse_class = " o bien "ResponseClass = " + response_class = Struct.new("#{operation_code_name.camelcase}Response", *fields.keys) do + attr_reader :status, :status_code, :messages, :http_response_info + + def initialize(status, status_code, messages, http_response_info, *args) + @status = status + @status_code = status_code + @messages = messages + @http_response_info = http_response_info - response.http_response_info[:status_code] = status_code - response.http_response_info[:body] = body - response.http_response_info[:service_code_name] = service_code_name - response.http_response_info[:service_version] = service_version - response.http_response_info[:operation_namespace] = operation_namespace - response.http_response_info[:operation_code_name] = operation_code_name + super(*args) + end + + def to_hash + { + http_status_code: @http_response_info[:status_code], + body: @http_response_info[:body], + service_name: @http_response_info[:service_name], + operation_name: @http_response_info[:operation_name] + } + end + + def elements + to_h.transform_keys(&:to_s) + end + end + + response = response_class.new( + json_response['status'], + status_code, + build_messages(json_response['messages']), + { + status_code: status_code, + body: body, + service_name: service_code_name, + service_version: service_version, + operation_namespace: operation_namespace, + operation_name: operation_code_name + }, + *fields.values + ) fields = nil response_class = nil @@ -216,8 +237,12 @@ def self.build_from_representation(hash_value, type, representations, glossary_t fields[field.name.to_sym] = field_value end - representation_class = Struct.new(*fields.keys, :elements) - representation_object = representation_class.new(*fields.values, fields.transform_keys(&:to_s)) + representation_class = Struct.new("#{type.to_s.camelcase}", *fields.keys) do + def elements + to_h.transform_keys(&:to_s) + end + end + representation_object = representation_class.new(*fields.values) fields = nil representation_class = nil @@ -263,8 +288,12 @@ def self.build_from_variable_fields(variable_fields_hash) fields[key_name.to_sym] = field_value end - representation_class = Struct.new(*fields.keys, :elements) - representation_object = representation_class.new(*fields.values, fields.transform_keys(&:to_s)) + representation_class = Struct.new(*fields.keys) do + def elements + to_h.transform_keys(&:to_s) + end + end + representation_object = representation_class.new(*fields.values) fields = nil representation_class = nil diff --git a/lib/angus/remote/response/hash.rb b/lib/angus/remote/response/hash.rb deleted file mode 100644 index f783c64..0000000 --- a/lib/angus/remote/response/hash.rb +++ /dev/null @@ -1,49 +0,0 @@ -module Angus - module Remote - module Response - - module Hash - - def elements - @elements ||= {} - end - - # Creates a hash base on the object - # - # The object must have an instance variable @elements that is a hash - # that keys => element name, value => element value - def to_hash - hash = {} - - elements.each do |name, value| - if value.is_a?(Angus::Remote::Response::Hash) - hash[name] = value.to_hash - elsif value.is_a?(Array) - hash[name] = build_hash_from_array(value) - else - hash[name] = value - end - end - - hash - end - - private - - def build_hash_from_array(elements) - elements.map do |element| - if element.is_a?(Angus::Remote::Response::Hash) - element.to_hash - elsif element.is_a?(Array) - build_hash_from_array(element) - else - element - end - end - end - - end - - end - end -end \ No newline at end of file diff --git a/spec/angus/remote/response/builder_spec.rb b/spec/angus/remote/response/builder_spec.rb index d81f2bb..00832e9 100644 --- a/spec/angus/remote/response/builder_spec.rb +++ b/spec/angus/remote/response/builder_spec.rb @@ -2,8 +2,6 @@ require 'angus/remote/response/builder' -require 'angus/remote/remote_response' - describe Angus::Remote::Response::Builder do subject(:builder) { Angus::Remote::Response::Builder } diff --git a/spec/angus/remote/response/hash_spec.rb b/spec/angus/remote/response/hash_spec.rb deleted file mode 100644 index c7a69f1..0000000 --- a/spec/angus/remote/response/hash_spec.rb +++ /dev/null @@ -1,56 +0,0 @@ -require 'spec_helper' - -require 'angus/remote/response/hash' - -describe Angus::Remote::Response::Hash do - - let(:response_class) do - Class.new do - include Angus::Remote::Response::Hash - - def initialize(elements) - @elements = elements - end - - end - end - - subject(:response) { response_class.new(elements) } - - describe '#to_hash' do - - context 'when a elements has a single level' do - let(:elements) { { :id => rand(1_000), :customer_fields => %w[a b c] } } - - it 'returns the flat elements' do - expect(response.to_hash).to eq(elements) - end - end - - context 'when a nested responses elements as a hash' do - let(:brand_elements) { { :id => rand(1_000), :name => 'Acme' } } - let(:elements) { { :id => rand(1_000), :brand => response_class.new(brand_elements) } } - - it 'returns the flat elements' do - expect(response.to_hash).to include(:brand => brand_elements) - end - end - - context 'when a nested responses elements as an array' do - let(:division_elements) { { :id => rand(1_000), :name => 'Acme Division' } } - let(:commission_elements) { { :id => rand(1_000), :name => 'Acme Commission', - :divisions => [response_class.new(division_elements)] } } - - let(:elements) { { :id => rand(1_000), - :commissions => [[commission_elements[:id], - response_class.new(commission_elements)]] } } - - it 'returns the flat elements' do - commission = commission_elements.merge(:divisions => [division_elements]) - expect(response.to_hash).to include(:commissions => [[commission[:id], commission]]) - end - end - - end - -end \ No newline at end of file