almodovar 0.9.8 → 1.0.0.pre
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.
- checksums.yaml +7 -0
- data/lib/almodovar.rb +1 -5
- data/lib/almodovar/http_accessor.rb +3 -3
- data/lib/almodovar/http_client.rb +104 -0
- data/lib/almodovar/resource.rb +1 -1
- data/lib/almodovar/resource_collection.rb +27 -2
- data/lib/almodovar/single_resource.rb +1 -1
- data/lib/almodovar/version.rb +1 -1
- metadata +29 -51
- data/lib/almodovar/resource_presenter.rb +0 -61
- data/lib/almodovar/resource_presenter/collection.rb +0 -69
- data/lib/almodovar/resource_presenter/html_serializer.rb +0 -44
- data/lib/almodovar/resource_presenter/json_serializer.rb +0 -31
- data/lib/almodovar/resource_presenter/link.rb +0 -104
- data/lib/almodovar/resource_presenter/metadata.rb +0 -27
- data/lib/almodovar/resource_presenter/serializer.rb +0 -18
- data/lib/almodovar/resource_presenter/template.html.erb +0 -125
- data/lib/almodovar/resource_presenter/xml_serializer.rb +0 -25
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: a9c558a41b4f44342111b462d222add5f75a1554
|
4
|
+
data.tar.gz: c3dfc2635e36cb5383fe5b800ceafcb0287f899e
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: c76748a94c90c083ced281b7b3c18ba2f514954c76d9fd613aedffeca966e833a7eb7e8c03610ae8bb61f4f88c2a1ae270ac4d6278051092ef60e1a660bf3fac
|
7
|
+
data.tar.gz: a46d9ce4a65c8e7156bc03c6d4fafeeaec5f55de7eff3d77a17bcc7d5d4cc510cabacdd942990370e888f25be1346282fcf458b8a91700a7ce587461f64c8feb
|
data/lib/almodovar.rb
CHANGED
@@ -1,4 +1,3 @@
|
|
1
|
-
require 'patron'
|
2
1
|
require 'nokogiri'
|
3
2
|
begin
|
4
3
|
require 'active_support/all'
|
@@ -8,16 +7,13 @@ end
|
|
8
7
|
|
9
8
|
require 'almodovar/version' unless defined?(Almodovar::VERSION)
|
10
9
|
require 'almodovar/digest_auth'
|
10
|
+
require 'almodovar/http_client'
|
11
11
|
require 'almodovar/http_accessor'
|
12
12
|
require 'almodovar/resource'
|
13
13
|
require 'almodovar/resource_collection'
|
14
14
|
require 'almodovar/single_resource'
|
15
15
|
require 'almodovar/errors'
|
16
16
|
require 'almodovar/to_xml'
|
17
|
-
require 'almodovar/resource_presenter'
|
18
|
-
require 'almodovar/resource_presenter/collection'
|
19
|
-
require 'almodovar/resource_presenter/link'
|
20
|
-
|
21
17
|
|
22
18
|
module Almodovar
|
23
19
|
|
@@ -16,11 +16,11 @@ module Almodovar
|
|
16
16
|
end
|
17
17
|
|
18
18
|
def http
|
19
|
-
@http ||=
|
19
|
+
@http ||= Almodovar::HttpClient.new.tap do |session|
|
20
20
|
session.timeout = Almodovar::default_options[:timeout]
|
21
21
|
session.connect_timeout = Almodovar::default_options[:connect_timeout]
|
22
|
-
session.
|
23
|
-
|
22
|
+
session.agent_name = Almodovar::default_options[:user_agent]
|
23
|
+
|
24
24
|
if @auth
|
25
25
|
session.username = @auth.username
|
26
26
|
session.password = @auth.password
|
@@ -0,0 +1,104 @@
|
|
1
|
+
require 'httpclient'
|
2
|
+
|
3
|
+
module Almodovar
|
4
|
+
class HttpClient
|
5
|
+
|
6
|
+
attr_accessor :client,
|
7
|
+
:headers,
|
8
|
+
:username,
|
9
|
+
:password,
|
10
|
+
:auth_type
|
11
|
+
|
12
|
+
delegate :agent_name=,
|
13
|
+
:connect_timeout=,
|
14
|
+
:to => :client
|
15
|
+
|
16
|
+
|
17
|
+
def timeout=(value)
|
18
|
+
client.send_timeout = value
|
19
|
+
end
|
20
|
+
|
21
|
+
def initialize
|
22
|
+
@client = HTTPClient.new
|
23
|
+
end
|
24
|
+
|
25
|
+
def get(uri, headers = {})
|
26
|
+
request(:get, uri, :headers => merge_headers(headers))
|
27
|
+
end
|
28
|
+
|
29
|
+
def post(uri, data, headers = {})
|
30
|
+
request(:post, uri, :body => data, :headers => merge_headers(headers))
|
31
|
+
end
|
32
|
+
|
33
|
+
def put(uri, data, headers = {})
|
34
|
+
request(:put, uri, :body => data, :headers => merge_headers(headers))
|
35
|
+
end
|
36
|
+
|
37
|
+
def delete(uri, headers = {})
|
38
|
+
request(:delete, uri, :headers => merge_headers(headers))
|
39
|
+
end
|
40
|
+
|
41
|
+
private
|
42
|
+
|
43
|
+
def merge_headers(headers)
|
44
|
+
headers.merge(headers)
|
45
|
+
end
|
46
|
+
|
47
|
+
def requires_auth?
|
48
|
+
username && password
|
49
|
+
end
|
50
|
+
|
51
|
+
def domain_for(uri)
|
52
|
+
scheme_chunk = uri.scheme ? "#{uri.scheme}://" : ''
|
53
|
+
port_chunk = uri.port ? ":#{uri.port}" : ''
|
54
|
+
host_chunk = uri.host.downcase
|
55
|
+
"#{scheme_chunk}#{host_chunk}#{port_chunk}"
|
56
|
+
end
|
57
|
+
|
58
|
+
def previous_context
|
59
|
+
@previous_auth_context
|
60
|
+
end
|
61
|
+
|
62
|
+
def previous_context=(context)
|
63
|
+
@previous_auth_context = context
|
64
|
+
end
|
65
|
+
|
66
|
+
def set_client_auth(domain)
|
67
|
+
context = AuthContext.new(username, password, domain)
|
68
|
+
if (context.differs_from?(previous_context))
|
69
|
+
client.set_auth(domain, username, password)
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
def request(method, uri, options = {})
|
74
|
+
uri = URI.parse(uri)
|
75
|
+
if (requires_auth?)
|
76
|
+
domain = domain_for(uri)
|
77
|
+
uri.user = username
|
78
|
+
uri.password = password
|
79
|
+
set_client_auth(domain)
|
80
|
+
end
|
81
|
+
client.request(method, uri, :body => options[:body], :header => options[:headers].stringify_keys || {}, :follow_redirect => true)
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
class AuthContext
|
86
|
+
attr_accessor :username, :password, :domain
|
87
|
+
|
88
|
+
def initialize(username, password, domain)
|
89
|
+
@username = username
|
90
|
+
@password = password
|
91
|
+
@domain = domain
|
92
|
+
end
|
93
|
+
|
94
|
+
def differs_from?(other)
|
95
|
+
return true if other.nil?
|
96
|
+
(username != other.username) || (password != other.password) || (domain != other.domain)
|
97
|
+
end
|
98
|
+
end
|
99
|
+
|
100
|
+
class HttpResponse
|
101
|
+
attr_accessor :status, :body
|
102
|
+
end
|
103
|
+
|
104
|
+
end
|
data/lib/almodovar/resource.rb
CHANGED
@@ -35,7 +35,7 @@ module Almodovar
|
|
35
35
|
|
36
36
|
def collection_call?(meth, *args)
|
37
37
|
([].respond_to?(meth) && !["delete", "id", "[]"].include?(meth.to_s)) ||
|
38
|
-
|
38
|
+
ResourceCollection.instance_methods(false).map(&:to_s).include?(meth.to_s) ||
|
39
39
|
(meth.to_s == "[]" && args.size == 1 && args.first.is_a?(Fixnum))
|
40
40
|
end
|
41
41
|
|
@@ -3,8 +3,10 @@ module Almodovar
|
|
3
3
|
include HttpAccessor
|
4
4
|
include Enumerable
|
5
5
|
|
6
|
+
PAGINATION_ENTITIES = ["self::total-entries", "self::link[@rel='next']", "self::link[@rel='prev']"].join('|').freeze
|
7
|
+
|
6
8
|
delegate :inspect, :to => :resources
|
7
|
-
|
9
|
+
|
8
10
|
def initialize(url, auth, xml = nil, options = {})
|
9
11
|
@url = url
|
10
12
|
@auth = auth
|
@@ -19,11 +21,34 @@ module Almodovar
|
|
19
21
|
check_errors(response, url_with_params)
|
20
22
|
Resource.new(nil, @auth, Nokogiri::XML.parse(response.body).root)
|
21
23
|
end
|
24
|
+
|
25
|
+
def total_entries
|
26
|
+
@total_entries ||= xml.at_xpath("./total-entries").try(:text).try(:to_i) || resources.size
|
27
|
+
end
|
28
|
+
|
29
|
+
def next_url
|
30
|
+
@next_url ||= xml.at_xpath("./link[@rel='next']").try(:[], "href")
|
31
|
+
end
|
32
|
+
|
33
|
+
def prev_url
|
34
|
+
@prev_url ||= xml.at_xpath("./link[@rel='prev']").try(:[], "href")
|
35
|
+
end
|
36
|
+
|
37
|
+
def next_page
|
38
|
+
Resource.new(next_url, @auth) if next_url
|
39
|
+
end
|
40
|
+
|
41
|
+
def prev_page
|
42
|
+
Resource.new(prev_url, @auth) if prev_url
|
43
|
+
end
|
22
44
|
|
23
45
|
private
|
24
46
|
|
25
47
|
def resources
|
26
|
-
@resources ||=
|
48
|
+
@resources ||= begin
|
49
|
+
xml.xpath("./*[not(#{PAGINATION_ENTITIES})]").
|
50
|
+
map { |subnode| Resource.new(subnode.at_xpath("./link[@rel='self']").try(:[], "href"), @auth, subnode, @options) }
|
51
|
+
end
|
27
52
|
end
|
28
53
|
|
29
54
|
def method_missing(meth, *args, &blk)
|
data/lib/almodovar/version.rb
CHANGED
metadata
CHANGED
@@ -1,110 +1,97 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: almodovar
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
5
|
-
prerelease:
|
4
|
+
version: 1.0.0.pre
|
6
5
|
platform: ruby
|
7
6
|
authors:
|
8
7
|
- BeBanjo S.L.
|
9
8
|
autorequire:
|
10
9
|
bindir: bin
|
11
10
|
cert_chain: []
|
12
|
-
date:
|
11
|
+
date: 2014-02-11 00:00:00.000000000 Z
|
13
12
|
dependencies:
|
14
13
|
- !ruby/object:Gem::Dependency
|
15
|
-
name:
|
14
|
+
name: builder
|
16
15
|
requirement: !ruby/object:Gem::Requirement
|
17
|
-
none: false
|
18
16
|
requirements:
|
19
|
-
- -
|
17
|
+
- - '>='
|
20
18
|
- !ruby/object:Gem::Version
|
21
19
|
version: '0'
|
22
20
|
type: :runtime
|
23
21
|
prerelease: false
|
24
22
|
version_requirements: !ruby/object:Gem::Requirement
|
25
|
-
none: false
|
26
23
|
requirements:
|
27
|
-
- -
|
24
|
+
- - '>='
|
28
25
|
- !ruby/object:Gem::Version
|
29
26
|
version: '0'
|
30
27
|
- !ruby/object:Gem::Dependency
|
31
|
-
name:
|
28
|
+
name: nokogiri
|
32
29
|
requirement: !ruby/object:Gem::Requirement
|
33
|
-
none: false
|
34
30
|
requirements:
|
35
|
-
- -
|
31
|
+
- - '>='
|
36
32
|
- !ruby/object:Gem::Version
|
37
33
|
version: '0'
|
38
34
|
type: :runtime
|
39
35
|
prerelease: false
|
40
36
|
version_requirements: !ruby/object:Gem::Requirement
|
41
|
-
none: false
|
42
37
|
requirements:
|
43
|
-
- -
|
38
|
+
- - '>='
|
44
39
|
- !ruby/object:Gem::Version
|
45
40
|
version: '0'
|
46
41
|
- !ruby/object:Gem::Dependency
|
47
|
-
name:
|
42
|
+
name: activesupport
|
48
43
|
requirement: !ruby/object:Gem::Requirement
|
49
|
-
none: false
|
50
44
|
requirements:
|
51
|
-
- -
|
45
|
+
- - '>='
|
52
46
|
- !ruby/object:Gem::Version
|
53
47
|
version: '0'
|
54
48
|
type: :runtime
|
55
49
|
prerelease: false
|
56
50
|
version_requirements: !ruby/object:Gem::Requirement
|
57
|
-
none: false
|
58
51
|
requirements:
|
59
|
-
- -
|
52
|
+
- - '>='
|
60
53
|
- !ruby/object:Gem::Version
|
61
54
|
version: '0'
|
62
55
|
- !ruby/object:Gem::Dependency
|
63
|
-
name:
|
56
|
+
name: i18n
|
64
57
|
requirement: !ruby/object:Gem::Requirement
|
65
|
-
none: false
|
66
58
|
requirements:
|
67
|
-
- -
|
59
|
+
- - '>='
|
68
60
|
- !ruby/object:Gem::Version
|
69
61
|
version: '0'
|
70
62
|
type: :runtime
|
71
63
|
prerelease: false
|
72
64
|
version_requirements: !ruby/object:Gem::Requirement
|
73
|
-
none: false
|
74
65
|
requirements:
|
75
|
-
- -
|
66
|
+
- - '>='
|
76
67
|
- !ruby/object:Gem::Version
|
77
68
|
version: '0'
|
78
69
|
- !ruby/object:Gem::Dependency
|
79
|
-
name:
|
70
|
+
name: httpclient
|
80
71
|
requirement: !ruby/object:Gem::Requirement
|
81
|
-
none: false
|
82
72
|
requirements:
|
83
|
-
- -
|
73
|
+
- - '>='
|
84
74
|
- !ruby/object:Gem::Version
|
85
75
|
version: '0'
|
86
76
|
type: :runtime
|
87
77
|
prerelease: false
|
88
78
|
version_requirements: !ruby/object:Gem::Requirement
|
89
|
-
none: false
|
90
79
|
requirements:
|
91
|
-
- -
|
80
|
+
- - '>='
|
92
81
|
- !ruby/object:Gem::Version
|
93
82
|
version: '0'
|
94
83
|
- !ruby/object:Gem::Dependency
|
95
|
-
name:
|
84
|
+
name: json
|
96
85
|
requirement: !ruby/object:Gem::Requirement
|
97
|
-
none: false
|
98
86
|
requirements:
|
99
|
-
- -
|
87
|
+
- - '>='
|
100
88
|
- !ruby/object:Gem::Version
|
101
89
|
version: '0'
|
102
90
|
type: :runtime
|
103
91
|
prerelease: false
|
104
92
|
version_requirements: !ruby/object:Gem::Requirement
|
105
|
-
none: false
|
106
93
|
requirements:
|
107
|
-
- -
|
94
|
+
- - '>='
|
108
95
|
- !ruby/object:Gem::Version
|
109
96
|
version: '0'
|
110
97
|
description:
|
@@ -118,23 +105,17 @@ files:
|
|
118
105
|
- lib/almodovar/digest_auth.rb
|
119
106
|
- lib/almodovar/errors.rb
|
120
107
|
- lib/almodovar/http_accessor.rb
|
108
|
+
- lib/almodovar/http_client.rb
|
121
109
|
- lib/almodovar/resource.rb
|
122
110
|
- lib/almodovar/resource_collection.rb
|
123
|
-
- lib/almodovar/resource_presenter/collection.rb
|
124
|
-
- lib/almodovar/resource_presenter/html_serializer.rb
|
125
|
-
- lib/almodovar/resource_presenter/json_serializer.rb
|
126
|
-
- lib/almodovar/resource_presenter/link.rb
|
127
|
-
- lib/almodovar/resource_presenter/metadata.rb
|
128
|
-
- lib/almodovar/resource_presenter/serializer.rb
|
129
|
-
- lib/almodovar/resource_presenter/template.html.erb
|
130
|
-
- lib/almodovar/resource_presenter/xml_serializer.rb
|
131
|
-
- lib/almodovar/resource_presenter.rb
|
132
111
|
- lib/almodovar/single_resource.rb
|
133
112
|
- lib/almodovar/to_xml.rb
|
134
113
|
- lib/almodovar/version.rb
|
135
114
|
- lib/almodovar.rb
|
136
115
|
homepage: http://wiki.github.com/bebanjo/almodovar/
|
137
|
-
licenses:
|
116
|
+
licenses:
|
117
|
+
- MIT
|
118
|
+
metadata: {}
|
138
119
|
post_install_message:
|
139
120
|
rdoc_options:
|
140
121
|
- --main
|
@@ -142,22 +123,19 @@ rdoc_options:
|
|
142
123
|
require_paths:
|
143
124
|
- lib
|
144
125
|
required_ruby_version: !ruby/object:Gem::Requirement
|
145
|
-
none: false
|
146
126
|
requirements:
|
147
|
-
- -
|
127
|
+
- - '>='
|
148
128
|
- !ruby/object:Gem::Version
|
149
129
|
version: '0'
|
150
130
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
151
|
-
none: false
|
152
131
|
requirements:
|
153
|
-
- -
|
132
|
+
- - '>'
|
154
133
|
- !ruby/object:Gem::Version
|
155
|
-
version:
|
134
|
+
version: 1.3.1
|
156
135
|
requirements: []
|
157
136
|
rubyforge_project:
|
158
|
-
rubygems_version:
|
137
|
+
rubygems_version: 2.0.6
|
159
138
|
signing_key:
|
160
|
-
specification_version:
|
139
|
+
specification_version: 4
|
161
140
|
summary: BeBanjo API client
|
162
141
|
test_files: []
|
163
|
-
has_rdoc:
|
@@ -1,61 +0,0 @@
|
|
1
|
-
module Almodovar
|
2
|
-
|
3
|
-
class ResourcePresenter
|
4
|
-
|
5
|
-
autoload :Serializer, 'almodovar/resource_presenter/serializer'
|
6
|
-
autoload :XmlSerializer, 'almodovar/resource_presenter/xml_serializer'
|
7
|
-
autoload :JsonSerializer, 'almodovar/resource_presenter/json_serializer'
|
8
|
-
autoload :HtmlSerializer, 'almodovar/resource_presenter/html_serializer'
|
9
|
-
autoload :Metadata, 'almodovar/resource_presenter/metadata'
|
10
|
-
|
11
|
-
extend Metadata
|
12
|
-
|
13
|
-
attr_accessor :url
|
14
|
-
|
15
|
-
def attributes
|
16
|
-
@attributes ||= ActiveSupport::OrderedHash.new
|
17
|
-
end
|
18
|
-
|
19
|
-
def links
|
20
|
-
@links ||= []
|
21
|
-
end
|
22
|
-
|
23
|
-
def self.resource_type
|
24
|
-
name.gsub(/Resource$/, '').underscore
|
25
|
-
end
|
26
|
-
|
27
|
-
def resource_class
|
28
|
-
self.class
|
29
|
-
end
|
30
|
-
|
31
|
-
def resource_type
|
32
|
-
resource_class.resource_type
|
33
|
-
end
|
34
|
-
|
35
|
-
def to_xml(options = {})
|
36
|
-
XmlSerializer.new(self, options).to_xml
|
37
|
-
end
|
38
|
-
|
39
|
-
def to_json(options = {})
|
40
|
-
JsonSerializer.new(self, options).to_json
|
41
|
-
end
|
42
|
-
|
43
|
-
def as_json(options = {})
|
44
|
-
JsonSerializer.new(self, options).as_json
|
45
|
-
end
|
46
|
-
|
47
|
-
def to_html(options = {})
|
48
|
-
HtmlSerializer.new(self, options).to_html
|
49
|
-
end
|
50
|
-
|
51
|
-
def all_links
|
52
|
-
([link_to_self] + links).compact
|
53
|
-
end
|
54
|
-
|
55
|
-
def link_to_self
|
56
|
-
Link.new(:self, @url) if @url
|
57
|
-
end
|
58
|
-
|
59
|
-
end
|
60
|
-
|
61
|
-
end
|
@@ -1,69 +0,0 @@
|
|
1
|
-
module Almodovar
|
2
|
-
class ResourcePresenter
|
3
|
-
class Collection
|
4
|
-
|
5
|
-
attr_reader :resource_class
|
6
|
-
|
7
|
-
def initialize(resource_class, resources_args = [], options = {})
|
8
|
-
@resource_class = resource_class
|
9
|
-
@resources = resources_args.map { |arg| @resource_class.new(arg) }
|
10
|
-
|
11
|
-
@total = options[:total_entries]
|
12
|
-
@next = options[:next_link]
|
13
|
-
@prev = options[:prev_link]
|
14
|
-
end
|
15
|
-
|
16
|
-
def to_xml(options = {})
|
17
|
-
# Most of the following code is borrowed from ActiveSupport's Array#to_xml.
|
18
|
-
# We cannot use Array#to_xml because we need to add a few custom tags for
|
19
|
-
# pagination.
|
20
|
-
require 'active_support/builder' unless defined?(Builder)
|
21
|
-
|
22
|
-
options = options.dup
|
23
|
-
options[:indent] ||= 2
|
24
|
-
options[:builder] ||= ::Builder::XmlMarkup.new(:indent => options[:indent])
|
25
|
-
|
26
|
-
xml = options[:builder]
|
27
|
-
|
28
|
-
xml.instruct! unless options[:skip_instruct]
|
29
|
-
xml.tag! resource_type.pluralize.dasherize, :type => 'array' do
|
30
|
-
xml.tag!('total-entries', @total) if @total
|
31
|
-
prev_link.to_xml(:builder => xml) if prev_link
|
32
|
-
next_link.to_xml(:builder => xml) if next_link
|
33
|
-
@resources.each { |value| value.to_xml(options.merge(:root => resource_type.singularize, :skip_instruct => true)) }
|
34
|
-
end
|
35
|
-
end
|
36
|
-
|
37
|
-
def as_json(options = {})
|
38
|
-
message = ActiveSupport::OrderedHash.new.tap do |message|
|
39
|
-
message[:total_entries] = @total if @total
|
40
|
-
message.merge! prev_link.as_json if prev_link
|
41
|
-
message.merge! next_link.as_json if next_link
|
42
|
-
message[:entries] = @resources.map { |resource| resource.as_json(options) }
|
43
|
-
end
|
44
|
-
end
|
45
|
-
|
46
|
-
def to_json(options = {})
|
47
|
-
require 'yajl'
|
48
|
-
Yajl::Encoder.encode(as_json(options), :pretty => true) + "\n"
|
49
|
-
end
|
50
|
-
|
51
|
-
def to_html(options = {})
|
52
|
-
HtmlSerializer.new(self, options).to_html
|
53
|
-
end
|
54
|
-
|
55
|
-
def resource_type
|
56
|
-
@resource_class.resource_type
|
57
|
-
end
|
58
|
-
|
59
|
-
def next_link
|
60
|
-
Link.new(:next, @next) if @next
|
61
|
-
end
|
62
|
-
|
63
|
-
def prev_link
|
64
|
-
Link.new(:prev, @prev) if @prev
|
65
|
-
end
|
66
|
-
|
67
|
-
end
|
68
|
-
end
|
69
|
-
end
|
@@ -1,44 +0,0 @@
|
|
1
|
-
%w(pygments.rb github-markdown).each do |lib|
|
2
|
-
begin
|
3
|
-
require lib.gsub('-', '/')
|
4
|
-
rescue LoadError => ex
|
5
|
-
raise ex, "In order to use the HtmlSerializer you need the gem `#{lib}` in your Gemfile"
|
6
|
-
end
|
7
|
-
end
|
8
|
-
|
9
|
-
module Almodovar
|
10
|
-
class ResourcePresenter
|
11
|
-
class HtmlSerializer < Serializer
|
12
|
-
|
13
|
-
TEMPLATE_PATH = File.join(File.dirname(__FILE__), 'template.html.erb')
|
14
|
-
|
15
|
-
def to_html
|
16
|
-
template = File.read(TEMPLATE_PATH)
|
17
|
-
ERB.new(template).result(binding)
|
18
|
-
end
|
19
|
-
|
20
|
-
def metadata
|
21
|
-
resource.resource_class.metadata
|
22
|
-
end
|
23
|
-
|
24
|
-
def beautify(representation, format)
|
25
|
-
body = Pygments.highlight(representation, :lexer => format)
|
26
|
-
body = body.gsub(/"(http\S+)"/) { url = $1; ""<a href=\"#{url}\">#{url}</a>"" }
|
27
|
-
body.html_safe
|
28
|
-
end
|
29
|
-
|
30
|
-
def metadata_text(text)
|
31
|
-
return if text.blank?
|
32
|
-
|
33
|
-
if indentation = text[/\n\s+/]
|
34
|
-
text = text.gsub(indentation, "\n")
|
35
|
-
end
|
36
|
-
|
37
|
-
text = text.strip
|
38
|
-
text = GitHub::Markdown.render text
|
39
|
-
text.html_safe
|
40
|
-
end
|
41
|
-
|
42
|
-
end
|
43
|
-
end
|
44
|
-
end
|
@@ -1,31 +0,0 @@
|
|
1
|
-
module Almodovar
|
2
|
-
class ResourcePresenter
|
3
|
-
class JsonSerializer < Serializer
|
4
|
-
|
5
|
-
def to_json
|
6
|
-
require 'yajl'
|
7
|
-
Yajl::Encoder.encode(as_json, :pretty => true) + "\n"
|
8
|
-
end
|
9
|
-
|
10
|
-
def as_json
|
11
|
-
ActiveSupport::OrderedHash[:resource_type, resource.resource_type].tap do |message|
|
12
|
-
message.merge! attributes_as_json
|
13
|
-
message.merge! links_as_json
|
14
|
-
end
|
15
|
-
end
|
16
|
-
|
17
|
-
private
|
18
|
-
|
19
|
-
def attributes_as_json
|
20
|
-
resource.attributes
|
21
|
-
end
|
22
|
-
|
23
|
-
def links_as_json
|
24
|
-
resource.all_links.inject(ActiveSupport::OrderedHash.new) do |message, link|
|
25
|
-
message.merge! link.as_json(options_for_link)
|
26
|
-
end
|
27
|
-
end
|
28
|
-
|
29
|
-
end
|
30
|
-
end
|
31
|
-
end
|
@@ -1,104 +0,0 @@
|
|
1
|
-
module Almodovar
|
2
|
-
class ResourcePresenter
|
3
|
-
class Link
|
4
|
-
|
5
|
-
attr_reader :rel, :href, :expand_resource, :expand_args, :attributes
|
6
|
-
|
7
|
-
def initialize(*args)
|
8
|
-
@rel, @href, @expand_resource, @expand_args, @attributes = args
|
9
|
-
@attributes ||= {}
|
10
|
-
end
|
11
|
-
|
12
|
-
def to_xml(options = {})
|
13
|
-
XmlSerializer.new(self, options.merge(:skip_instruct => true)).to_xml
|
14
|
-
end
|
15
|
-
|
16
|
-
def as_json(options = {})
|
17
|
-
JsonSerializer.new(self, options).as_json
|
18
|
-
end
|
19
|
-
|
20
|
-
def resource
|
21
|
-
resource_collection? ? resource_collection : single_resource
|
22
|
-
end
|
23
|
-
|
24
|
-
def resource_collection?
|
25
|
-
expand_args.is_a?(Array)
|
26
|
-
end
|
27
|
-
|
28
|
-
def resource_collection
|
29
|
-
ResourcePresenter::Collection.new(expand_resource, expand_args)
|
30
|
-
end
|
31
|
-
|
32
|
-
def single_resource
|
33
|
-
expand_resource.new(*[expand_args].compact)
|
34
|
-
end
|
35
|
-
|
36
|
-
def expand_resource?
|
37
|
-
expand_resource.present?
|
38
|
-
end
|
39
|
-
|
40
|
-
class Serializer
|
41
|
-
|
42
|
-
attr_reader :link, :options
|
43
|
-
|
44
|
-
def initialize(link, options)
|
45
|
-
@link = link
|
46
|
-
@options = options
|
47
|
-
end
|
48
|
-
|
49
|
-
def expands?
|
50
|
-
link.expand_resource? &&
|
51
|
-
Array(options[:expand]).include?(link.rel) &&
|
52
|
-
!Array(options[:dont_expand]).include?(link.href)
|
53
|
-
end
|
54
|
-
|
55
|
-
def dont_expand_link!
|
56
|
-
(options[:dont_expand] ||= []) << link.href
|
57
|
-
end
|
58
|
-
|
59
|
-
end
|
60
|
-
|
61
|
-
class XmlSerializer < Serializer
|
62
|
-
|
63
|
-
def to_xml
|
64
|
-
builder.link link.attributes.merge(:rel => link.rel, :href => link.href), &expand_block
|
65
|
-
end
|
66
|
-
|
67
|
-
private
|
68
|
-
|
69
|
-
def builder
|
70
|
-
options[:builder]
|
71
|
-
end
|
72
|
-
|
73
|
-
def expand_block
|
74
|
-
Proc.new { expand_resource } if expands?
|
75
|
-
end
|
76
|
-
|
77
|
-
def expand_resource
|
78
|
-
dont_expand_link!
|
79
|
-
link.resource.to_xml(options)
|
80
|
-
end
|
81
|
-
|
82
|
-
end
|
83
|
-
|
84
|
-
class JsonSerializer < Serializer
|
85
|
-
|
86
|
-
def as_json
|
87
|
-
ActiveSupport::OrderedHash.new.tap do |message|
|
88
|
-
message["#{link.rel}_link"] = link.href
|
89
|
-
message[link.rel] = expand_resource if expands?
|
90
|
-
end
|
91
|
-
end
|
92
|
-
|
93
|
-
private
|
94
|
-
|
95
|
-
def expand_resource
|
96
|
-
dont_expand_link!
|
97
|
-
link.resource.as_json(options)
|
98
|
-
end
|
99
|
-
|
100
|
-
end
|
101
|
-
|
102
|
-
end
|
103
|
-
end
|
104
|
-
end
|
@@ -1,27 +0,0 @@
|
|
1
|
-
module Almodovar
|
2
|
-
class ResourcePresenter
|
3
|
-
module Metadata
|
4
|
-
|
5
|
-
def inherited(base)
|
6
|
-
base.metadata[:name] = base.name.demodulize.titleize
|
7
|
-
end
|
8
|
-
|
9
|
-
def desc(description)
|
10
|
-
metadata[:desc] = description
|
11
|
-
end
|
12
|
-
|
13
|
-
def link(name, options)
|
14
|
-
metadata[:links][name] = options
|
15
|
-
end
|
16
|
-
|
17
|
-
def attribute(name, options)
|
18
|
-
metadata[:attributes][name] = options
|
19
|
-
end
|
20
|
-
|
21
|
-
def metadata
|
22
|
-
@metadata ||= Hash.new {|h,a| h[a] = ActiveSupport::OrderedHash.new}
|
23
|
-
end
|
24
|
-
|
25
|
-
end
|
26
|
-
end
|
27
|
-
end
|
@@ -1,18 +0,0 @@
|
|
1
|
-
module Almodovar
|
2
|
-
class ResourcePresenter
|
3
|
-
class Serializer
|
4
|
-
|
5
|
-
attr_reader :resource, :options
|
6
|
-
|
7
|
-
def initialize(resource, options = {})
|
8
|
-
@resource = resource
|
9
|
-
@options = options
|
10
|
-
end
|
11
|
-
|
12
|
-
def options_for_link
|
13
|
-
options.merge(:dont_expand => Array(options[:dont_expand]) << resource.url)
|
14
|
-
end
|
15
|
-
|
16
|
-
end
|
17
|
-
end
|
18
|
-
end
|
@@ -1,125 +0,0 @@
|
|
1
|
-
<!DOCTYPE html>
|
2
|
-
<html lang='en'>
|
3
|
-
<head>
|
4
|
-
<meta charset='utf-8'/>
|
5
|
-
<style type='text/css'>
|
6
|
-
.highlight .hll { background-color: #ffffcc }
|
7
|
-
.highlight { background: #f8f8f8; }
|
8
|
-
.highlight .c { color: #408080; font-style: italic } /* Comment */
|
9
|
-
.highlight .err { border: 1px solid #FF0000 } /* Error */
|
10
|
-
.highlight .k { color: #008000; font-weight: bold } /* Keyword */
|
11
|
-
.highlight .o { color: #666666 } /* Operator */
|
12
|
-
.highlight .cm { color: #408080; font-style: italic } /* Comment.Multiline */
|
13
|
-
.highlight .cp { color: #BC7A00 } /* Comment.Preproc */
|
14
|
-
.highlight .c1 { color: #408080; font-style: italic } /* Comment.Single */
|
15
|
-
.highlight .cs { color: #408080; font-style: italic } /* Comment.Special */
|
16
|
-
.highlight .gd { color: #A00000 } /* Generic.Deleted */
|
17
|
-
.highlight .ge { font-style: italic } /* Generic.Emph */
|
18
|
-
.highlight .gr { color: #FF0000 } /* Generic.Error */
|
19
|
-
.highlight .gh { color: #000080; font-weight: bold } /* Generic.Heading */
|
20
|
-
.highlight .gi { color: #00A000 } /* Generic.Inserted */
|
21
|
-
.highlight .go { color: #808080 } /* Generic.Output */
|
22
|
-
.highlight .gp { color: #000080; font-weight: bold } /* Generic.Prompt */
|
23
|
-
.highlight .gs { font-weight: bold } /* Generic.Strong */
|
24
|
-
.highlight .gu { color: #800080; font-weight: bold } /* Generic.Subheading */
|
25
|
-
.highlight .gt { color: #0040D0 } /* Generic.Traceback */
|
26
|
-
.highlight .kc { color: #008000; font-weight: bold } /* Keyword.Constant */
|
27
|
-
.highlight .kd { color: #008000; font-weight: bold } /* Keyword.Declaration */
|
28
|
-
.highlight .kn { color: #008000; font-weight: bold } /* Keyword.Namespace */
|
29
|
-
.highlight .kp { color: #008000 } /* Keyword.Pseudo */
|
30
|
-
.highlight .kr { color: #008000; font-weight: bold } /* Keyword.Reserved */
|
31
|
-
.highlight .kt { color: #B00040 } /* Keyword.Type */
|
32
|
-
.highlight .m { color: #666666 } /* Literal.Number */
|
33
|
-
.highlight .s { color: #BA2121 } /* Literal.String */
|
34
|
-
.highlight .na { color: #7D9029 } /* Name.Attribute */
|
35
|
-
.highlight .nb { color: #008000 } /* Name.Builtin */
|
36
|
-
.highlight .nc { color: #0000FF; font-weight: bold } /* Name.Class */
|
37
|
-
.highlight .no { color: #880000 } /* Name.Constant */
|
38
|
-
.highlight .nd { color: #AA22FF } /* Name.Decorator */
|
39
|
-
.highlight .ni { color: #999999; font-weight: bold } /* Name.Entity */
|
40
|
-
.highlight .ne { color: #D2413A; font-weight: bold } /* Name.Exception */
|
41
|
-
.highlight .nf { color: #0000FF } /* Name.Function */
|
42
|
-
.highlight .nl { color: #A0A000 } /* Name.Label */
|
43
|
-
.highlight .nn { color: #0000FF; font-weight: bold } /* Name.Namespace */
|
44
|
-
.highlight .nt { color: #008000; font-weight: bold } /* Name.Tag */
|
45
|
-
.highlight .nv { color: #19177C } /* Name.Variable */
|
46
|
-
.highlight .ow { color: #AA22FF; font-weight: bold } /* Operator.Word */
|
47
|
-
.highlight .w { color: #bbbbbb } /* Text.Whitespace */
|
48
|
-
.highlight .mf { color: #666666 } /* Literal.Number.Float */
|
49
|
-
.highlight .mh { color: #666666 } /* Literal.Number.Hex */
|
50
|
-
.highlight .mi { color: #666666 } /* Literal.Number.Integer */
|
51
|
-
.highlight .mo { color: #666666 } /* Literal.Number.Oct */
|
52
|
-
.highlight .sb { color: #BA2121 } /* Literal.String.Backtick */
|
53
|
-
.highlight .sc { color: #BA2121 } /* Literal.String.Char */
|
54
|
-
.highlight .sd { color: #BA2121; font-style: italic } /* Literal.String.Doc */
|
55
|
-
.highlight .s2 { color: #BA2121 } /* Literal.String.Double */
|
56
|
-
.highlight .se { color: #BB6622; font-weight: bold } /* Literal.String.Escape */
|
57
|
-
.highlight .sh { color: #BA2121 } /* Literal.String.Heredoc */
|
58
|
-
.highlight .si { color: #BB6688; font-weight: bold } /* Literal.String.Interpol */
|
59
|
-
.highlight .sx { color: #008000 } /* Literal.String.Other */
|
60
|
-
.highlight .sr { color: #BB6688 } /* Literal.String.Regex */
|
61
|
-
.highlight .s1 { color: #BA2121 } /* Literal.String.Single */
|
62
|
-
.highlight .ss { color: #19177C } /* Literal.String.Symbol */
|
63
|
-
.highlight .bp { color: #008000 } /* Name.Builtin.Pseudo */
|
64
|
-
.highlight .vc { color: #19177C } /* Name.Variable.Class */
|
65
|
-
.highlight .vg { color: #19177C } /* Name.Variable.Global */
|
66
|
-
.highlight .vi { color: #19177C } /* Name.Variable.Instance */
|
67
|
-
.highlight .il { color: #666666 } /* Literal.Number.Integer.Long */
|
68
|
-
|
69
|
-
body {
|
70
|
-
padding-top: 30px;
|
71
|
-
}
|
72
|
-
</style>
|
73
|
-
<link href="//netdna.bootstrapcdn.com/twitter-bootstrap/2.2.1/css/bootstrap-combined.min.css" rel="stylesheet">
|
74
|
-
</head>
|
75
|
-
<body>
|
76
|
-
<div class='container'>
|
77
|
-
|
78
|
-
<ul class="nav nav-tabs">
|
79
|
-
<li><a href="#xml" data-toggle="tab">XML</a> </li>
|
80
|
-
<li><a href="#help" data-toggle="tab">Reference</a> </li>
|
81
|
-
</ul>
|
82
|
-
|
83
|
-
<div class="tab-content">
|
84
|
-
<div class="tab-pane" id="xml">
|
85
|
-
<%= beautify resource.to_xml(options), :xml %>
|
86
|
-
</div>
|
87
|
-
<div class="tab-pane" id="help">
|
88
|
-
<h2><%= metadata[:name] %></h2>
|
89
|
-
<%= metadata_text metadata[:desc] %>
|
90
|
-
<% if metadata[:attributes].present? %>
|
91
|
-
<h3>Attributes</h3>
|
92
|
-
<dl>
|
93
|
-
<% metadata[:attributes].each do |name, options| %>
|
94
|
-
<dt><%= name %></dt>
|
95
|
-
<dd><%= metadata_text options[:desc] %></dd>
|
96
|
-
<% end %>
|
97
|
-
</dl>
|
98
|
-
<% end %>
|
99
|
-
<% if metadata[:links].present? %>
|
100
|
-
<h3>Links</h3>
|
101
|
-
<dl>
|
102
|
-
<% metadata[:links].each do |name, options| %>
|
103
|
-
<dt><%= name %></dt>
|
104
|
-
<dd><%= metadata_text options[:desc] %></dd>
|
105
|
-
<% end %>
|
106
|
-
</dl>
|
107
|
-
<% end %>
|
108
|
-
</div>
|
109
|
-
</div>
|
110
|
-
|
111
|
-
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
|
112
|
-
<script src="//netdna.bootstrapcdn.com/twitter-bootstrap/2.2.1/js/bootstrap.min.js"></script>
|
113
|
-
<script>
|
114
|
-
$(function () {
|
115
|
-
var currentTab = localStorage.currentTab || '#xml'
|
116
|
-
$('ul.nav a[href="' + currentTab + '"]').tab('show')
|
117
|
-
|
118
|
-
$('a[data-toggle="tab"]').on('shown', function (e) {
|
119
|
-
localStorage.currentTab = $(e.target).attr('href')
|
120
|
-
})
|
121
|
-
})
|
122
|
-
</script>
|
123
|
-
</div>
|
124
|
-
</body>
|
125
|
-
</html>
|
@@ -1,25 +0,0 @@
|
|
1
|
-
module Almodovar
|
2
|
-
class ResourcePresenter
|
3
|
-
class XmlSerializer < Serializer
|
4
|
-
|
5
|
-
def to_xml
|
6
|
-
attributes_to_xml do |builder|
|
7
|
-
links_to_xml builder
|
8
|
-
end
|
9
|
-
end
|
10
|
-
|
11
|
-
private
|
12
|
-
|
13
|
-
def attributes_to_xml(&block)
|
14
|
-
resource.attributes.to_xml(options.merge(:root => resource.resource_type), &block)
|
15
|
-
end
|
16
|
-
|
17
|
-
def links_to_xml(builder)
|
18
|
-
resource.all_links.each do |link|
|
19
|
-
link.to_xml(options_for_link.merge(:builder => builder))
|
20
|
-
end
|
21
|
-
end
|
22
|
-
|
23
|
-
end
|
24
|
-
end
|
25
|
-
end
|