Sha256: 52690110691ca5e04c756649541529788bd4fee9925cd9956254585d277b9485

Contents?: true

Size: 1.17 KB

Versions: 1

Compression:

Stored size: 1.17 KB

Contents

# -*- encoding : utf-8 -*-
require 'hashie/mash'

module Pacto
  class PactoRequest
    # FIXME: Need case insensitive header lookup, but case-sensitive storage
    attr_accessor :headers, :body, :method, :uri

    def initialize(data)
      mash = Hashie::Mash.new data
      @headers = mash.headers.nil? ? {} : mash.headers
      @body    = mash.body
      @method  = mash[:method]
      @uri     = mash.uri
      normalize
    end

    def to_hash
      {
        method: method,
        uri: uri,
        headers: headers,
        body: body
      }
    end

    def to_s
      string = Pacto::UI.colorize_method(method)
      string << " #{relative_uri}"
      string << " with body (#{body.bytesize} bytes)" if body
      string
    end

    def relative_uri
      uri.to_s.tap do |s|
        s.slice!(uri.normalized_site)
      end
    end

    def parsed_body
      if body.is_a?(String) && content_type == 'application/json'
        JSON.parse(body)
      else
        body
      end
    rescue
      body
    end

    def content_type
      headers['Content-Type']
    end

    def normalize
      @method = @method.to_s.downcase.to_sym
      @uri = @uri.normalize if @uri
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
pacto-0.4.0.rc1 lib/pacto/core/pacto_request.rb