Sha256: 68fc2bb3304b639ee372b0c1d5e46e364c8131d1ca10667d4b8d7f117bf24dbf

Contents?: true

Size: 1.4 KB

Versions: 3

Compression:

Stored size: 1.4 KB

Contents

require 'active_support/core_ext/hash/indifferent_access'

require 'expedition/status'

module Expedition
  class Response

    attr_reader :body

    attr_reader :raw

    attr_reader :status

    def self.parse(raw)
      normalized = normalize(raw)
      normalized = yield normalized if block_given?
      new(normalized, raw)
    end

    def initialize(normalized, raw)
      @body   = normalized
      @raw    = raw
      @status = Status.new(raw['STATUS'])
    end

    delegate :success?, :info?, :warn?, :error?, :fatal?, :ok?,
             :executed_at, to: :status

    delegate :[], :to_h, :to_hash, :to_a, :to_ary, to: :body

    def respond_to_missing?(method_name, include_private = false)
      body.respond_to?(method_name) || super
    end

    def method_missing(method_name, *arguments, &block)
      if respond_to_missing?(method_name)
        body.send(method_name, *arguments, &block)
      else
        super
      end
    end

    private

    def self.normalize(value)
      case value
      when Hash
        Hash[value.collect { |k, v| [normalize_key(k), normalize(v)] }].with_indifferent_access
      when Array
        value.collect { |v| normalize(v) }
      else
        value
      end
    end

    def self.normalize_key(key)
      key = key.gsub(/(\w)%/, '\\1_percent').gsub('%', 'percent').gsub(/[^\w]+/, ' ')
      key.strip.gsub(/\s+/, '_').downcase
    end
  end # Response
end # Expedition

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
expedition-0.4.0 lib/expedition/response.rb
expedition-0.3.0 lib/expedition/response.rb
expedition-0.2.0 lib/expedition/response.rb