api-presenter 0.1.0 → 0.1.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.
@@ -0,0 +1,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ NDg5ZTVjMDA3M2E4YWQxZjM4YzFmOTY3MzJlNjk5NmZlNjczMjE5ZQ==
5
+ data.tar.gz: !binary |-
6
+ YjJhY2EwMjM4NzdmNWNiMzNhN2FmN2Y4MGExZWQ5MTBlZjEyMzFhZQ==
7
+ !binary "U0hBNTEy":
8
+ metadata.gz: !binary |-
9
+ NjQ1NzkyZTExODQxOWE3ZDA1ZWQyNzBjNjAzYjg0ZTAyYjVmNDFkMzcxZjNh
10
+ MzFlYzdjYTE0NjBkZmFhZDhkMzA0MWM3ZTA4NjFlZjc1Y2E3NjQ3YWY0MjIy
11
+ ZmEwMTg2OTI1YWJhZGEyN2VhOTk1ODMwZjRmZTVmODM4NzM3YTM=
12
+ data.tar.gz: !binary |-
13
+ MzM0ZmJjZGNhM2I0ZDdiYmY4MDc1MmI0MWI4NWFkNzBkNTc4OWRmNDQzZDFh
14
+ ZTJjZTIxOTZjY2UyMWYyZDdlMTI5MzgyNzA1NzFkYjEyMWUxNjExMGI5NmRj
15
+ OGQ4NWJlNzkyOTkxNGVjY2UwZWM3ZDJjMGRhNGFlMGZjZTM1MzU=
data/README.md CHANGED
@@ -1,4 +1,6 @@
1
1
  # Api::Presenter
