Module: HypermediaAPI

Defined in:
lib/api/api.rb

Defined Under Namespace

Modules: Html Classes: Document, Entity, Form, Link

Constant Summary collapse

BadURI =
Class.new(Exception)
MissingForm =
Class.new(Exception)
Class.new(Exception)
UnsupportedHTTPMethod =
Class.new(Exception)

Class Method Summary collapse

Class Method Details

.connection(uri) ⇒ Object

Returns an open Net::HTTP connection to the given URI.



10
11
12
13
14
15
16
17
18
19
# File 'lib/api/api.rb', line 10

def HypermediaAPI.connection (uri)
  connection = Net::HTTP.new(uri.host, uri.port)
  
  if uri.scheme == 'https'
    connection.use_ssl = true
    connection.ssl_version = 'SSLv3'
  end
  
  connection
end

.deleteObject

Raises an error. DELETE requests are unsupported.



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

def HypermediaAPI.delete (*)
  raise HypermediaAPI::UnsupportedHTTPMethod, "Only GET and POST requests are supported (attempted DELETE)"
end

.get(uri_string, options = {}) ⇒ Object

GETs the given uri (with or without form data) and returns a HypermediaAPI::Document representing the response. Currently the valid options are 1) an :inputs Hash containing form data key-value pairs and 2) an :auth Hash containing HTTP basic auth username and password.



30
31
32
33
34
35
36
37
38
39
# File 'lib/api/api.rb', line 30

def HypermediaAPI.get (uri_string, options = {})
  uri = URI(uri_string)
  get = Net::HTTP::Get.new(uri.request_uri)
  
  if options[:inputs]
    get.set_form_data(options[:inputs])
  end
  
  HypermediaAPI.send_request(uri, get, options)
end

.post(uri_string, options = {}) ⇒ Object

POSTs form data to the given uri and returns a HypermediaAPI::Document representing the response. Currently the valid options are 1) an :inputs Hash containing form data key-value pairs and 2) an :auth Hash containing HTTP basic auth username and password.



45
46
47
48
49
50
51
# File 'lib/api/api.rb', line 45

def HypermediaAPI.post (uri_string, options = {})
  uri = URI(uri_string)
  post = Net::HTTP::Post.new(uri.request_uri)
  post.set_form_data(options[:inputs] || {})
  
  HypermediaAPI.send_request(uri, post, options)
end

.putObject

Raises an error. PUT requests are unsupported.



54
55
56
# File 'lib/api/api.rb', line 54

def HypermediaAPI.put (*)
  raise HypermediaAPI::UnsupportedHTTPMethod, "Only GET and POST requests are supported (attempted PUT)"
end

.send_request(uri, request, options) ⇒ Object

Sends a request to the given URI and returns a HypermediaAPI::Document representing the response. Currently the only valid option is an :auth Hash containing HTTP basic auth username and password.



61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/api/api.rb', line 61

def HypermediaAPI.send_request (uri, request, options)
  if auth = options[:auth]
    request.basic_auth(auth[:username], auth[:password])
  end
  
  response = HypermediaAPI.connection(uri).request(request)
  status = response.code.to_i
  headers = response.to_hash
  nokogiri_html_document = Nokogiri::HTML::Document.parse(response.body, uri.to_s)
  
  Document.new(nokogiri_html_document, status, headers, auth)
end