grape-entity 0.3.0 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: grape-entity
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Michael Bleigh
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-03-29 00:00:00.000000000 Z
11
+ date: 2014-01-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -118,13 +118,14 @@ extra_rdoc_files: []
118
118
  files:
119
119
  - .gitignore
120
120
  - .rspec
121
+ - .rubocop.yml
121
122
  - .travis.yml
122
123
  - .yardopts
123
- - CHANGELOG.markdown
124
+ - CHANGELOG.md
124
125
  - Gemfile
125
126
  - Guardfile
126
127
  - LICENSE
127
- - README.markdown
128
+ - README.md
128
129
  - Rakefile
129
130
  - grape-entity.gemspec
130
131
  - lib/grape-entity.rb
@@ -153,7 +154,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
153
154
  version: '0'
154
155
  requirements: []
155
156
  rubyforge_project: grape-entity
156
- rubygems_version: 2.0.0
157
+ rubygems_version: 2.0.3
157
158
  signing_key:
158
159
  specification_version: 4
159
160
  summary: A simple facade for managing the relationship between your model and API.
@@ -1,21 +0,0 @@
1
- Next Release
2
- ============
3
- * Your contribution here.
4
- * Fixed bug with to_json.
5
-
6
- 0.3.0 (2013-03-29)
7
- ==================
8
-
9
- * Adds `with_options` for block-level exposure setting
10
- * DSL instance.entity now optionally accepts options to instantiate on the entity
11
- * You can pass symbols to `:if` and `:unless` to simply check for truthiness/falsiness of the specified options key
12
-
13
- 0.2.0 (2013-01-11)
14
- ==================
15
-
16
- * Moved the namespace back to Grape::Entity to preserve compatibility with Grape (via @dblock).
17
-
18
- 0.1.0 (2013-01-11)
19
- ==================
20
-
21
- * Initial Release
@@ -1,197 +0,0 @@
1
- # Grape::Entity
2
-
3
- [![Build Status](https://travis-ci.org/agileanimal/grape-entity.png?branch=master)](https://travis-ci.org/agileanimal/grape-entity)
4
-
5
- ## Introduction
6
-
7
- This gem adds Entity support to API frameworks, such as [Grape](https://github.com/intridea/grape). Grape's Entity is an API focussed facade that sits on top of an object model.
8
-
9
- ## What's New
10
-
11
- We are currently working on a set of "shoulda-style matchers" (sorry, RSpec only right now -- although they've been done in a way that can support test-unit in the future).
12
-
13
- [Grape Entity Matchers](https://github.com/agileanimal/grape-entity-matchers).
14
-
15
- This is still a work in progress but worth checking out.
16
-
17
- ## Reusable Responses with Entities
18
-
19
- Entities are a reusable means for converting Ruby objects to API responses.
20
- Entities can be used to conditionally include fields, nest other entities, and build
21
- ever larger responses, using inheritance.
22
-
23
- ### Defining Entities
24
-
25
- Entities inherit from Grape::Entity, and define a simple DSL. Exposures can use
26
- runtime options to determine which fields should be visible, these options are
27
- available to `:if`, `:unless`, and `:proc`. The option keys `:version` and `:collection`
28
- will always be defined. The `:version` key is defined as `api.version`. The
29
- `:collection` key is boolean, and defined as `true` if the object presented is an
30
- array.
31
-
32
- * `expose SYMBOLS`
33
- * define a list of fields which will always be exposed
34
- * `expose SYMBOLS, HASH`
35
- * HASH keys include `:if`, `:unless`, `:proc`, `:as`, `:using`, `:format_with`, `:documentation`
36
- * `:if` and `:unless` accept hashes (passed during runtime), procs (arguments are object and options), or symbols (checks for presence of the specified key on the options hash)
37
- * `expose SYMBOL, { :format_with => :formatter }`
38
- * expose a value, formatting it first
39
- * `:format_with` can only be applied to one exposure at a time
40
- * `expose SYMBOL, { :as => "alias" }`
41
- * Expose a value, changing its hash key from SYMBOL to alias
42
- * `:as` can only be applied to one exposure at a time
43
- * `expose SYMBOL BLOCK`
44
- * block arguments are object and options
45
- * expose the value returned by the block
46
- * block can only be applied to one exposure at a time
47
-
48
- ```ruby
49
- module API
50
- module Entities
51
- class Status < Grape::Entity
52
- expose :user_name
53
- expose :text, :documentation => { :type => "string", :desc => "Status update text." }
54
- expose :ip, :if => { :type => :full }
55
- expose :user_type, user_id, :if => lambda{ |status, options| status.user.public? }
56
- expose :digest { |status, options| Digest::MD5.hexdigest(satus.txt) }
57
- expose :replies, :using => API::Status, :as => :replies
58
- end
59
- end
60
- end
61
-
62
- module API
63
- module Entities
64
- class StatusDetailed < API::Entities::Status
65
- expose :internal_id
66
- end
67
- end
68
- end
69
- ```
70
-
71
- #### Using the Exposure DSL
72
-
73
- Grape ships with a DSL to easily define entities within the context
74
- of an existing class:
75
-
76
- ```ruby
77
- class Status
78
- include Grape::Entity::DSL
79
-
80
- entity :text, :user_id do
81
- expose :detailed, if: :conditional
82
- end
83
- end
84
- ```
85
-
86
- The above will automatically create a `Status::Entity` class and define properties on it according
87
- to the same rules as above. If you only want to define simple exposures you don't have to supply
88
- a block and can instead simply supply a list of comma-separated symbols.
89
-
90
- ### Using Entities
91
-
92
- Once an entity is defined, it can be used within endpoints, by calling `present`. The `present`
93
- method accepts two arguments, the object to be presented and the options associated with it. The
94
- options hash must always include `:with`, which defines the entity to expose.
95
-
96
- If the entity includes documentation it can be included in an endpoint's description.
97
-
98
- ```ruby
99
- module API
100
- class Statuses < Grape::API
101
- version 'v1'
102
-
103
- desc 'Statuses index', {
104
- :object_fields => API::Entities::Status.documentation
105
- }
106
- get '/statuses' do
107
- statuses = Status.all
108
- type = current_user.admin? ? :full : :default
109
- present statuses, with: API::Entities::Status, :type => type
110
- end
111
- end
112
- end
113
- ```
114
-
115
- ### Entity Organization
116
-
117
- In addition to separately organizing entities, it may be useful to put them as namespaced
118
- classes underneath the model they represent.
119
-
120
- ```ruby
121
- class Status
122
- def entity
123
- Status.new(self)
124
- end
125
-
126
- class Entity < Grape::Entity
127
- expose :text, :user_id
128
- end
129
- end
130
- ```
131
-
132
- If you organize your entities this way, Grape will automatically detect the `Entity` class and
133
- use it to present your models. In this example, if you added `present User.new` to your endpoint,
134
- Grape would automatically detect that there is a `Status::Entity` class and use that as the
135
- representative entity. This can still be overridden by using the `:with` option or an explicit
136
- `represents` call.
137
-
138
- ### Caveats
139
-
140
- Entities with duplicate exposure names and conditions will silently overwrite one another.
141
- In the following example, when `object.check` equals "foo", only `field_a` will be exposed.
142
- However, when `object.check` equals "bar" both `field_b` and `foo` will be exposed.
143
-
144
- ```ruby
145
- module API
146
- module Entities
147
- class Status < Grape::Entity
148
- expose :field_a, :foo, :if => lambda { |object, options| object.check == "foo" }
149
- expose :field_b, :foo, :if => lambda { |object, options| object.check == "bar" }
150
- end
151
- end
152
- end
153
- ```
154
-
155
- This can be problematic, when you have mixed collections. Using `respond_to?` is safer.
156
-
157
- ```ruby
158
- module API
159
- module Entities
160
- class Status < Grape::Entity
161
- expose :field_a, :if => lambda { |object, options| object.check == "foo" }
162
- expose :field_b, :if => lambda { |object, options| object.check == "bar" }
163
- expose :foo, :if => lambda { |object, options| object.respond_to?(:foo) }
164
- end
165
- end
166
- end
167
- ```
168
-
169
- ## Installation
170
-
171
- Add this line to your application's Gemfile:
172
-
173
- gem 'grape-entity'
174
-
175
- And then execute:
176
-
177
- $ bundle
178
-
179
- Or install it yourself as:
180
-
181
- $ gem install grape-entity
182
-
183
- ## Contributing
184
-
185
- 1. Fork it
186
- 2. Create your feature branch (`git checkout -b my-new-feature`)
187
- 3. Commit your changes (`git commit -am 'Add some feature'`)
188
- 4. Push to the branch (`git push origin my-new-feature`)
189
- 5. Create new Pull Request
190
-
191
- ## License
192
-
193
- MIT License. See LICENSE for details.
194
-
195
- ## Copyright
196
-
197
- Copyright (c) 2010-2013 Michael Bleigh, and Intridea, Inc.