2
+ [![Build Status](https://travis-ci.org/guiman/api-presenter.png?branch=complete_url_feature)](https://travis-ci.org/guiman/api-presenter)
3
+ [![Code Climate](https://codeclimate.com/github/guiman/api-presenter.png)](https://codeclimate.com/github/guiman/api-presenter)
2
4
 
3
5
  This gem builds the basics for presenting your data using the media type described in the [api doc](https://github.com/ncuesta/api-doc). Here you will find classes to represent your resources
4
6
  and also the functions to convert them to a ruby hash. It's your decision how to export them.
@@ -23,15 +23,23 @@ module Api
23
23
  def links
24
24
  @links ||= {}
25
25
  end
26
-
26
+
27
27
  def host
28
28
  @@host ||= ''
29
29
  end
30
-
30
+
31
31
  def host=(v)
32
32
  @@host = v
33
33
  end
34
34
 
35
+ def prefix
36
+ @@prefix ||= ''
37
+ end
38
+
39
+ def prefix=(v)
40
+ @@prefix = v
41
+ end
42
+
35
43
  def inherited(subclass)
36
44
  (subclass.properties << properties).flatten!
37
45
  end
@@ -50,15 +58,15 @@ module Api
50
58
 
51
59
  self.class.links.each do |link_name, link_value|
52
60
  link_actual_value = link_value[:value].dup
53
-
61
+
54
62
  # retrieve stubs to replace looking for {{method_to_call}}
55
63
  stubs = link_actual_value.scan(/\{\{(\w+)\}\}/).flatten
56
-
64
+
57
65
  # now we replace them
58
66
  stubs.each{ |stub| link_actual_value.gsub!(/\{\{#{stub}\}\}/, self.send(stub.to_sym).to_s) }
59
-
67
+
60
68
  # and finish the url
61
- links[link_name.to_s] = { "href" => "#{self.class.host}#{link_actual_value}" } if link_value[:condition].call(options)
69
+ links[link_name.to_s] = { "href" => "#{self.class.host}#{self.class.prefix}#{link_actual_value}" } if link_value[:condition].call(options)
62
70
  end
63
71
 
64
72
  links
@@ -67,7 +75,7 @@ module Api
67
75
  def self_link?(options = {})
68
76
  true
69
77
  end
70
-
78
+
71
79
  def present
72
80
  Api::Presenter::Hypermedia.present self
73
81
  end
@@ -1,5 +1,5 @@
1
1
  module Api
2
2
  module Presenter
3
- VERSION = "0.1.0"
3
+ VERSION = "0.1.1"
4
4
  end
5
5
  end
@@ -96,7 +96,7 @@ describe Api::Presenter::Hypermedia do
96
96
  "offset" => 0,
97
97
  "limit" => 15,
98
98
  "total" => 4,
99
-
99
+
100
100
  "query" =>
101
101
  {
102
102
  "page" => 1,
@@ -154,7 +154,7 @@ describe Api::Presenter::Hypermedia do
154
154
  def floating_point_value
155
155
  10.3
156
156
  end
157
-
157
+
158
158
  def to_resource
159
159
  MockSingleSpecificResource.new self
160
160
  end
@@ -169,17 +169,17 @@ describe Api::Presenter::Hypermedia do
169
169
 
170
170
  new_single_resource_standard = single_resource_standard
171
171
  new_single_resource_standard['floating_point_value'] = 10.3
172
-
172
+
173
173
  mock_data = MockSpecificData.new(number: 1, string: 'This is a string', date: Date.today)
174
174
 
175
175
  mock_data.to_resource.present.must_equal new_single_resource_standard
176
176
  end
177
177
  end
178
-
178
+
179
179
  describe "when using host" do
180
180
  it "must display a complete url" do
181
181
  Api::Presenter::Resource.host = "http://localhost:9292"
182
-
182
+
183
183
  mock_data.to_resource.present.wont_equal single_resource_standard
184
184
 
185
185
  new_single_resource_standard = {
@@ -202,43 +202,76 @@ describe Api::Presenter::Hypermedia do
202
202
  "string" => 'This is a string',
203
203
  "date" => Date.today
204
204
  }
205
-
205
+
206
206
  mock_data.to_resource.present.must_equal new_single_resource_standard
207
-
207
+
208
208
  Api::Presenter::Resource.host = ''
209
- end
209
+ end
210
+ end
211
+
212
+ describe "when using prefix" do
213
+ it "must display a complete url" do
214
+ Api::Presenter::Resource.prefix = "/my_prefix"
215
+
216
+ mock_data.to_resource.present.wont_equal single_resource_standard
217
+
218
+ new_single_resource_standard = {
219
+ "links" =>
220
+ {
221
+ "self" =>
222
+ {
223
+ "href" => "/my_prefix/path/to/single_resource/1"
224
+ },
225
+ "custom" =>
226
+ {
227
+ "href" => "/my_prefix/path/to/custom_link"
228
+ },
229
+ "sibling" =>
230
+ {
231
+ "href" => "/my_prefix/path/to/single_resource/20"
232
+ }
233
+ },
234
+ "number" => 1,
235
+ "string" => 'This is a string',
236
+ "date" => Date.today
237
+ }
238
+
239
+ mock_data.to_resource.present.must_equal new_single_resource_standard
240
+
241
+ Api::Presenter::Resource.prefix = ''
242
+ end
210
243
  end
211
-
244
+
212
245
  describe "when presenting a single resource" do
213
246
  let(:presented_single_resource) { mock_data.to_resource.present }
214
-
247
+
215
248
  it "must respect the standard" do
216
249
  presented_single_resource.must_equal single_resource_standard
217
- end
250
+ end
218
251
  end
219
252
 
220
253
  describe "presenting a collection resource" do
221
-
254
+
222
255
  let(:mock_data_collection) do
223
256
  collection = []
224
257
  4.times { |number| collection << MockData.new(number: number + 1, string: 'This is a string', date: Date.today) }
225
258
  Collection.new(collection)
226
259
  end
227
-
260
+
228
261
  let(:collection_resource) { MockCollectionResource.new mock_data_collection }
229
-
262
+
230
263
  let(:presented_collection_resource) { collection_resource.present }
231
-
264
+
232
265
  it "must respect the standard" do
233
266
  presented_collection_resource.must_equal collection_resource_standard
234
267
  end
235
-
268
+
236
269
  describe "presenting a search resource" do
237
-
270
+
238
271
  let(:search_resource) { MockSearchResource.new mock_data_collection, "page" => 1, "param1" => 1, "param2" => "string" }
239
-
272
+
240
273
  let(:presented_search_resource) { search_resource.present }
241
-
274
+
242
275
  it "must respect the standard" do
243
276
  presented_search_resource.must_equal search_resource_standard
244
277
  end
metadata CHANGED
@@ -1,20 +1,18 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: api-presenter
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
5
- prerelease:
4
+ version: 0.1.1
6
5
  platform: ruby
7
6
  authors:
8
7
  - Álvaro Lara
9
8
  autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
- date: 2013-06-13 00:00:00.000000000 Z
11
+ date: 2013-08-13 00:00:00.000000000 Z
13
12
  dependencies:
14
13
  - !ruby/object:Gem::Dependency
15
14
  name: bundler
16
15
  requirement: !ruby/object:Gem::Requirement
17
- none: false
18
16
  requirements:
19
17
  - - ~>
20
18
  - !ruby/object:Gem::Version
@@ -22,7 +20,6 @@ dependencies:
22
20
  type: :development
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
@@ -30,7 +27,6 @@ dependencies:
30
27
  - !ruby/object:Gem::Dependency
31
28
  name: rake
32
29
  requirement: !ruby/object:Gem::Requirement
33
- none: false
34
30
  requirements:
35
31
  - - ! '>='
36
32
  - !ruby/object:Gem::Version
@@ -38,7 +34,6 @@ dependencies:
38
34
  type: :development
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
@@ -69,27 +64,26 @@ files:
69
64
  homepage: https://github.com/guiman/api-presenter
70
65
  licenses:
71
66
  - MIT
67
+ metadata: {}
72
68
  post_install_message:
73
69
  rdoc_options: []
74
70
  require_paths:
75
71
  - lib
76
72
  required_ruby_version: !ruby/object:Gem::Requirement
77
- none: false
78
73
  requirements:
79
74
  - - ! '>='
80
75
  - !ruby/object:Gem::Version
81
76
  version: '0'
82
77
  required_rubygems_version: !ruby/object:Gem::Requirement
83
- none: false
84
78
  requirements:
85
79
  - - ! '>='
86
80
  - !ruby/object:Gem::Version
87
81
  version: '0'
88
82
  requirements: []
89
83
  rubyforge_project:
90
- rubygems_version: 1.8.23
84
+ rubygems_version: 2.0.6
91
85
  signing_key:
92
- specification_version: 3
86
+ specification_version: 4
93
87
  summary: A JSON resource presenter for a specific api media type
94
88
  test_files:
95
89
  - spec/hypertext_presenter_mocks.rb