Sha256: 1af8704bd20907b26a83aabc103f60ab62c0f204c1405c9de830873f51dd199e

Contents?: true

Size: 1017 Bytes

Versions: 3

Compression:

Stored size: 1017 Bytes

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 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
      "-d \"#{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

3 entries across 3 versions & 1 rubygems

Version Path
rspec_api_documentation-0.8.0 lib/rspec_api_documentation/curl.rb
rspec_api_documentation-0.7.1 lib/rspec_api_documentation/curl.rb
rspec_api_documentation-0.6.0 lib/rspec_api_documentation/curl.rb