Class: RDFMapper::HTTP

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

Overview

Basic HTTP interface, built on top of Patron library. Used by RDFMapper adapters for communication with external data sources.

Patron automatically handles cookies, redirects, timeouts. Can be substituted by any other library as long as HTTP class implements ‘get` and `post` methods.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ self



43
44
45
46
47
48
49
# File 'lib/lib/util/http.rb', line 43

def initialize(options = {})
  @session = Patron::Session.new
  @session.handle_cookies
  @session.timeout = 10
  @session.headers['User-Agent'] = 'Mozilla / RDFMapper'
  @options = options
end

Class Method Details

.get(url, options = {}) ⇒ String

Performs a ‘GET` request and returns received data

Parameters:

  • url (String, RDF::URI)
  • options (Hash) (defaults to: {})

    for Patron constructor

Returns:

  • (String)


22
23
24
# File 'lib/lib/util/http.rb', line 22

def get(url, options = {})
  self.new(options).get(url.to_s)
end

.post(url, data, options = {}) ⇒ String

Performs a ‘POST` request and returns received data

Parameters:

  • url (String, RDF::URI)
  • data (String)
  • options (Hash) (defaults to: {})

    for Patron constructor

Returns:

  • (String)


34
35
36
# File 'lib/lib/util/http.rb', line 34

def post(url, data, options = {})
  self.new(options).post(url.to_s, data)
end

Instance Method Details

#get(url) ⇒ Object



51
52
53
# File 'lib/lib/util/http.rb', line 51

def get(url)
  @session.get(url, headers).body
end

#post(url, data) ⇒ Object



55
56
57
# File 'lib/lib/util/http.rb', line 55

def post(url, data)
  @session.post(url, data, headers).body
end