grape-roar 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 5827a1b2d9d4d8b3add2a301aa5fae68074c5d22
4
+ data.tar.gz: a60fedc73b8d76bdbb608e5331448a910476a7b2
5
+ SHA512:
6
+ metadata.gz: 343baa16ae114e19e69f365f4109a4e1f8a269a03e3cb1623302377d567cea1e020f24cd0d9726c8ee951d535f0872012378c84e37cd9f6e505bd094b081144f
7
+ data.tar.gz: aa5f1ed03620b635defe792007768d8f7afd8b5b11ff2969b99306db70767091937ffc943b73aa03a8105d96f85c55847e4251cda658a10c79bcaaab1c40cf5a
data/.gitignore ADDED
@@ -0,0 +1,3 @@
1
+ Gemfile.lock
2
+ doc
3
+ pkg
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --color
2
+ --format=documentation
3
+
data/.rubocop.yml ADDED
@@ -0,0 +1,29 @@
1
+ AllCops:
2
+ Exclude:
3
+ - vendor/**/*
4
+ - bin/**/*
5
+
6
+ LineLength:
7
+ Enabled: false
8
+
9
+ MethodLength:
10
+ Enabled: false
11
+
12
+ ClassLength:
13
+ Enabled: false
14
+
15
+ Documentation:
16
+ # don't require classes to be documented
17
+ Enabled: false
18
+
19
+ CollectionMethods:
20
+ # don't prefer map to collect, recuce to inject
21
+ Enabled: false
22
+
23
+ Encoding:
24
+ # no need to always specify encoding
25
+ Enabled: false
26
+
27
+ FileName:
28
+ # allow grape-roar.rb
29
+ Enabled: false
data/.travis.yml ADDED
@@ -0,0 +1,11 @@
1
+ language: ruby
2
+
3
+ cache: bundler
4
+
5
+ rvm:
6
+ - ruby-head
7
+ - 2.1.2
8
+ - 2.0.0
9
+ - 1.9.3
10
+ - jruby-19mode
11
+ - rbx-2
data/CHANGELOG.md ADDED
@@ -0,0 +1,4 @@
1
+ 0.1.0 (18/07/2014)
2
+ ------------------
3
+
4
+ * Initial public release, with support for Grape `present` - [@dblock](https://github.com/dblock).
data/CONTRIBUTING.md ADDED
@@ -0,0 +1,118 @@
1
+ Contributing to Grape-Roar
2
+ ==========================
3
+
4
+ Grape-Roar is work of [many of contributors](https://github.com/dblock/grape-roar/graphs/contributors). You're encouraged to submit [pull requests](https://github.com/dblock/grape-roar/pulls), [propose features and discuss issues](https://github.com/dblock/grape-roar/issues). When in doubt, ask a question in the [Grape Google Group](http://groups.google.com/group/ruby-grape).
5
+
6
+ #### Fork the Project
7
+
8
+ Fork the [project on Github](https://github.com/dblock/grape-roar) and check out your copy.
9
+
10
+ ```
11
+ git clone https://github.com/contributor/grape-roar.git
12
+ cd grape-roar
13
+ git remote add upstream https://github.com/dblock/grape-roar.git
14
+ ```
15
+
16
+ #### Create a Topic Branch
17
+
18
+ Make sure your fork is up-to-date and create a topic branch for your feature or bug fix.
19
+
20
+ ```
21
+ git checkout master
22
+ git pull upstream master
23
+ git checkout -b my-feature-branch
24
+ ```
25
+
26
+ #### Bundle Install and Test
27
+
28
+ Ensure that you can build the project and run tests.
29
+
30
+ ```
31
+ bundle install
32
+ bundle exec rake
33
+ ```
34
+
35
+ #### Write Tests
36
+
37
+ Try to write a test that reproduces the problem you're trying to fix or describes a feature that you want to build. Add to [spec/grape-roar](spec/grape-roar).
38
+
39
+ We definitely appreciate pull requests that highlight or reproduce a problem, even without a fix.
40
+
41
+ #### Write Code
42
+
43
+ Implement your feature or bug fix.
44
+
45
+ Ruby style is enforced with [Rubocop](https://github.com/bbatsov/rubocop), run `bundle exec rubocop` and fix any style issues highlighted.
46
+
47
+ Make sure that `bundle exec rake` completes without errors.
48
+
49
+ #### Write Documentation
50
+
51
+ Document any external behavior in the [README](README.md).
52
+
53
+ #### Update Changelog
54
+
55
+ Add a line to [CHANGELOG](CHANGELOG.md) under *Next Release*. Make it look like every other line, including your name and link to your Github account.
56
+
57
+ #### Commit Changes
58
+
59
+ Make sure git knows your name and email address:
60
+
61
+ ```
62
+ git config --global user.name "Your Name"
63
+ git config --global user.email "contributor@example.com"
64
+ ```
65
+
66
+ Writing good commit logs is important. A commit log should describe what changed and why.
67
+
68
+ ```
69
+ git add ...
70
+ git commit
71
+ ```
72
+
73
+ #### Push
74
+
75
+ ```
76
+ git push origin my-feature-branch
77
+ ```
78
+
79
+ #### Make a Pull Request
80
+
81
+ Go to https://github.com/contributor/grape-roar and select your feature branch. Click the 'Pull Request' button and fill out the form. Pull requests are usually reviewed within a few days.
82
+
83
+ #### Rebase
84
+
85
+ If you've been working on a change for a while, rebase with upstream/master.
86
+
87
+ ```
88
+ git fetch upstream
89
+ git rebase upstream/master
90
+ git push origin my-feature-branch -f
91
+ ```
92
+
93
+ #### Update CHANGELOG Again
94
+
95
+ Update the [CHANGELOG](CHANGELOG.md) with the pull request number. A typical entry looks as follows.
96
+
97
+ ```
98
+ * [#123](https://github.com/dblock/grape-roar/pull/123): Reticulated splines - [@contributor](https://github.com/contributor).
99
+ ```
100
+
101
+ Amend your previous commit and force push the changes.
102
+
103
+ ```
104
+ git commit --amend
105
+ git push origin my-feature-branch -f
106
+ ```
107
+
108
+ #### Check on Your Pull Request
109
+
110
+ Go back to your pull request after a few minutes and see whether it passed muster with Travis-CI. Everything should look green, otherwise fix issues and amend your commit as described above.
111
+
112
+ #### Be Patient
113
+
114
+ It's likely that your change will not be merged and that the nitpicky maintainers will ask you to do more, or fix seemingly benign problems. Hang on there!
115
+
116
+ #### Thank You
117
+
118
+ Please do know that we really appreciate and value your time and work. We love you, really.
data/Gemfile ADDED
@@ -0,0 +1,16 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
4
+
5
+ group :development do
6
+ gem "rake"
7
+ end
8
+
9
+ group :test do
10
+ gem "rspec", "~> 3.0"
11
+ gem "rack-test"
12
+ end
13
+
14
+ group :development, :test do
15
+ gem 'rubocop', '0.24.1'
16
+ end
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2012 Daniel Doubrovkine, Art.sy Inc.
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,79 @@
1
+ Grape::Roar
2
+ ------------
3
+
4
+ Use [Roar](https://github.com/apotonick/roar) with [Grape](https://github.com/intridea/grape).
5
+
6
+ [![Build Status](https://secure.travis-ci.org/dblock/grape-roar.png)](http://travis-ci.org/dblock/grape-roar)
7
+
8
+ Installation
9
+ ------------
10
+
11
+ Add the `grape`, `roar` and `grape-roar` gems to Gemfile.
12
+
13
+ ```ruby
14
+ gem 'grape'
15
+ gem 'roar'
16
+ gem 'grape-roar'
17
+ ```
18
+
19
+ Usage
20
+ -----
21
+
22
+ ### Tell your API to use Grape::Formatter::Roar
23
+
24
+ ```ruby
25
+ class API < Grape::API
26
+ format :json
27
+ formatter :json, Grape::Formatter::Roar
28
+ end
29
+ ```
30
+
31
+ ### Use Grape's Present
32
+
33
+ You can use Grape's `present` keyword after including Grape::Roar::Presenter into a representer module.
34
+
35
+ ```ruby
36
+ module ProductRepresenter
37
+ include Roar::Representer::JSON
38
+ include Roar::Representer::Feature::Hypermedia
39
+ include Grape::Roar::Representer
40
+
41
+ property :title
42
+ property :id
43
+ end
44
+ ```
45
+
46
+ ```ruby
47
+ get 'product/:id' do
48
+ present Product.find(params[:id]), with: ProductRepresenter
49
+ end
50
+ ```
51
+
52
+ ### Accessing the Request Inside a Presenter
53
+
54
+ The formatter invokes `to_json` on presented objects and provides access to the requesting environment via the `env` option. The following example renders a full request URL in a presenter.
55
+
56
+ ```ruby
57
+ module ProductRepresenter
58
+ include Roar::Representer::JSON
59
+ include Roar::Representer::Feature::Hypermedia
60
+ include Grape::Roar::Representer
61
+
62
+ link :self do |opts|
63
+ request = Grape::Request.new(opts[:env])
64
+ "#{request.url}"
65
+ end
66
+ end
67
+ ```
68
+
69
+ Contributing
70
+ ------------
71
+
72
+ See [CONTRIBUTING](CONTRIBUTING.md).
73
+
74
+ Copyright and License
75
+ ---------------------
76
+
77
+ MIT License, see [LICENSE](http://github.com/dblock/grape-roar/raw/master/LICENSE) for details.
78
+
79
+ (c) 2012-2014 [Daniel Doubrovkine](http://github.com/dblock), [Art.sy](http://artsy.github.com)
data/Rakefile ADDED
@@ -0,0 +1,16 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+
4
+ Bundler.setup :default, :test, :development
5
+
6
+ Bundler::GemHelper.install_tasks
7
+
8
+ require 'rspec/core/rake_task'
9
+ RSpec::Core::RakeTask.new(:spec) do |spec|
10
+ spec.pattern = 'spec/**/*_spec.rb'
11
+ end
12
+
13
+ require 'rubocop/rake_task'
14
+ RuboCop::RakeTask.new(:rubocop)
15
+
16
+ task default: [:rubocop, :spec]
@@ -0,0 +1,20 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/grape/roar/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ['Daniel Doubrovkine']
6
+ gem.email = ['dblock@dblock.org']
7
+ gem.description = 'Use Roar with Grape'
8
+ gem.summary = 'Enable Resource-Oriented Architectures in Grape API DSL'
9
+ gem.homepage = 'http://github.com/dblock/grape-roar'
10
+
11
+ gem.executables = `git ls-files -- bin/*`.split("\n").map { |f| File.basename(f) }
12
+ gem.files = `git ls-files`.split("\n")
13
+ gem.test_files = `git ls-files -- {spec}/*`.split("\n")
14
+ gem.name = 'grape-roar'
15
+ gem.require_paths = ['lib']
16
+ gem.version = Grape::Roar::VERSION
17
+
18
+ gem.add_dependency 'grape'
19
+ gem.add_dependency 'roar'
20
+ end
data/lib/grape-roar.rb ADDED
@@ -0,0 +1,3 @@
1
+ require 'roar'
2
+ require 'grape'
3
+ require 'grape/roar'
data/lib/grape/roar.rb ADDED
@@ -0,0 +1,3 @@
1
+ require 'grape/roar/version'
2
+ require 'grape/roar/formatter'
3
+ require 'grape/roar/representer'
@@ -0,0 +1,12 @@
1
+ module Grape
2
+ module Formatter
3
+ module Roar
4
+ class << self
5
+ def call(object, env)
6
+ return object.to_json(env: env) if object.respond_to?(:to_json)
7
+ MultiJson.dump(object)
8
+ end
9
+ end
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,16 @@
1
+ module Grape
2
+ module Roar
3
+ module Representer
4
+ def self.included(base)
5
+ base.extend(ClassMethods)
6
+ end
7
+
8
+ module ClassMethods
9
+ def represent(object, _options = {})
10
+ object.extend self
11
+ object
12
+ end
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,5 @@
1
+ module Grape
2
+ module Roar
3
+ VERSION = '0.1.0'
4
+ end
5
+ end
@@ -0,0 +1,32 @@
1
+ require 'spec_helper'
2
+
3
+ describe Grape::Roar do
4
+ subject do
5
+ Class.new(Grape::API)
6
+ end
7
+
8
+ before do
9
+ subject.format :json
10
+ subject.formatter :json, Grape::Formatter::Roar
11
+ end
12
+
13
+ def app
14
+ subject
15
+ end
16
+
17
+ context 'nested representer' do
18
+ before do
19
+ subject.get('/order/:id') do
20
+ order = Order.new(id: params[:id], client_id: 42)
21
+ order.articles << Article.new(title: 'One', id: 1)
22
+ order.articles << Article.new(title: 'Two', id: 2)
23
+ order
24
+ end
25
+ end
26
+
27
+ it 'returns a hypermedia representation' do
28
+ get '/order/666'
29
+ expect(last_response.body).to eq '{"id":"666","client_id":42,"articles":[{"title":"One","id":1,"links":[{"rel":"self","href":"/article/1"}]},{"title":"Two","id":2,"links":[{"rel":"self","href":"/article/2"}]}],"links":[{"rel":"self","href":"/order/666"},{"rel":"items","href":"/order/666/items"}]}'
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,29 @@
1
+ require 'spec_helper'
2
+
3
+ describe Grape::Roar do
4
+ subject do
5
+ Class.new(Grape::API)
6
+ end
7
+
8
+ before do
9
+ subject.format :json
10
+ subject.formatter :json, Grape::Formatter::Roar
11
+ end
12
+
13
+ def app
14
+ subject
15
+ end
16
+
17
+ context 'using present' do
18
+ before do
19
+ subject.get('/product/:id') do
20
+ present Product.new(title: 'Lonestar', id: params[:id]), with: ProductRepresenter
21
+ end
22
+ end
23
+
24
+ it 'returns a hypermedia representation' do
25
+ get '/product/666'
26
+ expect(last_response.body).to eq '{"title":"Lonestar","id":"666","links":[{"rel":"self","href":"http://example.org/product/666"}]}'
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,29 @@
1
+ require 'spec_helper'
2
+
3
+ describe Grape::Roar do
4
+ subject do
5
+ Class.new(Grape::API)
6
+ end
7
+
8
+ before do
9
+ subject.format :json
10
+ subject.formatter :json, Grape::Formatter::Roar
11
+ end
12
+
13
+ def app
14
+ subject
15
+ end
16
+
17
+ context 'representer' do
18
+ before do
19
+ subject.get('/article/:id') do
20
+ Article.new(title: 'Lonestar', id: params[:id])
21
+ end
22
+ end
23
+
24
+ it 'returns a hypermedia representation' do
25
+ get '/article/666'
26
+ expect(last_response.body).to eq '{"title":"Lonestar","id":"666","links":[{"rel":"self","href":"/article/666"}]}'
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,21 @@
1
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
2
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
3
+
4
+ require 'bundler'
5
+ Bundler.setup :default, :test
6
+
7
+ require 'grape'
8
+
9
+ require 'roar'
10
+ require 'roar/representer/json'
11
+ require 'roar/representer/feature/hypermedia'
12
+
13
+ require 'grape/roar'
14
+ require 'rack/test'
15
+ require 'rspec'
16
+
17
+ RSpec.configure do |config|
18
+ config.include Rack::Test::Methods
19
+ end
20
+
21
+ Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each { |f| require f }
@@ -0,0 +1,14 @@
1
+ require 'support/article_representer'
2
+
3
+ class Article
4
+ include Roar::Representer::JSON
5
+ include ArticleRepresenter
6
+
7
+ attr_accessor :title, :id
8
+
9
+ def initialize(attrs = {})
10
+ attrs.each_pair do |k, v|
11
+ send("#{k}=", v)
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,11 @@
1
+ module ArticleRepresenter
2
+ include Roar::Representer::JSON
3
+ include Roar::Representer::Feature::Hypermedia
4
+
5
+ property :title
6
+ property :id
7
+
8
+ link :self do
9
+ "/article/#{id}"
10
+ end
11
+ end
@@ -0,0 +1,14 @@
1
+ require 'support/order_representer'
2
+
3
+ class Order
4
+ include Roar::Representer::JSON
5
+ include OrderRepresenter
6
+
7
+ attr_accessor :id, :client_id, :articles
8
+
9
+ def initialize(attrs = {})
10
+ { articles: [] }.merge(attrs).each_pair do |k, v|
11
+ send("#{k}=", v)
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,19 @@
1
+ require 'support/article'
2
+
3
+ module OrderRepresenter
4
+ include Roar::Representer::JSON
5
+ include Roar::Representer::Feature::Hypermedia
6
+
7
+ property :id
8
+ property :client_id
9
+
10
+ collection :articles, class: Article
11
+
12
+ link :self do
13
+ "/order/#{id}"
14
+ end
15
+
16
+ link :items do
17
+ "/order/#{id}/items"
18
+ end
19
+ end
@@ -0,0 +1,9 @@
1
+ class Product
2
+ attr_accessor :title, :id
3
+
4
+ def initialize(attrs = {})
5
+ attrs.each_pair do |k, v|
6
+ send("#{k}=", v)
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,15 @@
1
+ require 'roar/representer/json/hal'
2
+
3
+ module ProductRepresenter
4
+ include Roar::Representer::JSON
5
+ include Roar::Representer::Feature::Hypermedia
6
+ include Grape::Roar::Representer
7
+
8
+ property :title
9
+ property :id
10
+
11
+ link :self do |opts|
12
+ request = Grape::Request.new(opts[:env])
13
+ "#{request.url}"
14
+ end
15
+ end
metadata ADDED
@@ -0,0 +1,97 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: grape-roar
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Daniel Doubrovkine
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-07-18 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: grape
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '>='
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '>='
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: roar
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ description: Use Roar with Grape
42
+ email:
43
+ - dblock@dblock.org
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - .gitignore
49
+ - .rspec
50
+ - .rubocop.yml
51
+ - .travis.yml
52
+ - CHANGELOG.md
53
+ - CONTRIBUTING.md
54
+ - Gemfile
55
+ - LICENSE
56
+ - README.md
57
+ - Rakefile
58
+ - grape-roar.gemspec
59
+ - lib/grape-roar.rb
60
+ - lib/grape/roar.rb
61
+ - lib/grape/roar/formatter.rb
62
+ - lib/grape/roar/representer.rb
63
+ - lib/grape/roar/version.rb
64
+ - spec/nested_representer_spec.rb
65
+ - spec/present_with_spec.rb
66
+ - spec/representer_spec.rb
67
+ - spec/spec_helper.rb
68
+ - spec/support/article.rb
69
+ - spec/support/article_representer.rb
70
+ - spec/support/order.rb
71
+ - spec/support/order_representer.rb
72
+ - spec/support/product.rb
73
+ - spec/support/product_representer.rb
74
+ homepage: http://github.com/dblock/grape-roar
75
+ licenses: []
76
+ metadata: {}
77
+ post_install_message:
78
+ rdoc_options: []
79
+ require_paths:
80
+ - lib
81
+ required_ruby_version: !ruby/object:Gem::Requirement
82
+ requirements:
83
+ - - '>='
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ required_rubygems_version: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - '>='
89
+ - !ruby/object:Gem::Version
90
+ version: '0'
91
+ requirements: []
92
+ rubyforge_project:
93
+ rubygems_version: 2.0.14
94
+ signing_key:
95
+ specification_version: 4
96
+ summary: Enable Resource-Oriented Architectures in Grape API DSL
97
+ test_files: []