hypermedia 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/lib/api/api.rb +25 -0
- data/lib/api/document.rb +37 -0
- data/lib/api/form.rb +21 -0
- data/lib/api/model.rb +26 -0
- data/lib/hypermedia.rb +9 -0
- metadata +49 -0
data/lib/api/api.rb
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
|
3
|
+
module HypermediaAPI
|
4
|
+
require 'nokogiri'
|
5
|
+
require 'httparty'
|
6
|
+
include HTTParty
|
7
|
+
|
8
|
+
BadURI = Class.new(Exception)
|
9
|
+
MissingForm = Class.new(Exception)
|
10
|
+
|
11
|
+
class Parser < HTTParty::Parser
|
12
|
+
require 'nokogiri'
|
13
|
+
|
14
|
+
SupportedFormats.merge!('text/html' => :html)
|
15
|
+
|
16
|
+
def html
|
17
|
+
Nokogiri::HTML(body)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
def HypermediaAPI.included (subclass)
|
22
|
+
subclass.send :include, HTTParty
|
23
|
+
subclass.parser HypermediaAPI::Parser
|
24
|
+
end
|
25
|
+
end
|
data/lib/api/document.rb
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
|
3
|
+
class HypermediaAPI::Document
|
4
|
+
include HypermediaAPI
|
5
|
+
|
6
|
+
def Document.click (url)
|
7
|
+
response = Document.get(url)
|
8
|
+
Document.new(response)
|
9
|
+
end
|
10
|
+
|
11
|
+
def form (css_selector)
|
12
|
+
if form_element = @document.css(css_selector).first
|
13
|
+
action_uri = form_element['action']
|
14
|
+
http_method = form_element['method'].downcase
|
15
|
+
|
16
|
+
Form.new(action_uri, http_method)
|
17
|
+
else
|
18
|
+
raise MissingForm, "The API does not have a form matching '#{css_selector}' at #{@document.path}."
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
def headers
|
23
|
+
@document.headers
|
24
|
+
end
|
25
|
+
|
26
|
+
def html
|
27
|
+
@document.to_html
|
28
|
+
end
|
29
|
+
|
30
|
+
def initialize (httparty_nokogiri_document)
|
31
|
+
@document = httparty_nokogiri_document
|
32
|
+
end
|
33
|
+
|
34
|
+
def status
|
35
|
+
@document.code
|
36
|
+
end
|
37
|
+
end
|
data/lib/api/form.rb
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
|
3
|
+
class HypermediaAPI::Form
|
4
|
+
include HypermediaAPI
|
5
|
+
|
6
|
+
def initialize (action_uri, http_method)
|
7
|
+
@action_uri = action_uri
|
8
|
+
@http_method = http_method.downcase
|
9
|
+
end
|
10
|
+
|
11
|
+
def submit (inputs)
|
12
|
+
case @http_method
|
13
|
+
when 'post' then response = Form.post(@action_uri, body: inputs)
|
14
|
+
end
|
15
|
+
|
16
|
+
HypermediaAPI::Document.new(response)
|
17
|
+
|
18
|
+
rescue SocketError
|
19
|
+
raise BadURI, "The client was unable to interact with a resource at #{@action_uri}."
|
20
|
+
end
|
21
|
+
end
|
data/lib/api/model.rb
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
|
3
|
+
class HypermediaAPI::Model
|
4
|
+
def self.new_from_fields (field_elements)
|
5
|
+
instance = self.new
|
6
|
+
|
7
|
+
field_elements.each do |field_element|
|
8
|
+
value_str = field_element.inner_html
|
9
|
+
name = field_element['data-name']
|
10
|
+
|
11
|
+
value = case field_element['data-type']
|
12
|
+
when 'date' then value_str.empty? ? nil : Date.parse(value_str)
|
13
|
+
when 'integer' then value_str.empty? ? nil : value_str.to_i
|
14
|
+
when 'float' then value_str.empty? ? nil : value_str.to_f
|
15
|
+
when 'boolean' then value_str.empty? ? nil : value_str != 'false'
|
16
|
+
when 'string' then value_str
|
17
|
+
end
|
18
|
+
|
19
|
+
if instance.respond_to?(name)
|
20
|
+
instance.send(:"#{name}=", value)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
instance
|
25
|
+
end
|
26
|
+
end
|
data/lib/hypermedia.rb
ADDED
metadata
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: hypermedia
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Jesse Sielaff
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-05-15 00:00:00.000000000 Z
|
13
|
+
dependencies: []
|
14
|
+
description: .
|
15
|
+
email: jesse@myenergy.com
|
16
|
+
executables: []
|
17
|
+
extensions: []
|
18
|
+
extra_rdoc_files: []
|
19
|
+
files:
|
20
|
+
- ./lib/api/api.rb
|
21
|
+
- ./lib/api/document.rb
|
22
|
+
- ./lib/api/form.rb
|
23
|
+
- ./lib/api/model.rb
|
24
|
+
- ./lib/hypermedia.rb
|
25
|
+
homepage:
|
26
|
+
licenses: []
|
27
|
+
post_install_message:
|
28
|
+
rdoc_options: []
|
29
|
+
require_paths:
|
30
|
+
- lib
|
31
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
32
|
+
none: false
|
33
|
+
requirements:
|
34
|
+
- - ! '>='
|
35
|
+
- !ruby/object:Gem::Version
|
36
|
+
version: '0'
|
37
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
38
|
+
none: false
|
39
|
+
requirements:
|
40
|
+
- - ! '>='
|
41
|
+
- !ruby/object:Gem::Version
|
42
|
+
version: '0'
|
43
|
+
requirements: []
|
44
|
+
rubyforge_project:
|
45
|
+
rubygems_version: 1.8.15
|
46
|
+
signing_key:
|
47
|
+
specification_version: 3
|
48
|
+
summary: A client builder for HTML5-based HATEOAS APIs.
|
49
|
+
test_files: []
|