Sha256: 6ba90ebe7c7afa4ce825e9f4b190669a36cfefd5ebc558d55035a4603a9c9311

Contents?: true

Size: 1.16 KB

Versions: 2

Compression:

Stored size: 1.16 KB

Contents

# frozen_string_literal: true

module Arbetsformedlingen
  module API
    module Values
      class CreateAdPage
        ResponseMessage = KeyStruct.new(:detail, :error_code)

        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)
              ResponseMessage.new(detail: message.last, error_code: nil)
            else
              error_code = message['ErrorCode']
              @valid = false if error_code

              ResponseMessage.new(detail: message['Message'], error_code: error_code)
            end
          end
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
arbetsformedlingen-0.7.0 lib/arbetsformedlingen/api/values/create_ad_page.rb
arbetsformedlingen-0.6.0 lib/arbetsformedlingen/api/values/create_ad_page.rb