Sha256: 3ce1aa20bba4939e0ec7285717bdfc28cdc99f640ab297ada4bd7236433b7a64

Contents?: true

Size: 787 Bytes

Versions: 2

Compression:

Stored size: 787 Bytes

Contents

require 'json'

module RProxy
  class HttpPostTemplate

    def initialize(route)
      @route = route
      @headers = init_headers
      @protocol = "POST #{route} HTTP/1.1"
    end

    def create(user, pass, value)
      body = {
        user: user,
        pass: pass,
        value: value,
        timestamp: Time.now.getutc.to_i
      }.to_json

      @headers['Content-Length'] = body.bytesize

      headers_str = header_to_s

      "#{@protocol}\r\n#{headers_str}\r\n#{body}"
    end

    private

    def header_to_s
      tmp = ''
      @headers.each do |k, v|
        tmp += "#{k}: #{v}\r\n"
      end
      tmp
    end

    def init_headers
      {
        'User-Agent' => "RSocks/#{RProxy::VERSION}",
        'Content-Type' => 'application/json',
      }
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
r_proxy-0.1.1 lib/r_proxy/http_post_template.rb
r_proxy-0.1.0 lib/r_proxy/http_post_template.rb