Module: Pipe2me::HTTP

Extended by:
HTTP
Included in:
HTTP
Defined in:
lib/pipe2me/ext/http.rb

Overview

The HTTP module implements a simple wrapper around Net::HTTP, intended to ease the pain of dealing with HTTP requests.

Defined Under Namespace

Classes: Error, RedirectionLimit, ResourceNotFound, Response, ServerError

Constant Summary collapse

@@config =

– configuration

OpenStruct.new

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(sym, *args, &block) ⇒ Object (private)



143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/pipe2me/ext/http.rb', line 143

def method_missing(sym, *args, &block)
  case sym.to_s
  when /^(.*)\!$/
    response = send $1, *args, &block
    response.validate!
  when /^(.*)\?$/
    response = send $1, *args, &block
    response if response.valid?
  else
    super
  end
end

Instance Method Details

#configObject

The configuration object. It supports the following entries:

  • config.headers: default headers to use when doing HTTP requests. Default: “Ruby HTTP client/1.0”

  • config.max_redirections: the number of maximum redirections to follow. Default: 10

To adjust the configuration change these objects, like so:

HTTP.config.headers =  { "User-Agent" => "My awesome thingy/1.0" }


48
49
50
# File 'lib/pipe2me/ext/http.rb', line 48

def config
  @@config
end

#get(url, headers = {}) ⇒ Object

runs a get request and return a HTTP::Response object.



132
133
134
# File 'lib/pipe2me/ext/http.rb', line 132

def get(url, headers = {})
  do_request Net::HTTP::Get, url, headers
end

#post(url, body, headers = {}) ⇒ Object

runs a post request and return a HTTP::Response object.



137
138
139
# File 'lib/pipe2me/ext/http.rb', line 137

def post(url, body, headers = {})
  do_request Net::HTTP::Post, url, headers, body
end