api-presenter 0.0.1

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/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Álvaro Lara
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,29 @@
1
+ # Api::Presenter
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'api-presenter'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install api-presenter
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,7 @@
1
+ require "bundler/gem_tasks"
2
+ require 'rake/testtask'
3
+
4
+ Rake::TestTask.new('spec') do |t|
5
+ t.test_files = FileList['spec/*_spec.rb']
6
+ t.verbose = true
7
+ end
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'api/presenter/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "api-presenter"
8
+ spec.version = Api::Presenter::VERSION
9
+ spec.authors = ["Álvaro Lara"]
10
+ spec.email = ["alvarola@gmail.com"]
11
+ spec.description = 'A JSON resource presenter for a specific api media type'
12
+ spec.summary = 'A JSON resource presenter for a specific api media type'
13
+ spec.homepage = "https://github.com/guiman/api-presenter"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.3"
22
+ spec.add_development_dependency "rake"
23
+
24
+ spec.add_dependency "multi_json", "~> 1.7.4"
25
+ spec.add_dependency "json", "~> 1.8.0"
26
+ end
@@ -0,0 +1,16 @@
1
+ module Api
2
+ module Presenter
3
+ class CollectionResource < Resource
4
+ def self.hypermedia_properties
5
+ {
6
+ simple: [:offset, :limit, :total,:entries],
7
+ resource: []
8
+ }
9
+ end
10
+
11
+ def entries
12
+ @resource
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,54 @@
1
+ module Api
2
+ module Presenter
3
+ class Hypermedia
4
+ def self.present(resource)
5
+ # Initialize representation with links
6
+ representation = build_links resource
7
+
8
+ present_properties resource, representation
9
+
10
+ MultiJson.dump representation
11
+ end
12
+
13
+ # Process basic information from the resource such as
14
+ # simlple fields(Dates, numbers, etc.) and also more
15
+ # complex ones that may be subject of expansion.
16
+ def self.present_properties(resource, representation)
17
+ properties = resource.class.hypermedia_properties
18
+
19
+ present_simple_properties resource, properties, representation
20
+
21
+ present_resource_properties resource, properties, representation
22
+ end
23
+
24
+ def self.present_simple_properties(resource, properties, representation)
25
+ entries_property = properties[:simple].delete(:entries)
26
+
27
+ if entries_property
28
+ representation[entries_property.to_s] = []
29
+ resource.send(entries_property).each do |nested_resource|
30
+ representation[entries_property.to_s] << nested_resource.to_resource.links(embed: true)
31
+ end
32
+ end
33
+
34
+ properties[:simple].each do |property|
35
+ representation[property.to_s] = resource.send(property)
36
+ end
37
+ end
38
+
39
+ def self.present_resource_properties(resource, properties, representation)
40
+ properties[:resource].each do |property|
41
+ relation = resource.send(property)
42
+ relation_resource = (relation.kind_of? Resource) ? relation : relation.to_resource
43
+ representation["links"][property.to_s] = relation_resource.links(embed: resource.kind_of?(CollectionResource))
44
+ # we only need the "self" contents
45
+ representation["links"][property.to_s] = representation["links"][property.to_s]["self"] if representation["links"][property.to_s]["self"]
46
+ end
47
+ end
48
+
49
+ def self.build_links(resource, options = {})
50
+ { "links" => resource.links(options) }
51
+ end
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,34 @@
1
+ module Api
2
+ module Presenter
3
+ class Resource
4
+ def initialize(resource)
5
+ @resource = resource
6
+ end
7
+
8
+ def method_missing(method, *args, &block)
9
+ allowed_methods = self.class.hypermedia_properties[:simple].concat self.class.hypermedia_properties[:resource]
10
+
11
+ if allowed_methods.include? method
12
+ @resource.send(method, *args, &block)
13
+ else
14
+ super
15
+ end
16
+ end
17
+
18
+ def links(options = {})
19
+ links = {}
20
+
21
+ self.methods.grep(/_link$/).each do |link_method|
22
+ link_name = link_method.to_s.split("_").first
23
+ links[link_name] = { "href" => self.send(link_method) } if self.send(link_method.to_s + "?", options)
24
+ end
25
+
26
+ links
27
+ end
28
+
29
+ def self_link?(options = {})
30
+ true
31
+ end
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,25 @@
1
+ module Api
2
+ module Presenter
3
+ class SearchResource < CollectionResource
4
+ attr_reader :query
5
+
6
+ def initialize(resource, query = {})
7
+ @resource = resource
8
+ @query = query
9
+ end
10
+
11
+ def self.hypermedia_properties
12
+ properties = super
13
+
14
+ properties[:simple] = properties[:simple].concat [:query]
15
+
16
+ properties
17
+ end
18
+
19
+ def query_string
20
+ result = self.class.hypermedia_query_parameters.inject([]) { |col, query_parameter| col << "query[#{query_parameter}]=#{@query[query_parameter]}" }
21
+ "?" + result.join("&")
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,5 @@
1
+ module Api
2
+ module Presenter
3
+ VERSION = "0.0.1"
4
+ end
5
+ end
@@ -0,0 +1,10 @@
1
+ require "api/presenter/version"
2
+ require "api/presenter/hypermedia"
3
+ require "api/presenter/resource"
4
+ require "api/presenter/collection_resource"
5
+ require "api/presenter/search_resource"
6
+
7
+ module Api
8
+ module Presenter
9
+ end
10
+ end
@@ -0,0 +1,82 @@
1
+ module HypertextPresenterMocks
2
+ class MockData
3
+ attr_accessor :number, :string, :date
4
+
5
+ def initialize(attributes = {})
6
+ @number = attributes[:number]
7
+ @string = attributes[:string]
8
+ @date = attributes[:date]
9
+ end
10
+
11
+ def to_resource
12
+ MockSingleResource.new(self)
13
+ end
14
+ end
15
+
16
+ class Collection
17
+ def initialize(arr)
18
+ @arr = arr
19
+ end
20
+
21
+ def offset
22
+ 0
23
+ end
24
+
25
+ def limit
26
+ 15
27
+ end
28
+
29
+ def total
30
+ @arr.count
31
+ end
32
+
33
+ def each(&block)
34
+ @arr.each &block
35
+ end
36
+ end
37
+
38
+ class MockSingleResource < Api::Presenter::Resource
39
+ def self.hypermedia_properties
40
+ {
41
+ simple: [:number, :string, :date],
42
+ resource: [:sibling]
43
+ }
44
+ end
45
+
46
+ def self_link
47
+ "/path/to/single_resource/#{@resource.number}"
48
+ end
49
+
50
+ def custom_link
51
+ "/path/to/custom_link"
52
+ end
53
+
54
+ def custom_link?(options = {})
55
+ options[:embed].nil? || !options[:embed]
56
+ end
57
+
58
+ def sibling
59
+ MockSingleResource.new(MockData.new(number: 20, string: "I'm a sibling of someone", date: (Date.today + 1)))
60
+ end
61
+ end
62
+
63
+ class MockCollectionResource < Api::Presenter::CollectionResource
64
+ def self_link
65
+ "/path/to/collection_resource"
66
+ end
67
+ end
68
+
69
+ class MockSearchResource < Api::Presenter::SearchResource
70
+ def self.hypermedia_query_parameters
71
+ ["page", "param1","param2"]
72
+ end
73
+
74
+ def self_link
75
+ "/path/to/search_resource#{query_string}"
76
+ end
77
+
78
+ def current_page
79
+ 1
80
+ end
81
+ end
82
+ end
@@ -0,0 +1,157 @@
1
+ require_relative './spec_helper'
2
+ require_relative './hypertext_presenter_mocks'
3
+
4
+ include HypertextPresenterMocks
5
+
6
+ describe Api::Presenter::Hypermedia do
7
+ let(:collection_resource_standard)do
8
+ {
9
+ "links" =>
10
+ {
11
+ "self" =>
12
+ {
13
+ "href" => "/path/to/collection_resource"
14
+ }
15
+ },
16
+
17
+ "offset" => 0,
18
+ "limit" => 15,
19
+ "total" => 4,
20
+
21
+ "entries" =>
22
+ [
23
+ {
24
+ "self" =>
25
+ {
26
+ "href" => "/path/to/single_resource/1"
27
+ }
28
+ },
29
+ {
30
+ "self" =>
31
+ {
32
+ "href" => "/path/to/single_resource/2"
33
+ }
34
+ },
35
+ {
36
+ "self" =>
37
+ {
38
+ "href" => "/path/to/single_resource/3"
39
+ }
40
+ },
41
+ {
42
+ "self" =>
43
+ {
44
+ "href" => "/path/to/single_resource/4"
45
+ }
46
+ }
47
+ ]
48
+ }
49
+ end
50
+ let(:single_resource_standard) do
51
+ {
52
+ "links" =>
53
+ {
54
+ "self" =>
55
+ {
56
+ "href" => "/path/to/single_resource/1"
57
+ },
58
+ "custom" =>
59
+ {
60
+ "href" => "/path/to/custom_link"
61
+ },
62
+ "sibling" =>
63
+ {
64
+ "href" => "/path/to/single_resource/20"
65
+ }
66
+ },
67
+ "number" => 1,
68
+ "string" => 'This is a string',
69
+ "date" => Date.today.to_s
70
+ }
71
+ end
72
+ let(:search_resource_standard)do
73
+ {
74
+ "links" =>
75
+ {
76
+ "self" =>
77
+ {
78
+ "href" => "/path/to/search_resource?query[page]=1&query[param1]=1&query[param2]=string"
79
+ }
80
+ },
81
+
82
+ "offset" => 0,
83
+ "limit" => 15,
84
+ "total" => 4,
85
+
86
+ "query" =>
87
+ {
88
+ "page" => 1,
89
+ "param1" => 1,
90
+ "param2" => "string"
91
+ },
92
+
93
+ "entries" =>
94
+ [ {
95
+ "self" =>
96
+ {
97
+ "href" => "/path/to/single_resource/1"
98
+ }
99
+ },
100
+ {
101
+ "self" =>
102
+ {
103
+ "href" => "/path/to/single_resource/2"
104
+ }
105
+ },
106
+ {
107
+ "self" =>
108
+ {
109
+ "href" => "/path/to/single_resource/3"
110
+ }
111
+ },
112
+ {
113
+ "self" =>
114
+ {
115
+ "href" => "/path/to/single_resource/4"
116
+ }
117
+ } ]
118
+ }
119
+ end
120
+
121
+ describe "when presenting a single resource" do
122
+
123
+ let(:mock_data) { MockData.new(number: 1, string: 'This is a string', date: Date.today) }
124
+ let(:single_resource){ MockSingleResource.new mock_data }
125
+ let(:presented_single_resource){ Api::Presenter::Hypermedia.present single_resource }
126
+
127
+ it "must respect the standard" do
128
+ MultiJson.load(presented_single_resource).must_equal single_resource_standard
129
+ end
130
+ end
131
+
132
+ describe "presenting a collection resource" do
133
+
134
+ let(:mock_data_collection) do
135
+ collection = []
136
+ 4.times { |number| collection << MockData.new(number: number + 1, string: 'This is a string', date: Date.today) }
137
+ Collection.new(collection)
138
+ end
139
+
140
+ let(:collection_resource){ MockCollectionResource.new mock_data_collection }
141
+
142
+ let(:presented_collection_resource){ Api::Presenter::Hypermedia.present collection_resource }
143
+
144
+ it "must respect the standard" do
145
+ MultiJson.load(presented_collection_resource).must_equal collection_resource_standard
146
+ end
147
+
148
+ describe "presenting a search resource" do
149
+ let(:search_resource){ MockSearchResource.new mock_data_collection, "page" => 1, "param1" => 1, "param2" => "string" }
150
+ let(:presented_search_resource){ Api::Presenter::Hypermedia.present search_resource }
151
+
152
+ it "must respect the standard" do
153
+ MultiJson.load(presented_search_resource).must_equal search_resource_standard
154
+ end
155
+ end
156
+ end
157
+ end
@@ -0,0 +1,7 @@
1
+ require 'bundler'
2
+ require 'minitest/autorun'
3
+ require 'multi_json'
4
+ require 'date'
5
+ require 'time'
6
+
7
+ Bundler.require
metadata ADDED
@@ -0,0 +1,129 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: api-presenter
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Álvaro Lara
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-06-03 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: bundler
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '1.3'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: '1.3'
30
+ - !ruby/object:Gem::Dependency
31
+ name: rake
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: multi_json
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ~>
52
+ - !ruby/object:Gem::Version
53
+ version: 1.7.4
54
+ type: :runtime
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: 1.7.4
62
+ - !ruby/object:Gem::Dependency
63
+ name: json
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ~>
68
+ - !ruby/object:Gem::Version
69
+ version: 1.8.0
70
+ type: :runtime
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ~>
76
+ - !ruby/object:Gem::Version
77
+ version: 1.8.0
78
+ description: A JSON resource presenter for a specific api media type
79
+ email:
80
+ - alvarola@gmail.com
81
+ executables: []
82
+ extensions: []
83
+ extra_rdoc_files: []
84
+ files:
85
+ - .gitignore
86
+ - Gemfile
87
+ - LICENSE.txt
88
+ - README.md
89
+ - Rakefile
90
+ - api-presenter.gemspec
91
+ - lib/api/presenter.rb
92
+ - lib/api/presenter/collection_resource.rb
93
+ - lib/api/presenter/hypermedia.rb
94
+ - lib/api/presenter/resource.rb
95
+ - lib/api/presenter/search_resource.rb
96
+ - lib/api/presenter/version.rb
97
+ - spec/hypertext_presenter_mocks.rb
98
+ - spec/hypertext_presenter_spec.rb
99
+ - spec/spec_helper.rb
100
+ homepage: https://github.com/guiman/api-presenter
101
+ licenses:
102
+ - MIT
103
+ post_install_message:
104
+ rdoc_options: []
105
+ require_paths:
106
+ - lib
107
+ required_ruby_version: !ruby/object:Gem::Requirement
108
+ none: false
109
+ requirements:
110
+ - - ! '>='
111
+ - !ruby/object:Gem::Version
112
+ version: '0'
113
+ required_rubygems_version: !ruby/object:Gem::Requirement
114
+ none: false
115
+ requirements:
116
+ - - ! '>='
117
+ - !ruby/object:Gem::Version
118
+ version: '0'
119
+ requirements: []
120
+ rubyforge_project:
121
+ rubygems_version: 1.8.23
122
+ signing_key:
123
+ specification_version: 3
124
+ summary: A JSON resource presenter for a specific api media type
125
+ test_files:
126
+ - spec/hypertext_presenter_mocks.rb
127
+ - spec/hypertext_presenter_spec.rb
128
+ - spec/spec_helper.rb
129
+ has_rdoc: