Sha256: b775d8bc727b1870574d1830bc5fe5c8fb776ca3b4abf4ae49d2eb2e42214310

Contents?: true

Size: 1.12 KB

Versions: 10

Compression:

Stored size: 1.12 KB

Contents

module RemoteResource
  class ResponseHandeling

    attr_reader :resource, :response, :connection_options

    def initialize(resource, response, connection_options = {})
      @resource           = resource
      @response           = response
      @connection_options = connection_options
    end

    def perform
      if response.success?
        if resource.respond_to? :new
          resource.build_resource_from_response response
        else
          resource
        end
      elsif errors?
        if resource.respond_to? :new
          #
        else
          # assign response
          resource.assign_errors response.parsed_response_body, root_element
        end
      else
        # nil
      end
    end

    def errors?
      parsed_response_body = response.parsed_response_body

      if parsed_response_body
        parsed_response_body.has_key?("errors") || parsed_response_body.fetch(root_element.to_s, nil).has_key?("errors")
      else
        false
      end
    end

    private

    def root_element
      connection_options[:root_element].presence || resource.connection_options.root_element.presence
    end

  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
ddy_remote_resource-0.4.11 lib/remote_resource/response_handeling.rb
ddy_remote_resource-0.4.10 lib/remote_resource/response_handeling.rb
ddy_remote_resource-0.4.9 lib/remote_resource/response_handeling.rb
ddy_remote_resource-0.4.8 lib/remote_resource/response_handeling.rb
ddy_remote_resource-0.4.7 lib/remote_resource/response_handeling.rb
ddy_remote_resource-0.4.6 lib/remote_resource/response_handeling.rb
ddy_remote_resource-0.4.5 lib/remote_resource/response_handeling.rb
ddy_remote_resource-0.4.4 lib/remote_resource/response_handeling.rb
ddy_remote_resource-0.4.3 lib/remote_resource/response_handeling.rb
ddy_remote_resource-0.4.2 lib/remote_resource/response_handeling.rb