hypermedia 1.0.0 → 1.0.2
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 +33 -13
- data/lib/api/document.rb +26 -10
- data/lib/api/form.rb +2 -7
- data/lib/api/link.rb +13 -0
- data/lib/hypermedia.rb +2 -0
- metadata +5 -4
data/lib/api/api.rb
CHANGED
@@ -1,25 +1,45 @@
|
|
1
1
|
# encoding: UTF-8
|
2
2
|
|
3
3
|
module HypermediaAPI
|
4
|
-
require 'nokogiri'
|
5
|
-
require 'httparty'
|
6
|
-
include HTTParty
|
7
|
-
|
8
4
|
BadURI = Class.new(Exception)
|
9
5
|
MissingForm = Class.new(Exception)
|
10
6
|
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
SupportedFormats.merge!('text/html' => :html)
|
7
|
+
def HypermediaAPI.connection (uri)
|
8
|
+
connection = Net::HTTP.new(uri.host, uri.port)
|
15
9
|
|
16
|
-
|
17
|
-
|
10
|
+
if uri.scheme == 'https'
|
11
|
+
connection.use_ssl = true
|
12
|
+
connection.ssl_version = 'SSLv3'
|
18
13
|
end
|
14
|
+
|
15
|
+
connection
|
16
|
+
end
|
17
|
+
|
18
|
+
def HypermediaAPI.get (uri_string, options = {})
|
19
|
+
uri = URI(uri_string)
|
20
|
+
get = Net::HTTP::Get.new(uri.request_uri)
|
21
|
+
|
22
|
+
HypermediaAPI.send_request(uri, get, options)
|
19
23
|
end
|
20
24
|
|
21
|
-
def HypermediaAPI.
|
22
|
-
|
23
|
-
|
25
|
+
def HypermediaAPI.post (uri_string, options = {})
|
26
|
+
uri = URI(uri_string)
|
27
|
+
post = Net::HTTP::Post.new(uri.request_uri)
|
28
|
+
post.set_form_data(options[:inputs] || {})
|
29
|
+
|
30
|
+
HypermediaAPI.send_request(uri, post, options)
|
31
|
+
end
|
32
|
+
|
33
|
+
def HypermediaAPI.send_request (uri, request, options)
|
34
|
+
if auth = options[:basic_auth]
|
35
|
+
request.basic_auth(auth[:username], auth[:password])
|
36
|
+
end
|
37
|
+
|
38
|
+
response = HypermediaAPI.connection(uri).request(request)
|
39
|
+
status = response.code.to_i
|
40
|
+
headers = response.to_hash
|
41
|
+
nokogiri_html_document = Nokogiri::HTML::Document.parse(response.body, uri.to_s)
|
42
|
+
|
43
|
+
Document.new(nokogiri_html_document, status, headers)
|
24
44
|
end
|
25
45
|
end
|
data/lib/api/document.rb
CHANGED
@@ -1,11 +1,16 @@
|
|
1
1
|
# encoding: UTF-8
|
2
2
|
|
3
3
|
class HypermediaAPI::Document
|
4
|
-
|
4
|
+
def entity (klass)
|
5
|
+
article = @document.css("article.#{klass.name.underscore}").first
|
6
|
+
return nil unless article
|
7
|
+
|
8
|
+
klass.new_from_fields(article.css('code'))
|
9
|
+
end
|
5
10
|
|
6
|
-
def
|
7
|
-
|
8
|
-
|
11
|
+
def entities (klass)
|
12
|
+
articles = @document.css("article.#{klass.name.underscore}")
|
13
|
+
articles.map {|article| klass.new_from_fields(article.css('code')) }
|
9
14
|
end
|
10
15
|
|
11
16
|
def form (css_selector)
|
@@ -13,25 +18,36 @@ class HypermediaAPI::Document
|
|
13
18
|
action_uri = form_element['action']
|
14
19
|
http_method = form_element['method'].downcase
|
15
20
|
|
16
|
-
Form.new(action_uri, http_method)
|
21
|
+
HypermediaAPI::Form.new(action_uri, http_method)
|
17
22
|
else
|
18
|
-
raise MissingForm, "The API does not have a form matching '#{css_selector}'
|
23
|
+
raise HypermediaAPI::MissingForm, "The API does not have a form matching '#{css_selector}'."
|
19
24
|
end
|
20
25
|
end
|
21
26
|
|
22
27
|
def headers
|
23
|
-
@
|
28
|
+
@headers
|
24
29
|
end
|
25
30
|
|
26
31
|
def html
|
27
32
|
@document.to_html
|
28
33
|
end
|
29
34
|
|
30
|
-
def initialize (
|
31
|
-
@document =
|
35
|
+
def initialize (nokogiri_html_document, status, headers)
|
36
|
+
@document = nokogiri_html_document
|
37
|
+
@status = status.to_i
|
38
|
+
@headers = headers
|
39
|
+
end
|
40
|
+
|
41
|
+
def link (css_selector)
|
42
|
+
if a_element = @document.css(css_selector).first
|
43
|
+
href_uri = form_element['href']
|
44
|
+
Link.new(action_uri, http_method)
|
45
|
+
else
|
46
|
+
raise MissingForm, "The API does not have a form matching '#{css_selector}'."
|
47
|
+
end
|
32
48
|
end
|
33
49
|
|
34
50
|
def status
|
35
|
-
@
|
51
|
+
@status
|
36
52
|
end
|
37
53
|
end
|
data/lib/api/form.rb
CHANGED
@@ -1,20 +1,15 @@
|
|
1
1
|
# encoding: UTF-8
|
2
2
|
|
3
3
|
class HypermediaAPI::Form
|
4
|
-
include HypermediaAPI
|
5
|
-
|
6
4
|
def initialize (action_uri, http_method)
|
7
5
|
@action_uri = action_uri
|
8
6
|
@http_method = http_method.downcase
|
9
7
|
end
|
10
8
|
|
11
|
-
def submit (inputs)
|
9
|
+
def submit (inputs, options = {})
|
12
10
|
case @http_method
|
13
|
-
when 'post' then
|
11
|
+
when 'post' then HypermediaAPI.post(@action_uri, options.merge(inputs: inputs))
|
14
12
|
end
|
15
|
-
|
16
|
-
HypermediaAPI::Document.new(response)
|
17
|
-
|
18
13
|
rescue SocketError
|
19
14
|
raise BadURI, "The client was unable to interact with a resource at #{@action_uri}."
|
20
15
|
end
|
data/lib/api/link.rb
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
|
3
|
+
class HypermediaAPI::Link
|
4
|
+
def initialize (href_uri)
|
5
|
+
@href_uri = href_uri
|
6
|
+
end
|
7
|
+
|
8
|
+
def click (options = {})
|
9
|
+
response = HypermediaAPI.get(@href_uri, options)
|
10
|
+
rescue SocketError
|
11
|
+
raise BadURI, "The client was unable to interact with a resource at #{@href_uri}."
|
12
|
+
end
|
13
|
+
end
|
data/lib/hypermedia.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: hypermedia
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.2
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,10 +9,10 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-05-
|
12
|
+
date: 2012-05-20 00:00:00.000000000 Z
|
13
13
|
dependencies: []
|
14
14
|
description: .
|
15
|
-
email: jesse@
|
15
|
+
email: jesse.sielaff@gmail.com
|
16
16
|
executables: []
|
17
17
|
extensions: []
|
18
18
|
extra_rdoc_files: []
|
@@ -20,6 +20,7 @@ files:
|
|
20
20
|
- ./lib/api/api.rb
|
21
21
|
- ./lib/api/document.rb
|
22
22
|
- ./lib/api/form.rb
|
23
|
+
- ./lib/api/link.rb
|
23
24
|
- ./lib/api/model.rb
|
24
25
|
- ./lib/hypermedia.rb
|
25
26
|
homepage:
|
@@ -42,7 +43,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
42
43
|
version: '0'
|
43
44
|
requirements: []
|
44
45
|
rubyforge_project:
|
45
|
-
rubygems_version: 1.8.
|
46
|
+
rubygems_version: 1.8.23
|
46
47
|
signing_key:
|
47
48
|
specification_version: 3
|
48
49
|
summary: A client builder for HTML5-based HATEOAS APIs.
|