Sha256: a7e5c7bb34dead117a63a37277fa700180b0f71450f659f39169d86e304a4d21

Contents?: true

Size: 1.47 KB

Versions: 1

Compression:

Stored size: 1.47 KB

Contents

class Http2::PostRequest < Http2::BaseRequest
  def headers_string
    unless @headers_string
      @headers_string = "#{method} /#{@args[:url]} HTTP/1.1#{@nl}"
      @headers_string << @http2.header_str(headers)
      @headers_string << @nl
      @headers_string << @data
    end

    @headers_string
  end

  def execute
    @data = raw_data

    @http2.mutex.synchronize do
      puts "Http2: Doing post." if @debug

      @conn.write(headers_string)
      return @http2.read_response(self, @args)
    end
  end

private

  def method
    if @args[:method]
      @args[:method].to_s.upcase
    else
      "POST"
    end
  end

  def content_type
    if @args[:content_type]
      @args[:content_type]
    elsif @args[:json]
      "application/json"
    else
      "application/x-www-form-urlencoded"
    end
  end

  def raw_data
    if @args[:json]
      require "json" unless ::Kernel.const_defined?(:JSON)
      @args[:json].to_json
    elsif @args[:post].is_a?(String)
      @args[:post]
    else
      phash = @args[:post] ? @args[:post].clone : {}
      @http2.autostate_set_on_post_hash(phash) if @http2.args[:autostate]
      ::Http2::PostDataGenerator.new(phash).generate
    end
  end

  def headers
    headers_hash = {
      "Content-Length" => @data.bytesize,
      "Content-Type" => content_type
    }
    headers_hash.merge! @http2.default_headers(@args)

    unless headers_hash["Accept"]
      headers_hash["Accept"] = "application/json" if @args[:json]
    end

    headers_hash
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
http2-0.0.32 lib/http2/post_request.rb