Sha256: e48c3eb251c5638d38f6a11fa2ce3c5a684bb9d56ae99be7e383bf05fd3f68c1

Contents?: true

Size: 1.2 KB

Versions: 2

Compression:

Stored size: 1.2 KB

Contents

require 'active_support/core_ext/object/to_query'

module RspecApiDocumentation
  class Curl < Struct.new(:method, :path, :data, :headers)
    attr_accessor :host

    def output(config_host)
      self.host = config_host
      send(method.downcase)
    end

    def post
      "curl \"#{url}\" #{post_data} -X POST #{headers}"
    end

    def get
      "curl \"#{url}#{get_data}\" -X GET #{headers}"
    end

    def put
      "curl \"#{url}\" #{post_data} -X PUT #{headers}"
    end

    def delete
      "curl \"#{url}\" #{post_data} -X DELETE #{headers}"
    end

    def head
      "curl \"#{url}#{get_data}\" -X HEAD #{headers}"
    end

    def patch
      "curl \"#{url}\" #{post_data} -X PATCH #{headers}"
    end

    def url
      "#{host}#{path}"
    end

    def headers
      super.map do |k, v|
        "-H \"#{format_header(k, v)}\""
      end.join(" ")
    end

    def get_data
      "?#{data}" unless data.blank?
    end

    def post_data
      escaped_data = data.to_s.gsub("'", "\\u0027")
      "-d '#{escaped_data}'"
    end

    private
    def format_header(header, value)
      formatted_header = header.gsub(/^HTTP_/, '').titleize.split.join("-")
      "#{formatted_header}: #{value}"
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
rspec_api_documentation-0.9.2 lib/rspec_api_documentation/curl.rb
rspec_api_documentation-0.9.1 lib/rspec_api_documentation/curl.rb