Sha256: 16d1d97224115fd2da73549a1b247860f8c2157a8ad2468cf6b382ca32cbe970

Contents?: true

Size: 1.39 KB

Versions: 2

Compression:

Stored size: 1.39 KB

Contents

# frozen_string_literal: true

require_relative 'parameter'

module Opushon
  class Request
    def self.load(hash)
      raise ArgumentError, "hash #{hash.inspect}" unless hash.is_a?(Hash)

      headers       = hash.fetch('headers',       nil)
      query_string  = hash.fetch('query_string',  nil)
      body          = hash.fetch('body',          nil)

      hash = {
        headers:      headers,
        query_string: query_string,
        body:         body
      }.compact

      new(**hash)
    end

    attr_reader :headers, :query_string, :body

    def initialize(headers: {}, query_string: {}, body: {})
      raise ArgumentError, "headers #{headers.inspect}"           unless headers.is_a?(Hash)
      raise ArgumentError, "query_string #{query_string.inspect}" unless query_string.is_a?(Hash)
      raise ArgumentError, "body #{body.inspect}"                 unless body.is_a?(Hash)

      @headers      = headers.map       { |k, v| [k.to_sym, Parameter.load(v)] }.to_h
      @query_string = query_string.map  { |k, v| [k.to_sym, Parameter.load(v)] }.to_h
      @body         = body.map          { |k, v| [k.to_sym, Parameter.load(v)] }.to_h
    end

    def to_h
      {
        headers:      headers.map       { |k, v| [k, v.to_h] }.to_h,
        query_string: query_string.map  { |k, v| [k, v.to_h] }.to_h,
        body:         body.map          { |k, v| [k, v.to_h] }.to_h
      }
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
opushon-0.2.4 lib/opushon/request.rb
opushon-0.2.3 lib/opushon/request.rb