halibut-rails 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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA512:
3
+ metadata.gz: 63e4447a232f1f4504003fca37003087ee3478927a83fc43e07d5b308c9495d8007d699c6bc7794946bac44ee6b59623a4b5892bbf567dbe22c43dd747d3489a
4
+ data.tar.gz: f54e3b4af0386c368b52fcdce4a7c6024921f97e21c5a44956ee184b721b0b7b71aea4e04de3d1ae6eb1f07b47571e530d546bf4f917fd14ecde7f071a213ab3
5
+ SHA1:
6
+ metadata.gz: be74758fe5a0aec238d93526f64b12b2d733edad
7
+ data.tar.gz: 11f3141ddebba2e062a63d5faed937bd82211133
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,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in halibut-rails.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Toru KAWAMURA
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,104 @@
1
+ # Halibut::Rails
2
+
3
+ Rails Template Handler for [Halibut](https://github.com/locks/halibut) Builder.
4
+
5
+ Useful to make [HAL](http://stateless.co/hal_specification.html) JSON responses in Rails such as Web APIs.
6
+
7
+
8
+ ## Installation
9
+
10
+ Add this line to your application's Gemfile:
11
+
12
+ gem 'halibut-rails'
13
+
14
+ And then execute:
15
+
16
+ $ bundle
17
+
18
+ ## Usage
19
+
20
+ You can write Halibut Builder syntax in view template.
21
+
22
+ Template file is recognized by extension `.halibut`.
23
+
24
+ *Notice:* You don't need `Halibut::Builder.new`. Instead, use `link 'self'` for indicating self path.
25
+
26
+ ```ruby
27
+ # app/views/orders/index.json.halibut
28
+ property 'currentlyProcessing', 14
29
+ property 'shippedToday', 20
30
+ property 'total', @orders.size
31
+
32
+ namespace 'th', 'http://things-db.com/{rel}'
33
+
34
+ link 'self', orders_path(page: @orders.current_page)
35
+ link 'find', '/orders{?q}', templated: true
36
+ link 'next', orders_path(page: @orders.next_page) unless @orders.last_page?
37
+ link 'prev', orders_path(page: @orders.prev_page) unless @orders.first_page?
38
+
39
+ @orders.each do |order|
40
+ resource 'orders', order_path(order) do
41
+ property 'total', order.total
42
+ property 'currency', order.currency
43
+ property 'status', order.status
44
+ link 'th:customer', customer_path(order.customer)
45
+ end
46
+ end
47
+ ```
48
+
49
+ This will build the following structure:
50
+
51
+ ```json
52
+ {
53
+ "currentlyProcessing": 14,
54
+ "shippedToday": 20,
55
+ "total": 35,
56
+ "_links": {
57
+ "self": { "href": "/orders" },
58
+ "find": { "href": "/orders{?q}", "templated": true },
59
+ "next": { "href": "/orders?page=2" },
60
+ "curie": {
61
+ "href": "http://things-db.com/{rel}",
62
+ "templated": true,
63
+ "name": "th"
64
+ }
65
+ },
66
+ "_embedded": {
67
+ "orders": [
68
+ {
69
+ "total": 30.00,
70
+ "currency": "USD",
71
+ "status": "shipped",
72
+ "_links": {
73
+ "self": { "href": "/orders/123" },
74
+ "th:customer": { "href": "/customers/7809" }
75
+ },
76
+ },
77
+ {
78
+ "total": 20.00,
79
+ "currency": "USD",
80
+ "status": "processing",
81
+ "_links": {
82
+ "self": { "href": "/orders/124" },
83
+ "th:customer": { "href": "/customers/12369" }
84
+ }
85
+ }
86
+ ]
87
+ }
88
+ }
89
+ ```
90
+
91
+ As the example above, you can use instance variables and helpers ordinarily.
92
+
93
+ ## Restriction
94
+
95
+ `relation` notation is not supported yet.
96
+
97
+
98
+ ## Contributing
99
+
100
+ 1. Fork it ( http://github.com/<my-github-username>/halibut-rails/fork )
101
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
102
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
103
+ 4. Push to the branch (`git push origin my-new-feature`)
104
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'halibut/rails/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'halibut-rails'
8
+ spec.version = Halibut::Rails::VERSION
9
+ spec.authors = ['Toru KAWAMURA']
10
+ spec.email = ['tkawa@4bit.net']
11
+ spec.summary = %q{Rails Template Handler for Halibut Builder}
12
+ spec.description = %q{Rails Template Handler for Halibut Builder}
13
+ spec.homepage = 'https://github.com/tkawa/halibut-rails'
14
+ spec.license = 'MIT'
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
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_dependency 'halibut'
22
+ spec.add_dependency 'rails'
23
+ spec.add_development_dependency 'bundler', '~> 1.5'
24
+ spec.add_development_dependency 'rake'
25
+ end
@@ -0,0 +1,5 @@
1
+ require 'halibut'
2
+ require 'halibut/rails/railtie'
3
+ require 'halibut/rails/context'
4
+ require 'halibut/rails/context_delegator'
5
+ require 'halibut/rails/version'
@@ -0,0 +1,10 @@
1
+ module Halibut
2
+ module Rails
3
+ class BuilderHandler
4
+ def self.call(template)
5
+ %{Halibut::Rails::ContextDelegator.around(self){ #{template.source} } }
6
+ end
7
+ end
8
+ end
9
+ end
10
+ ActionView::Template.register_template_handler :halibut, Halibut::Rails::BuilderHandler
@@ -0,0 +1,50 @@
1
+ module Halibut
2
+ module Rails
3
+ class Context < Halibut::Builder::RootContext
4
+ def initialize(view_context, options = {})
5
+ @view_context = view_context
6
+ @resource = Halibut::Core::Resource.new(options[:href])
7
+ end
8
+
9
+ def _resource
10
+ @resource
11
+ end
12
+
13
+ # Adds an embedded resource.
14
+ #
15
+ # @param [String] rel Embedded resource relation to the parent resource
16
+ # @param [String] href URI to the resource itself
17
+ # @param [Proc] blk Instructions to construct the embedded resource
18
+ def resource(rel, href=nil, &embedded_definition)
19
+ embedded = self.class.new(@view_context, href: href)
20
+ @view_context._swap_context(embedded, &embedded_definition) if block_given?
21
+ @resource.embed_resource(rel, embedded._resource)
22
+ end
23
+
24
+ # Adds links or resources to a relation.
25
+ #
26
+ # Relation allows the user to specify links, or resources, per relation,
27
+ # instead of individually.
28
+ # This feature was introduced as an attempt to reduce repeating the
29
+ # relation per link/resource, and thus reducing typos.
30
+ #
31
+ # resource = Halibut::Builder.new do
32
+ # relation :john do
33
+ # link 'http://appleseed.com/john'
34
+ # end
35
+ # end.resource
36
+ # resource.links[:john].first.href
37
+ #
38
+ # @param [String,Symbol] rel
39
+ # @param [Proc] blk Instructions to be executed in the relation
40
+ # context
41
+ def relation(rel, &relation_definition)
42
+ raise NotImplementedError, 'relation is not supported yet'
43
+ end
44
+ end
45
+
46
+ # RelationContext is not supported yet
47
+ # class RelationContext < Halibut::Builder::RelationContext
48
+ # end
49
+ end
50
+ end
@@ -0,0 +1,47 @@
1
+ module Halibut
2
+ module Rails
3
+ module ContextDelegator
4
+ attr_reader :_halibut_context
5
+
6
+ def self.setup(view_context)
7
+ view_context.extend(self)
8
+ view_context._setup_delegation!(Halibut::Rails::Context.new(view_context))
9
+ end
10
+
11
+ def self.teardown(view_context)
12
+ # if @options[:pretty]
13
+ JSON.pretty_generate(view_context._halibut_resource.to_hash)
14
+ # else
15
+ # Halibut::Adapter::JSON.dump(context._halibut_resource)
16
+ # end
17
+ end
18
+
19
+ def self.around(view_context)
20
+ setup(view_context)
21
+ yield
22
+ teardown(view_context)
23
+ end
24
+
25
+ def self.extended(obj)
26
+ obj.extend(SingleForwardable)
27
+ end
28
+
29
+ def _setup_delegation!(halibut_context)
30
+ @_halibut_context = halibut_context
31
+ methods = @_halibut_context.methods - Object.instance_methods
32
+ def_delegators(:@_halibut_context, *methods)
33
+ end
34
+
35
+ def _swap_context(another_context, &block)
36
+ original_context, @_halibut_context = @_halibut_context, another_context
37
+ yield
38
+ ensure
39
+ @_halibut_context = original_context
40
+ end
41
+
42
+ def _halibut_resource
43
+ @_halibut_context.instance_variable_get(:@resource)
44
+ end
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,11 @@
1
+ module Halibut
2
+ module Rails
3
+ class Railtie < ::Rails::Railtie
4
+ initializer 'halibut-rails.initialize' do |app|
5
+ ActiveSupport.on_load(:action_view) do
6
+ require 'halibut/rails/builder_handler'
7
+ end
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,5 @@
1
+ module Halibut
2
+ module Rails
3
+ VERSION = '0.0.1'
4
+ end
5
+ end
metadata ADDED
@@ -0,0 +1,97 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: halibut-rails
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Toru KAWAMURA
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2014-07-21 00:00:00 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: halibut
16
+ prerelease: false
17
+ requirement: &id001 !ruby/object:Gem::Requirement
18
+ requirements:
19
+ - &id002
20
+ - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: "0"
23
+ type: :runtime
24
+ version_requirements: *id001
25
+ - !ruby/object:Gem::Dependency
26
+ name: rails
27
+ prerelease: false
28
+ requirement: &id003 !ruby/object:Gem::Requirement
29
+ requirements:
30
+ - *id002
31
+ type: :runtime
32
+ version_requirements: *id003
33
+ - !ruby/object:Gem::Dependency
34
+ name: bundler
35
+ prerelease: false
36
+ requirement: &id004 !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: "1.5"
41
+ type: :development
42
+ version_requirements: *id004
43
+ - !ruby/object:Gem::Dependency
44
+ name: rake
45
+ prerelease: false
46
+ requirement: &id005 !ruby/object:Gem::Requirement
47
+ requirements:
48
+ - *id002
49
+ type: :development
50
+ version_requirements: *id005
51
+ description: Rails Template Handler for Halibut Builder
52
+ email:
53
+ - tkawa@4bit.net
54
+ executables: []
55
+
56
+ extensions: []
57
+
58
+ extra_rdoc_files: []
59
+
60
+ files:
61
+ - .gitignore
62
+ - Gemfile
63
+ - LICENSE.txt
64
+ - README.md
65
+ - Rakefile
66
+ - halibut-rails.gemspec
67
+ - lib/halibut/rails.rb
68
+ - lib/halibut/rails/builder_handler.rb
69
+ - lib/halibut/rails/context.rb
70
+ - lib/halibut/rails/context_delegator.rb
71
+ - lib/halibut/rails/railtie.rb
72
+ - lib/halibut/rails/version.rb
73
+ homepage: https://github.com/tkawa/halibut-rails
74
+ licenses:
75
+ - MIT
76
+ metadata: {}
77
+
78
+ post_install_message:
79
+ rdoc_options: []
80
+
81
+ require_paths:
82
+ - lib
83
+ required_ruby_version: !ruby/object:Gem::Requirement
84
+ requirements:
85
+ - *id002
86
+ required_rubygems_version: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - *id002
89
+ requirements: []
90
+
91
+ rubyforge_project:
92
+ rubygems_version: 2.0.14
93
+ signing_key:
94
+ specification_version: 4
95
+ summary: Rails Template Handler for Halibut Builder
96
+ test_files: []
97
+