almodovar 0.7.0 → 0.8.0
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/almodovar/resource_presenter/collection.rb +53 -47
- data/lib/almodovar/resource_presenter/html_serializer.rb +44 -0
- data/lib/almodovar/resource_presenter/json_serializer.rb +31 -0
- data/lib/almodovar/resource_presenter/link.rb +79 -79
- data/lib/almodovar/resource_presenter/metadata.rb +27 -0
- data/lib/almodovar/resource_presenter/serializer.rb +18 -0
- data/lib/almodovar/resource_presenter/template.html.erb +130 -0
- data/lib/almodovar/resource_presenter/xml_serializer.rb +25 -0
- data/lib/almodovar/resource_presenter.rb +17 -66
- data/lib/almodovar/version.rb +1 -1
- metadata +11 -5
@@ -1,55 +1,61 @@
|
|
1
1
|
module Almodovar
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
@resource_class = resource_class
|
7
|
-
@resources = resources_args.map { |arg| @resource_class.new(arg) }
|
2
|
+
class ResourcePresenter
|
3
|
+
class Collection
|
4
|
+
|
5
|
+
attr_reader :resource_class
|
8
6
|
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
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)
|
19
21
|
|
20
|
-
|
21
|
-
|
22
|
-
|
22
|
+
options = options.dup
|
23
|
+
options[:indent] ||= 2
|
24
|
+
options[:builder] ||= ::Builder::XmlMarkup.new(:indent => options[:indent])
|
23
25
|
|
24
|
-
|
26
|
+
xml = options[:builder]
|
25
27
|
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
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
|
+
xml.link :rel => 'next', :href => @next if @next
|
32
|
+
xml.link :rel => 'prev', :href => @prev if @prev
|
33
|
+
@resources.each { |value| value.to_xml(options.merge(:root => resource_type.singularize, :skip_instruct => true)) }
|
34
|
+
end
|
32
35
|
end
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
36
|
+
|
37
|
+
def as_json(options = {})
|
38
|
+
ActiveSupport::OrderedHash.new.tap do |message|
|
39
|
+
message[:total_entries] = @total if @total
|
40
|
+
message[:next_link] = @next if @next
|
41
|
+
message[:prev_link] = @prev if @prev
|
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
|
41
57
|
end
|
42
|
-
end
|
43
|
-
|
44
|
-
def to_json(options = {})
|
45
|
-
require 'yajl'
|
46
|
-
Yajl::Encoder.encode(as_json(options), :pretty => true) + "\n"
|
47
|
-
end
|
48
|
-
|
49
|
-
def resource_type
|
50
|
-
@resource_class.resource_type
|
51
|
-
end
|
52
58
|
|
53
|
-
|
54
|
-
|
55
|
-
end
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
@@ -0,0 +1,44 @@
|
|
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
|
@@ -0,0 +1,31 @@
|
|
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,103 +1,103 @@
|
|
1
1
|
module Almodovar
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
attr_reader :rel, :href, :expand_resource, :expand_args
|
6
|
-
|
7
|
-
def initialize(*args)
|
8
|
-
@rel, @href, @expand_resource, @expand_args = args
|
9
|
-
end
|
10
|
-
|
11
|
-
def to_xml(options = {})
|
12
|
-
XmlSerializer.new(self, options.merge(:skip_instruct => true)).to_xml
|
13
|
-
end
|
14
|
-
|
15
|
-
def as_json(options)
|
16
|
-
JsonSerializer.new(self, options).as_json
|
17
|
-
end
|
18
|
-
|
19
|
-
def resource
|
20
|
-
resource_collection? ? resource_collection : single_resource
|
21
|
-
end
|
22
|
-
|
23
|
-
def resource_collection?
|
24
|
-
expand_args.is_a?(Array)
|
25
|
-
end
|
26
|
-
|
27
|
-
def resource_collection
|
28
|
-
ResourcePresenter::Collection.new(expand_resource, expand_args)
|
29
|
-
end
|
30
|
-
|
31
|
-
def single_resource
|
32
|
-
expand_resource.new(*[expand_args].compact)
|
33
|
-
end
|
34
|
-
|
35
|
-
def expand_resource?
|
36
|
-
expand_resource.present?
|
37
|
-
end
|
38
|
-
|
39
|
-
class Serializer
|
2
|
+
class ResourcePresenter
|
3
|
+
class Link
|
40
4
|
|
41
|
-
attr_reader :
|
5
|
+
attr_reader :rel, :href, :expand_resource, :expand_args
|
42
6
|
|
43
|
-
def initialize(
|
44
|
-
@
|
45
|
-
@options = options
|
7
|
+
def initialize(*args)
|
8
|
+
@rel, @href, @expand_resource, @expand_args = args
|
46
9
|
end
|
47
10
|
|
48
|
-
def
|
49
|
-
|
50
|
-
Array(options[:expand]).include?(link.rel) &&
|
51
|
-
!Array(options[:dont_expand]).include?(link.href)
|
11
|
+
def to_xml(options = {})
|
12
|
+
XmlSerializer.new(self, options.merge(:skip_instruct => true)).to_xml
|
52
13
|
end
|
53
14
|
|
54
|
-
def
|
55
|
-
(options
|
15
|
+
def as_json(options)
|
16
|
+
JsonSerializer.new(self, options).as_json
|
56
17
|
end
|
57
18
|
|
58
|
-
|
59
|
-
|
60
|
-
class XmlSerializer < Serializer
|
61
|
-
|
62
|
-
def to_xml
|
63
|
-
builder.link :rel => link.rel, :href => link.href, &expand_block
|
19
|
+
def resource
|
20
|
+
resource_collection? ? resource_collection : single_resource
|
64
21
|
end
|
65
22
|
|
66
|
-
|
67
|
-
|
68
|
-
def builder
|
69
|
-
options[:builder]
|
23
|
+
def resource_collection?
|
24
|
+
expand_args.is_a?(Array)
|
70
25
|
end
|
71
26
|
|
72
|
-
def
|
73
|
-
|
27
|
+
def resource_collection
|
28
|
+
ResourcePresenter::Collection.new(expand_resource, expand_args)
|
74
29
|
end
|
75
|
-
|
76
|
-
def
|
77
|
-
|
78
|
-
link.resource.to_xml(options)
|
30
|
+
|
31
|
+
def single_resource
|
32
|
+
expand_resource.new(*[expand_args].compact)
|
79
33
|
end
|
80
34
|
|
81
|
-
|
82
|
-
|
83
|
-
|
35
|
+
def expand_resource?
|
36
|
+
expand_resource.present?
|
37
|
+
end
|
84
38
|
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
39
|
+
class Serializer
|
40
|
+
|
41
|
+
attr_reader :link, :options
|
42
|
+
|
43
|
+
def initialize(link, options)
|
44
|
+
@link = link
|
45
|
+
@options = options
|
89
46
|
end
|
47
|
+
|
48
|
+
def expands?
|
49
|
+
link.expand_resource? &&
|
50
|
+
Array(options[:expand]).include?(link.rel) &&
|
51
|
+
!Array(options[:dont_expand]).include?(link.href)
|
52
|
+
end
|
53
|
+
|
54
|
+
def dont_expand_link!
|
55
|
+
(options[:dont_expand] ||= []) << link.href
|
56
|
+
end
|
57
|
+
|
90
58
|
end
|
91
59
|
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
60
|
+
class XmlSerializer < Serializer
|
61
|
+
|
62
|
+
def to_xml
|
63
|
+
builder.link :rel => link.rel, :href => link.href, &expand_block
|
64
|
+
end
|
65
|
+
|
66
|
+
private
|
67
|
+
|
68
|
+
def builder
|
69
|
+
options[:builder]
|
70
|
+
end
|
71
|
+
|
72
|
+
def expand_block
|
73
|
+
Proc.new { expand_resource } if expands?
|
74
|
+
end
|
75
|
+
|
76
|
+
def expand_resource
|
77
|
+
dont_expand_link!
|
78
|
+
link.resource.to_xml(options)
|
79
|
+
end
|
80
|
+
|
97
81
|
end
|
82
|
+
|
83
|
+
class JsonSerializer < Serializer
|
84
|
+
|
85
|
+
def as_json
|
86
|
+
ActiveSupport::OrderedHash.new.tap do |message|
|
87
|
+
message["#{link.rel}_link"] = link.href
|
88
|
+
message[link.rel] = expand_resource if expands?
|
89
|
+
end
|
90
|
+
end
|
91
|
+
|
92
|
+
private
|
93
|
+
|
94
|
+
def expand_resource
|
95
|
+
dont_expand_link!
|
96
|
+
link.resource.as_json(options)
|
97
|
+
end
|
98
98
|
|
99
|
+
end
|
100
|
+
|
99
101
|
end
|
100
|
-
|
101
102
|
end
|
102
|
-
|
103
|
-
end
|
103
|
+
end
|
@@ -0,0 +1,27 @@
|
|
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
|
@@ -0,0 +1,18 @@
|
|
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
|
@@ -0,0 +1,130 @@
|
|
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="#json" data-toggle="tab">JSON</a> </li>
|
81
|
+
<li><a href="#help" data-toggle="tab">Reference</a> </li>
|
82
|
+
</ul>
|
83
|
+
|
84
|
+
<div class="tab-content">
|
85
|
+
<div class="tab-pane" id="xml">
|
86
|
+
<%= beautify resource.to_xml(options), :xml %>
|
87
|
+
</div>
|
88
|
+
<div class="tab-pane" id="json">
|
89
|
+
<p><em>JSON support is experimental and it's subject to change in the future</em></p>
|
90
|
+
<%= beautify resource.to_json(options), :json %>
|
91
|
+
</div>
|
92
|
+
<div class="tab-pane" id="help">
|
93
|
+
<h2><%= metadata[:name] %></h2>
|
94
|
+
<%= metadata_text metadata[:desc] %>
|
95
|
+
<% if metadata[:attributes].present? %>
|
96
|
+
<h3>Attributes</h3>
|
97
|
+
<dl>
|
98
|
+
<% metadata[:attributes].each do |name, options| %>
|
99
|
+
<dt><%= name %></dt>
|
100
|
+
<dd><%= metadata_text options[:desc] %></dd>
|
101
|
+
<% end %>
|
102
|
+
</dl>
|
103
|
+
<% end %>
|
104
|
+
<% if metadata[:links].present? %>
|
105
|
+
<h3>Links</h3>
|
106
|
+
<dl>
|
107
|
+
<% metadata[:links].each do |name, options| %>
|
108
|
+
<dt><%= name %></dt>
|
109
|
+
<dd><%= metadata_text options[:desc] %></dd>
|
110
|
+
<% end %>
|
111
|
+
</dl>
|
112
|
+
<% end %>
|
113
|
+
</div>
|
114
|
+
</div>
|
115
|
+
|
116
|
+
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
|
117
|
+
<script src="//netdna.bootstrapcdn.com/twitter-bootstrap/2.2.1/js/bootstrap.min.js"></script>
|
118
|
+
<script>
|
119
|
+
$(function () {
|
120
|
+
var currentTab = localStorage.currentTab || '#xml'
|
121
|
+
$('ul.nav a[href="' + currentTab + '"]').tab('show')
|
122
|
+
|
123
|
+
$('a[data-toggle="tab"]').on('shown', function (e) {
|
124
|
+
localStorage.currentTab = $(e.target).attr('href')
|
125
|
+
})
|
126
|
+
})
|
127
|
+
</script>
|
128
|
+
</div>
|
129
|
+
</body>
|
130
|
+
</html>
|
@@ -0,0 +1,25 @@
|
|
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
|
@@ -2,6 +2,14 @@ module Almodovar
|
|
2
2
|
|
3
3
|
class ResourcePresenter
|
4
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
|
+
|
5
13
|
attr_accessor :url
|
6
14
|
|
7
15
|
def attributes
|
@@ -16,8 +24,12 @@ module Almodovar
|
|
16
24
|
name.gsub(/Resource$/, '').underscore
|
17
25
|
end
|
18
26
|
|
27
|
+
def resource_class
|
28
|
+
self.class
|
29
|
+
end
|
30
|
+
|
19
31
|
def resource_type
|
20
|
-
|
32
|
+
resource_class.resource_type
|
21
33
|
end
|
22
34
|
|
23
35
|
def to_xml(options = {})
|
@@ -31,6 +43,10 @@ module Almodovar
|
|
31
43
|
def as_json(options = {})
|
32
44
|
JsonSerializer.new(self, options).as_json
|
33
45
|
end
|
46
|
+
|
47
|
+
def to_html(options = {})
|
48
|
+
HtmlSerializer.new(self, options).to_html
|
49
|
+
end
|
34
50
|
|
35
51
|
def all_links
|
36
52
|
([link_to_self] + links).compact
|
@@ -39,71 +55,6 @@ module Almodovar
|
|
39
55
|
def link_to_self
|
40
56
|
Link.new(:self, @url) if @url
|
41
57
|
end
|
42
|
-
|
43
|
-
class Serializer
|
44
|
-
|
45
|
-
attr_reader :resource, :options
|
46
|
-
|
47
|
-
def initialize(resource, options)
|
48
|
-
@resource = resource
|
49
|
-
@options = options
|
50
|
-
end
|
51
|
-
|
52
|
-
def options_for_link
|
53
|
-
options.merge(:dont_expand => Array(options[:dont_expand]) << resource.url)
|
54
|
-
end
|
55
|
-
|
56
|
-
end
|
57
|
-
|
58
|
-
class XmlSerializer < Serializer
|
59
|
-
|
60
|
-
def to_xml
|
61
|
-
attributes_to_xml do |builder|
|
62
|
-
links_to_xml builder
|
63
|
-
end
|
64
|
-
end
|
65
|
-
|
66
|
-
private
|
67
|
-
|
68
|
-
def attributes_to_xml(&block)
|
69
|
-
resource.attributes.to_xml(options.merge(:root => resource.resource_type), &block)
|
70
|
-
end
|
71
|
-
|
72
|
-
def links_to_xml(builder)
|
73
|
-
resource.all_links.each do |link|
|
74
|
-
link.to_xml(options_for_link.merge(:builder => builder))
|
75
|
-
end
|
76
|
-
end
|
77
|
-
|
78
|
-
end
|
79
|
-
|
80
|
-
class JsonSerializer < Serializer
|
81
|
-
|
82
|
-
def to_json
|
83
|
-
require 'yajl'
|
84
|
-
Yajl::Encoder.encode(as_json, :pretty => true) + "\n"
|
85
|
-
end
|
86
|
-
|
87
|
-
def as_json
|
88
|
-
ActiveSupport::OrderedHash[:resource_type, resource.resource_type].tap do |message|
|
89
|
-
message.merge! attributes_as_json
|
90
|
-
message.merge! links_as_json
|
91
|
-
end
|
92
|
-
end
|
93
|
-
|
94
|
-
private
|
95
|
-
|
96
|
-
def attributes_as_json
|
97
|
-
resource.attributes
|
98
|
-
end
|
99
|
-
|
100
|
-
def links_as_json
|
101
|
-
resource.all_links.inject(ActiveSupport::OrderedHash.new) do |message, link|
|
102
|
-
message.merge! link.as_json(options_for_link)
|
103
|
-
end
|
104
|
-
end
|
105
|
-
|
106
|
-
end
|
107
58
|
|
108
59
|
end
|
109
60
|
|
data/lib/almodovar/version.rb
CHANGED
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: almodovar
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 63
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
|
-
-
|
8
|
+
- 8
|
9
9
|
- 0
|
10
|
-
version: 0.
|
10
|
+
version: 0.8.0
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- BeBanjo S.L.
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date:
|
18
|
+
date: 2013-03-01 00:00:00 Z
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|
21
21
|
name: patron
|
@@ -117,7 +117,13 @@ files:
|
|
117
117
|
- lib/almodovar/resource.rb
|
118
118
|
- lib/almodovar/resource_collection.rb
|
119
119
|
- lib/almodovar/resource_presenter/collection.rb
|
120
|
+
- lib/almodovar/resource_presenter/html_serializer.rb
|
121
|
+
- lib/almodovar/resource_presenter/json_serializer.rb
|
120
122
|
- lib/almodovar/resource_presenter/link.rb
|
123
|
+
- lib/almodovar/resource_presenter/metadata.rb
|
124
|
+
- lib/almodovar/resource_presenter/serializer.rb
|
125
|
+
- lib/almodovar/resource_presenter/template.html.erb
|
126
|
+
- lib/almodovar/resource_presenter/xml_serializer.rb
|
121
127
|
- lib/almodovar/resource_presenter.rb
|
122
128
|
- lib/almodovar/single_resource.rb
|
123
129
|
- lib/almodovar/to_xml.rb
|
@@ -153,7 +159,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
153
159
|
requirements: []
|
154
160
|
|
155
161
|
rubyforge_project:
|
156
|
-
rubygems_version: 1.8.
|
162
|
+
rubygems_version: 1.8.15
|
157
163
|
signing_key:
|
158
164
|
specification_version: 3
|
159
165
|
summary: BeBanjo API client
|