Sha256: d0cb83ae4ab963d3ee45a9951ed020724f489d9c0e9ed28b32a957dff81ae01a

Contents?: true

Size: 1.17 KB

Versions: 1

Compression:

Stored size: 1.17 KB

Contents

require 'thron/string_extensions'

module Thron
  using StringExtensions
  class Response
    attr_accessor :body
    attr_reader :http_code, :result_code, :sso_code, :total, :other_results, :error

    ERROR_KEY = 'errorDescription'
    ID_REGEX  = /\A\w{8}-\w{4}-\w{4}-\w{4}-\w{12}\Z/

    def initialize(raw_data)
      @http_code     = raw_data.code
      @body          = fetch(raw_data)
      @result_code   = @body.delete('resultCode')
      @sso_code      = @body.delete('ssoCode')
      @total         = @body.delete('totalResults')
      @other_results = @body.delete('otherResults') { false }
      @error         = @body.delete(ERROR_KEY)
    end

    def extra(options = {})
      attribute = options[:attribute].to_s
      name = attribute.snakecase 
      self.class.send(:attr_reader, name)
      instance_variable_set(:"@#{name}", body.delete(attribute))
    end

    def is_200?
      (@http_code.to_i / 100) == 2
    end

    private

    def fetch(raw_data)
      case(parsed = raw_data.parsed_response)
      when Hash
        parsed
      when ID_REGEX
        { id: parsed }
      when String
        { ERROR_KEY => parsed }
      else
        {}
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
thron-0.7.0 lib/thron/response.rb