grape-named_routes 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: dd79227aa5be172ce4591596d7cdd43674db8fca
4
+ data.tar.gz: 9be5189b0b78d9aa4b09766a4b53d28692d29654
5
+ SHA512:
6
+ metadata.gz: b6701ee8075eebf50cd8ea3ec6f116124d1977ed47f93c69521c91288d4718ce9faf9c4f25991c804e266df80d6f0b9457901a92ccf6d96f63ba10a1b727c276
7
+ data.tar.gz: e2a19a8b6873f872cbf7d13a090fa780fd2359b2dc90c74eb1c0f50f003d810f9364f06e84d1a37ef2c6c729b8a4145e640a95c35c690c1ff209f26e6e2d6182
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.2
4
+ before_install: gem install bundler -v 1.10.4
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in grape-named_routes.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Anton Chuchkalov
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,77 @@
1
+ # Grape::NamedRoutes
2
+
3
+ It is an extenstion for API creating framework [Grape](https://github.com/intridea/grape).
4
+ With this gem you can create named routes to call them later without hardcoding pathes.
5
+
6
+ ## Installation
7
+
8
+ Add these lines to your application's Gemfile:
9
+
10
+ ```ruby
11
+ gem 'grape'
12
+ gem 'grape-named_routes'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install grape
22
+ $ gem install grape-named_routes
23
+
24
+ ## Usage
25
+
26
+ These endpoints simply return their URLs.
27
+
28
+ ```ruby
29
+ module Twitter
30
+ class API < Grape::API
31
+ resource :statuses do
32
+ desc 'Endpoint without params'
33
+ get :home_timeline, as: :home_timeline do
34
+ Twitter::API.home_timeline_path
35
+ end
36
+
37
+ desc 'Endpoint with route param'
38
+ params do
39
+ requires :id, type: Integer
40
+ end
41
+ route_param :id do
42
+ get as: :status do
43
+ Twitter::API.status_path(id: params[:id])
44
+ end
45
+ end
46
+
47
+ desc 'Endpoint with inline param'
48
+ params do
49
+ requires :param, type: String
50
+ end
51
+ get '/custom_route/:param', as: :inline do
52
+ Twitter::API.inline_path(param: params[:param])
53
+ end
54
+ end
55
+ end
56
+ end
57
+ ```
58
+
59
+ If you somewhy need to get `endpoint` object instead of compiled path, you can use following:
60
+
61
+ ```ruby
62
+ Twitter::API.find_endpoint(:home_timeline) # returns Grape::Endpoint instance
63
+
64
+ # also supports "dangerous" version (raises an error if route is not declared)
65
+ Twitter::API.find_endpoint!(:home_timeline)
66
+ ```
67
+
68
+
69
+ ## Contributing
70
+
71
+ Bug reports and pull requests are welcome on GitHub at https://github.com/hedgesky/grape-named_routes.
72
+
73
+
74
+ ## License
75
+
76
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
77
+
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,25 @@
1
+ lib = File.expand_path('../lib', __FILE__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require 'grape/named_routes/version'
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = 'grape-named_routes'
7
+ spec.version = Grape::NamedRoutes::VERSION
8
+ spec.authors = ['Anton Chuchkalov']
9
+ spec.email = ['a2new@yandex.ru']
10
+
11
+ spec.summary = %q{Add support for named routes in grape.}
12
+ spec.description = %q{This implementation is really simple and not very dynamic, but may be handy.}
13
+ spec.homepage = 'https://github.com/hedgesky/grape-named_routes'
14
+ spec.license = 'MIT'
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
17
+ spec.executables = `git ls-files -- bin/*`.split("\n").map { |f| File.basename(f) }
18
+ spec.require_paths = ['lib']
19
+
20
+ spec.add_runtime_dependency 'grape', '>= 0.11'
21
+
22
+ spec.add_development_dependency 'bundler', '~> 1.10'
23
+ spec.add_development_dependency 'rake', '~> 10.0'
24
+ spec.add_development_dependency 'rspec', '~> 3.0'
25
+ end
@@ -0,0 +1,4 @@
1
+ require 'grape/named_routes/version'
2
+ require 'grape/named_routes/reopened_classes/grape'
3
+ require 'grape/named_routes/reopened_classes/api'
4
+
@@ -0,0 +1,11 @@
1
+ module Grape
2
+ module NamedRoutes
3
+ class MissedRequiredParam < StandardError
4
+ attr_reader :missed_param
5
+
6
+ def initialize(param)
7
+ @missed_param = param
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,45 @@
1
+ module Grape
2
+ module NamedRoutes
3
+ module NamedPathHelper
4
+ def get_named_path(route_name, route_params = {})
5
+ endpoint = named_route_seeker.find_endpoint!(route_name)
6
+ NamedRoutes::PathCompiler.compile_path(endpoint.routes.first, route_params)
7
+ end
8
+
9
+ def find_endpoint(route_name)
10
+ named_route_seeker.find_endpoint(route_name)
11
+ end
12
+
13
+ def find_endpoint!(route_name)
14
+ named_route_seeker.find_endpoint!(route_name)
15
+ end
16
+
17
+ def method_missing(method_name, *arguments)
18
+ if method_is_named_path?(method_name)
19
+ route_name = method_name.to_s.sub(/_path$/, '')
20
+ get_named_path(route_name, arguments.first || {})
21
+ else
22
+ super
23
+ end
24
+ end
25
+
26
+ def respond_to_missing?(method_name, include_private = false)
27
+ method_is_named_path?(method_name) || super
28
+ end
29
+
30
+ private
31
+
32
+ def named_route_seeker
33
+ @named_route_seeker ||= NamedRoutes::NamedRouteSeeker.new(self)
34
+ end
35
+
36
+ def method_is_named_path?(method_name)
37
+ if method_name.to_s.end_with?('_path')
38
+ route_name = method_name.to_s.sub(/_path$/, '')
39
+ return true if named_route_seeker.named_endpoint_present?(route_name)
40
+ end
41
+ false
42
+ end
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,11 @@
1
+ module Grape
2
+ module NamedRoutes
3
+ class NamedRouteNotFound < StandardError
4
+ attr_reader :missed_route
5
+
6
+ def initialize(route_name)
7
+ @missed_route = route_name
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,39 @@
1
+ module Grape
2
+ module NamedRoutes
3
+ class NamedRouteSeeker
4
+ def initialize(app)
5
+ @app = app
6
+ end
7
+
8
+ def find_endpoint(name)
9
+ name_to_endpoint_hash[name.to_sym]
10
+ end
11
+
12
+ def find_endpoint!(name)
13
+ fail NamedRouteNotFound.new(name), "Named route '#{name}' is missed." unless named_endpoint_present?(name)
14
+ find_endpoint(name)
15
+ end
16
+
17
+
18
+ def named_endpoint_present?(name)
19
+ name_to_endpoint_hash.key?(name.to_sym)
20
+ end
21
+
22
+ private
23
+
24
+ def named_endpoints
25
+ @named_endpoints ||= @app.endpoints.select do |endpoint|
26
+ endpoint.options[:route_options].key?(:as)
27
+ end
28
+ end
29
+
30
+ def name_to_endpoint_hash
31
+ @name_to_endpoint_hash ||= named_endpoints.inject({}) do |hash, endpoint|
32
+ endpoint_name = endpoint.options[:route_options][:as].to_sym
33
+ hash[endpoint_name] = endpoint
34
+ hash
35
+ end
36
+ end
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,49 @@
1
+ module Grape
2
+ module NamedRoutes
3
+ module PathCompiler
4
+ class << self
5
+ # this method substitutes given params to route path
6
+ def compile_path(route, params = {})
7
+ path_with_optional_params = substitute_optional_params(route.route_path, params)
8
+ substitute_required_params(path_with_optional_params, params)
9
+ end
10
+
11
+ private
12
+
13
+ def substitute_optional_params(path, params)
14
+ # explanation for regexp below: we seek parenthesis
15
+ # with string like :string in them (possibly appended and/or prepended with smth)
16
+ #
17
+ # for example, for string '/api/statuses/:id/(.:format)'
18
+ # match[1] will be '.'
19
+ # match[2] will be param_value(params, format)
20
+ # match[3] will be nil
21
+ #
22
+ # It could be rewrote with $1,$2,$3, but Rubocop doesn't like these variables
23
+ optional_params_regexp = /\((.*):(\w+)(.*)\)/
24
+
25
+ path.gsub(optional_params_regexp) do
26
+ match = Regexp.last_match
27
+ value = param_value(params, match[2])
28
+ [match[1], value, match[3]].join if value
29
+ end
30
+ end
31
+
32
+ def substitute_required_params(path, params)
33
+ path.gsub(/:(\w+)/) do
34
+ param = Regexp.last_match[1]
35
+ value = param_value(params, param)
36
+ unless value
37
+ fail Grape::NamedRoutes::MissedRequiredParam.new(param), "Required param '#{param}' is missed."
38
+ end
39
+ value
40
+ end
41
+ end
42
+
43
+ def param_value(params, param)
44
+ params[param] || params[param.to_sym]
45
+ end
46
+ end
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,13 @@
1
+ module Grape
2
+ class API
3
+ extend NamedRoutes::NamedPathHelper
4
+
5
+ class << self
6
+ alias_method :original_reset!, :reset!
7
+ def reset!
8
+ original_reset!
9
+ @named_route_seeker = nil
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,11 @@
1
+ require 'active_support/dependencies/autoload'
2
+ module Grape
3
+ module NamedRoutes
4
+ extend ActiveSupport::Autoload
5
+ autoload :NamedRouteNotFound
6
+ autoload :NamedRouteSeeker
7
+ autoload :PathCompiler
8
+ autoload :MissedRequiredParam
9
+ autoload :NamedPathHelper
10
+ end
11
+ end
@@ -0,0 +1,5 @@
1
+ module Grape
2
+ module NamedRoutes
3
+ VERSION = '0.1.0'
4
+ end
5
+ end
metadata ADDED
@@ -0,0 +1,119 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: grape-named_routes
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Anton Chuchkalov
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-06-29 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.11'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0.11'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.10'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.10'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '3.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3.0'
69
+ description: This implementation is really simple and not very dynamic, but may be
70
+ handy.
71
+ email:
72
+ - a2new@yandex.ru
73
+ executables: []
74
+ extensions: []
75
+ extra_rdoc_files: []
76
+ files:
77
+ - ".gitignore"
78
+ - ".rspec"
79
+ - ".travis.yml"
80
+ - Gemfile
81
+ - LICENSE.txt
82
+ - README.md
83
+ - Rakefile
84
+ - grape-named_routes.gemspec
85
+ - lib/grape/named_routes.rb
86
+ - lib/grape/named_routes/missed_required_param.rb
87
+ - lib/grape/named_routes/named_path_helper.rb
88
+ - lib/grape/named_routes/named_route_not_found.rb
89
+ - lib/grape/named_routes/named_route_seeker.rb
90
+ - lib/grape/named_routes/path_compiler.rb
91
+ - lib/grape/named_routes/reopened_classes/api.rb
92
+ - lib/grape/named_routes/reopened_classes/grape.rb
93
+ - lib/grape/named_routes/version.rb
94
+ homepage: https://github.com/hedgesky/grape-named_routes
95
+ licenses:
96
+ - MIT
97
+ metadata: {}
98
+ post_install_message:
99
+ rdoc_options: []
100
+ require_paths:
101
+ - lib
102
+ required_ruby_version: !ruby/object:Gem::Requirement
103
+ requirements:
104
+ - - ">="
105
+ - !ruby/object:Gem::Version
106
+ version: '0'
107
+ required_rubygems_version: !ruby/object:Gem::Requirement
108
+ requirements:
109
+ - - ">="
110
+ - !ruby/object:Gem::Version
111
+ version: '0'
112
+ requirements: []
113
+ rubyforge_project:
114
+ rubygems_version: 2.4.8
115
+ signing_key:
116
+ specification_version: 4
117
+ summary: Add support for named routes in grape.
118
+ test_files: []
119
+ has_rdoc: