sliver-rails 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: a602a7ec57e691d3da4697d141d4b571fa690ec5
4
+ data.tar.gz: 6f5950675d41e5802d1c4c929a850ab7e9f4ca02
5
+ SHA512:
6
+ metadata.gz: 3ce1572bd6bb0e237022ebefac65a0f1ead860de4b92cf296894482b1d0128e33ceeda2ddea895c0068819dccfdbdb698e1b30101700c73805bd69b4ab7797dc
7
+ data.tar.gz: f43f53d17a40d0861f04626ded74785c4eabf5591a14dc5f77409ebe244761a97b40f8e0403af6044cb2f4bf6eab35fc3511a4b1971efd42a4e1b5775d1010cb
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 Pat Allan
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.
@@ -0,0 +1,31 @@
1
+ # Sliver::Rails
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'sliver-rails'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install sliver-rails
20
+
21
+ ## Usage
22
+
23
+ TODO: Write usage instructions here
24
+
25
+ ## Contributing
26
+
27
+ 1. Fork it ( https://github.com/[my-github-username]/sliver-rails/fork )
28
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
29
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
30
+ 4. Push to the branch (`git push origin my-new-feature`)
31
+ 5. Create a new Pull Request
@@ -0,0 +1,2 @@
1
+ require 'bundler/gem_tasks'
2
+
@@ -0,0 +1,28 @@
1
+ require 'sliver'
2
+ require 'rails'
3
+
4
+ module Sliver::Rails
5
+ module Processors; end
6
+
7
+ def self.template_class
8
+ @template_class || Sliver::Rails::JBuilderTemplate
9
+ end
10
+
11
+ def self.template_class=(klass)
12
+ @template_class = klass
13
+ end
14
+
15
+ def self.view_paths
16
+ @view_paths || Rails.root.join('app/views').to_s
17
+ end
18
+
19
+ def self.view_paths=(paths)
20
+ @view_paths = paths
21
+ end
22
+ end
23
+
24
+ require 'sliver/rails/action'
25
+ require 'sliver/rails/jbuilder_renderer'
26
+ require 'sliver/rails/jbuilder_template'
27
+ require 'sliver/rails/pagination'
28
+ require 'sliver/rails/processors/json_processor'
@@ -0,0 +1,86 @@
1
+ class Sliver::Rails::Action
2
+ include Sliver::Action
3
+
4
+ BODIED_METHODS = %w( post put patch )
5
+
6
+ def self.expose(name, options = {}, &block)
7
+ add_exposed_methods name, options, &block if block_given?
8
+
9
+ locals << name
10
+ end
11
+
12
+ def self.add_exposed_methods(name, options = {}, &block)
13
+ uncached_name = "#{name}_without_caching"
14
+
15
+ define_method uncached_name, &block
16
+ define_method(name) do
17
+ @exposed_methods ||= {}
18
+ @exposed_methods[name] ||= send uncached_name
19
+ end
20
+
21
+ return if options[:public]
22
+
23
+ private uncached_name
24
+ private name
25
+ end
26
+
27
+ def self.locals
28
+ @locals ||= []
29
+ end
30
+
31
+ def self.template
32
+ @template
33
+ end
34
+
35
+ def self.use_template(template)
36
+ @template = template
37
+ end
38
+
39
+ def call
40
+ #
41
+ end
42
+
43
+ def locals
44
+ self.class.locals.inject({}) do |hash, name|
45
+ hash[name] = send name
46
+ hash
47
+ end
48
+ end
49
+
50
+ private
51
+
52
+ def bodied_request?
53
+ BODIED_METHODS.include? environment['REQUEST_METHOD'].downcase
54
+ end
55
+
56
+ def content_type_header
57
+ environment['Content-Type'] ||
58
+ environment['HTTP_CONTENT_TYPE'] ||
59
+ environment['CONTENT_TYPE']
60
+ end
61
+
62
+ def content_types
63
+ content_type_header.split(/;\s?/)
64
+ end
65
+
66
+ def json_request?
67
+ content_types.include? 'application/json'
68
+ end
69
+
70
+ def params
71
+ ActionController::Parameters.new request_params
72
+ end
73
+
74
+ def request_params
75
+ @request_params ||= if bodied_request? && json_request?
76
+ JSON.parse(request.body.read)
77
+ else
78
+ request.params
79
+ end
80
+ end
81
+
82
+ def set_response(status, body = nil)
83
+ response.status = status
84
+ response.body = body unless body.nil?
85
+ end
86
+ end
@@ -0,0 +1,32 @@
1
+ class Sliver::Rails::JBuilderRenderer
2
+ def self.call(template, locals = {})
3
+ new(template, locals).call
4
+ end
5
+
6
+ def initialize(template, locals = {})
7
+ @template, @locals = template, locals
8
+ end
9
+
10
+ def call
11
+ engine.render Sliver::Rails.template_class.new, locals
12
+ end
13
+
14
+ private
15
+
16
+ attr_reader :template, :locals
17
+
18
+ def engine
19
+ Tilt.new template_path, nil, view_path: views_path
20
+ end
21
+
22
+ def template_path
23
+ template_with_ext = template
24
+ template_with_ext = "#{template}.jbuilder" unless template[/\.jbuilder$/]
25
+
26
+ File.join views_path, template_with_ext
27
+ end
28
+
29
+ def views_path
30
+ Sliver::Rails.view_paths
31
+ end
32
+ end
@@ -0,0 +1,5 @@
1
+ class Sliver::Rails::JBuilderTemplate
2
+ def routes
3
+ Rails.application.routes
4
+ end
5
+ end
@@ -0,0 +1,6 @@
1
+ module Sliver::Rails::Pagination
2
+ def self.included(base)
3
+ base.expose(:page) { params['page'].try(:to_i) || 1 }
4
+ base.expose(:per_page) { [(params['per_page'].try(:to_i) || 20), 100].min }
5
+ end
6
+ end
@@ -0,0 +1,21 @@
1
+ class Sliver::Rails::Processors::JSONProcessor < Sliver::Hook
2
+ def call
3
+ response.status ||= 200
4
+ response.body = [body]
5
+ response.headers['Content-Type'] = 'application/json'
6
+ end
7
+
8
+ private
9
+
10
+ def body
11
+ return response.body if response.body.is_a?(String)
12
+ return response.body.to_json if response.body.present?
13
+ return '' if template.nil?
14
+
15
+ Sliver::Rails::JBuilderRenderer.call template, action.locals
16
+ end
17
+
18
+ def template
19
+ action.class.template
20
+ end
21
+ end
@@ -0,0 +1,17 @@
1
+ # coding: utf-8
2
+ Gem::Specification.new do |spec|
3
+ spec.name = 'sliver-rails'
4
+ spec.version = '0.1.0'
5
+ spec.authors = ['Pat Allan']
6
+ spec.email = ['pat@freelancing-gods.com']
7
+ spec.summary = %q{Rails extensions for Sliver}
8
+ spec.homepage = 'https://github.com/pat/sliver-rails'
9
+ spec.license = 'MIT'
10
+
11
+ spec.files = `git ls-files -z`.split("\x0")
12
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
13
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
14
+ spec.require_paths = ['lib']
15
+
16
+ spec.add_runtime_dependency 'sliver', '~> 0.1'
17
+ end
metadata ADDED
@@ -0,0 +1,71 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sliver-rails
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Pat Allan
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-03-13 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: sliver
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '0.1'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '0.1'
27
+ description:
28
+ email:
29
+ - pat@freelancing-gods.com
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - ".gitignore"
35
+ - Gemfile
36
+ - LICENSE.txt
37
+ - README.md
38
+ - Rakefile
39
+ - lib/sliver/rails.rb
40
+ - lib/sliver/rails/action.rb
41
+ - lib/sliver/rails/jbuilder_renderer.rb
42
+ - lib/sliver/rails/jbuilder_template.rb
43
+ - lib/sliver/rails/pagination.rb
44
+ - lib/sliver/rails/processors/json_processor.rb
45
+ - sliver-rails.gemspec
46
+ homepage: https://github.com/pat/sliver-rails
47
+ licenses:
48
+ - MIT
49
+ metadata: {}
50
+ post_install_message:
51
+ rdoc_options: []
52
+ require_paths:
53
+ - lib
54
+ required_ruby_version: !ruby/object:Gem::Requirement
55
+ requirements:
56
+ - - ">="
57
+ - !ruby/object:Gem::Version
58
+ version: '0'
59
+ required_rubygems_version: !ruby/object:Gem::Requirement
60
+ requirements:
61
+ - - ">="
62
+ - !ruby/object:Gem::Version
63
+ version: '0'
64
+ requirements: []
65
+ rubyforge_project:
66
+ rubygems_version: 2.2.2
67
+ signing_key:
68
+ specification_version: 4
69
+ summary: Rails extensions for Sliver
70
+ test_files: []
71
+ has_rdoc: