Sha256: c7a787a4040b40e1ca50e420fb2fdf43f27f1a7ef2f87f63aa3a681c9ff0b3dd

Contents?: true

Size: 1.28 KB

Versions: 1

Compression:

Stored size: 1.28 KB

Contents

# -*- encoding : utf-8 -*-
module Pacto
  class RequestClause < Pacto::Dash
    include Logger
    property :host # required?
    property :http_method, required: true
    property :schema, default: {}
    property :path, default: '/'
    property :headers
    property :params, default: {}
    property :request_pattern_provider, default: Pacto::RequestPattern

    def initialize(definition)
      mash = Hashie::Mash.new definition
      mash['http_method'] = normalize(mash['http_method'])
      super mash
    end

    def http_method=(method)
      normalize(method)
    end

    def pattern
      @pattern ||= request_pattern_provider.for(self)
    end

    def uri(values = {})
      values ||= {}
      uri_template = pattern.uri_template
      missing_keys = uri_template.keys - values.keys
      values[:scheme] = 'http' if missing_keys.delete(:scheme)
      values[:server] = 'localhost' if missing_keys.delete(:server)
      logger.warn "Missing keys for building a complete URL: #{missing_keys.inspect}" unless missing_keys.empty?
      Addressable::URI.heuristic_parse(uri_template.expand(values)).tap do |uri|
        uri.query_values = params unless params.nil? || params.empty?
      end
    end

    private

    def normalize(method)
      method.to_s.downcase.to_sym
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
pacto-0.4.0.rc1 lib/pacto/request_clause.rb