Sha256: 3eff79d21ec22d380b3b70e249fe6b8d6633f4776d507cd9631317c43346513c

Contents?: true

Size: 901 Bytes

Versions: 4

Compression:

Stored size: 901 Bytes

Contents

module Arbetsformedlingen
  class Response
    attr_reader :code, :messages, :body, :request_body

    def initialize(httparty_response, request_boby)
      @code = httparty_response.code
      @body = httparty_response.body
      @request_body = request_body
      @valid = @code == 202
      @messages = build_messages(httparty_response.to_a)
    end

    def valid?
      @valid
    end

    private

    def build_messages(messages)
      messages.map do |message|
        # HTTParty returns an array if there is only one key-value pair in the response
        # so we need to check for it here and normalize
        if message.is_a?(Array)
          { message: message.last, error_code: nil }
        else
          error_code = message['ErrorCode']
          @valid = false if error_code

          { message: message['Message'], error_code: error_code }
        end
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
arbetsformedlingen-0.2.0 lib/arbetsformedlingen/response.rb
arbetsformedlingen-0.1.2 lib/arbetsformedlingen/response.rb
arbetsformedlingen-0.1.1 lib/arbetsformedlingen/response.rb
arbetsformedlingen-0.1.0 lib/arbetsformedlingen/response.rb