Class: Clockwork::HTTP

Inherits:
Object
  • Object
show all
Defined in:
lib/clockwork/http.rb

Overview

Wrapper around NET/HTTP

Author:

  • James Inman <james@mediaburst.co.uk>

Class Method Summary collapse

Class Method Details

.post(url, data = '', use_ssl = true) ⇒ string

Build a HTTP POST request.

Parameters:

  • url (string)

    URL to POST to

  • data (string) (defaults to: '')

    Body of the POST request.

  • use_ssl (boolean) (defaults to: true)

    Whether to use SSL when making the request.

Returns:

  • (string)

    XML data



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/clockwork/http.rb', line 12

def self.post url, data = '', use_ssl = true
  if use_ssl
    uri = URI.parse "https://#{url}"
    req = Net::HTTP::Post.new( uri.path )

    socket = Net::HTTP.new( uri.host, uri.port )
    socket.use_ssl = true
    socket.verify_mode = OpenSSL::SSL::VERIFY_PEER
  else
    uri = URI.parse "http://#{url}"
    req = Net::HTTP::Post.new( uri.path )

    socket = Net::HTTP.new( uri.host, uri.port ) 
  end
  
  req.content_type = "text/xml"
  req.body = data
  req.add_field "User-Agent", "Clockwork Ruby Wrapper/#{Clockwork::VERSION}"
  
  response = socket.start do |http|
    http.request( req )
  end
  
  response
end