grape-raketasks 0.0.1

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: 54f52266aea4d335c644a785522633a0c1f67de3
4
+ data.tar.gz: 35edad40355fc1e36d4051bbdd5c1ee7cfbe4180
5
+ SHA512:
6
+ metadata.gz: 1cb39671ddf1853690fe2c3aeeb9c90791438558ee6e79122ec00c9e7a717a0f699fbdeef98dbf9ffd331ece12d47e0f594393c5322eea119ea64a1f1f42686e
7
+ data.tar.gz: 8e6a2769fdf071b3185dc3557bc6367273fb327c40b643631d8a50ba606c85047447182752b9e6ed11870f0414867a42d82d05aa0e9db96b963218086781d973
data/.gitignore ADDED
@@ -0,0 +1,6 @@
1
+ # Ruby
2
+ Gemfile.lock
3
+ *.gem
4
+
5
+ # Vim
6
+ *.sw[a-z]
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
4
+
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 H. Henn
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,69 @@
1
+ # grape-raketasks
2
+
3
+ Rake tasks to ease the development and debugging of Grape APIs.
4
+
5
+ ## Available Tasks
6
+
7
+ ### Routes
8
+
9
+ `rake grape_raketasks:routes` is like `rake routes` for your Grape APIs. All routes within every Grape API in your web application will be printed to the terminal, along with parameter requiremements, HTTP verb, the API it belongs to, etc.
10
+
11
+ #### Filtering
12
+
13
+ If you want to see routes belonging to only one API, pass an environment variable set to your API name in snake-case after writing the task. For example, if I wanted to view only routes belonging to my Grape API named CatPictures, I'd execute `rake grape_raketasks:routes GRAPE_API=cat_pictures`.
14
+
15
+ ## Installation
16
+
17
+ 1.) grape-raketasks isn't served on Rubygems yet. Reference this repository, or your own fork, from a Gemfile:
18
+
19
+ ```ruby
20
+ # Gemfile
21
+ gem 'grape-raketasks', git: 'git://github.com/reprah/grape-raketasks'
22
+ ```
23
+
24
+ 2.) Install the gem via Bundler:
25
+
26
+ ```shell
27
+ $ bundle install
28
+ ```
29
+ If you don't want to use Bundler, follow the [instructions here](http://ruby.about.com/od/advancedruby/a/gitgem.htm).
30
+
31
+ 3.) If your Grape APIs are defined in a Sinatra or Rack web application, you need to write a rake task called `:environment`that loads the application's environment first. This gem's tasks are dependent on it. You could put this in the root of your project directory:
32
+
33
+ ```ruby
34
+ # Rakefile
35
+
36
+ require 'rake'
37
+ require 'bundler'
38
+ Bundler.setup
39
+ require 'grape-raketasks'
40
+ require 'grape-raketasks/tasks'
41
+
42
+ desc 'load the Sinatra environment.'
43
+ task :environment do
44
+ require File.expand_path('your_app_file', File.dirname(__FILE__))
45
+ end
46
+ ```
47
+ Rails applications with mounted Grape APIs don't require an extra step here.
48
+
49
+ 4.) Run `rake -T` to see all available rake tasks. Tasks with a `grape_raketasks` namespace should appear somewhere.
50
+
51
+ 5.) Use the tasks! Find bugs or ideas for improvement! Report them here!
52
+
53
+ ## Contributing
54
+
55
+ 1.) Fork it
56
+
57
+ 2.) Create your feature branch (`git checkout -b my-new-feature`)
58
+
59
+ 3.) Write specs for your feature
60
+
61
+ 4.) Commit your changes (`git commit -am 'Add some feature'`)
62
+
63
+ 5.) Push to the branch (`git push origin my-new-feature`)
64
+
65
+ 6.) Create a new pull request
66
+
67
+ ## License
68
+
69
+ See LICENSE
@@ -0,0 +1,22 @@
1
+ require File.dirname(__FILE__) + '/lib/grape-raketasks/version'
2
+
3
+ Gem::Specification.new do |gem|
4
+ gem.name = 'grape-raketasks'
5
+ gem.version = GrapeRakeTasks::VERSION
6
+
7
+ gem.homepage = 'https://github.com/reprah/grape-raketasks'
8
+ gem.license = 'MIT'
9
+ gem.summary = 'Rake tasks for web applications using Grape APIs.'
10
+ gem.description = 'Provides rake tasks to ease the development and debugging of Grape APIs.'
11
+ gem.author = 'H. Henn'
12
+
13
+ gem.files = `git ls-files`.split($\)
14
+ gem.test_files = gem.files.grep(/^spec/)
15
+
16
+ gem.add_runtime_dependency 'grape'
17
+ gem.add_runtime_dependency 'rake'
18
+ gem.add_runtime_dependency 'activesupport'
19
+
20
+ gem.add_development_dependency 'rspec'
21
+ end
22
+
@@ -0,0 +1,54 @@
1
+ require 'active_support/core_ext/string'
2
+
3
+ module GrapeRakeTasks
4
+ class ConsoleFormatter
5
+ attr_reader :buffer
6
+
7
+ def initialize
8
+ @buffer = []
9
+ end
10
+
11
+ def result
12
+ buffer.join << "\n\n"
13
+ end
14
+
15
+ def construct_output(routes)
16
+ if routes.any?
17
+ buffer << routes.map { |r| format_route(r) }.join("\n\n")
18
+ else
19
+ buffer << no_routes_message
20
+ end
21
+ end
22
+
23
+ def format_route(route)
24
+ opts = route.options
25
+ # two characters (colon + space) after the title
26
+ longest_key_length = opts.keys.map(&:length).max + 2
27
+
28
+ opts.sort.map do |key, value|
29
+ title = key_to_title(key)
30
+ title << build_padding(longest_key_length, key.length)
31
+ title << value.inspect
32
+ end.join("\n")
33
+ end
34
+
35
+ private
36
+
37
+ def build_padding(max_len, title_len)
38
+ # whitespace padding used to align output
39
+ ' ' * (max_len - title_len)
40
+ end
41
+
42
+ def key_to_title(key)
43
+ key.to_s.upcase.concat(': ')
44
+ end
45
+
46
+ def no_routes_message
47
+ <<-MSG.strip_heredoc
48
+ You don't have any Grape routes defined!
49
+ Visit https://github.com/intridea/grape for help.
50
+ MSG
51
+ end
52
+ end
53
+ end
54
+
@@ -0,0 +1,24 @@
1
+ require 'active_support/core_ext/string'
2
+
3
+ module GrapeRakeTasks
4
+ class Processor
5
+ attr_reader :routes
6
+
7
+ def initialize(routes)
8
+ @routes = routes
9
+ end
10
+
11
+ def format(formatter, filter = nil)
12
+ routes_to_display = filter_by_api(filter)
13
+ formatter.construct_output(routes_to_display)
14
+ formatter.result
15
+ end
16
+
17
+ def filter_by_api(filter = nil)
18
+ return routes unless filter
19
+ filter_as_class = "#{filter.camelcase}::API"
20
+ routes.select { |r| r.route_api.to_s == filter_as_class }
21
+ end
22
+ end
23
+ end
24
+
@@ -0,0 +1,7 @@
1
+ module GrapeRakeTasks
2
+ class Railtie < Rails::Railtie
3
+ rake_tasks do
4
+ Dir[File.join(File.dirname(__FILE__), '../tasks/*.rake')].each { |f| load f }
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,29 @@
1
+ require 'active_support/core_ext/class'
2
+
3
+ # enable adding the API name to a route's options
4
+ class Grape::Route
5
+ attr_accessor :options
6
+ end
7
+
8
+ module GrapeRakeTasks
9
+ class Route
10
+ def self.all_routes(api)
11
+ api.subclasses.flat_map do |klass|
12
+ api_routes(klass)
13
+ end
14
+ end
15
+
16
+ def self.api_routes(api)
17
+ api.routes.map do |grape_route|
18
+ route_with_api_name(grape_route, api)
19
+ end
20
+ end
21
+
22
+ def self.route_with_api_name(grape_route, api_name)
23
+ new_opts = grape_route.options.merge(api: api_name)
24
+ grape_route.options = new_opts
25
+ grape_route
26
+ end
27
+ end
28
+ end
29
+
@@ -0,0 +1,2 @@
1
+ load File.expand_path('../../tasks/grape-raketasks.rake', __FILE__)
2
+
@@ -0,0 +1,4 @@
1
+ module GrapeRakeTasks
2
+ VERSION = '0.0.1'
3
+ end
4
+
@@ -0,0 +1,9 @@
1
+ require 'grape'
2
+ require 'grape-raketasks/route'
3
+ require 'grape-raketasks/processor'
4
+ require 'grape-raketasks/console_formatter'
5
+
6
+ module GrapeTasks
7
+ require 'grape-raketasks/railtie' if defined?(Rails)
8
+ end
9
+
@@ -0,0 +1,9 @@
1
+ namespace :grape_raketasks do
2
+ desc 'Print routes provided by Grape APIs to the terminal. Target a specific API with GRAPE_API=x.'
3
+ task routes: :environment do
4
+ all_routes = GrapeRakeTasks::Route.all_routes(Grape::API)
5
+ inspector = GrapeRakeTasks::Processor.new(all_routes)
6
+ puts inspector.format(GrapeRakeTasks::ConsoleFormatter.new, ENV['GRAPE_API'])
7
+ end
8
+ end
9
+
@@ -0,0 +1,52 @@
1
+ require 'spec_helper'
2
+
3
+ describe GrapeRakeTasks::ConsoleFormatter do
4
+ let(:formatter) { described_class.new }
5
+
6
+ describe '#construct_output' do
7
+ before do
8
+ formatter.stub(format_route: 'formatted route')
9
+ end
10
+
11
+ context 'when routes exist' do
12
+ before do
13
+ formatter.construct_output(['a route'])
14
+ end
15
+
16
+ it 'adds formatted routes to the buffer' do
17
+ expect(formatter.buffer).to include('formatted route')
18
+ end
19
+ end
20
+
21
+ context 'when no routes exist' do
22
+ before do
23
+ formatter.construct_output([])
24
+ end
25
+
26
+ it 'adds a message to the buffer' do
27
+ msg = formatter.buffer.first
28
+ expect(msg).to include("You don't have any Grape routes defined!")
29
+ end
30
+ end
31
+ end
32
+
33
+ describe '#format_route' do
34
+ let(:route_object) { grape_route_object }
35
+
36
+ it 'returns a text representation of a route object' do
37
+ result = formatter.format_route(route_object)
38
+ expect(result).to match(/METHOD:\s+"GET"/)
39
+ end
40
+ end
41
+
42
+ describe '#result' do
43
+ before do
44
+ formatter.construct_output([])
45
+ end
46
+
47
+ it 'returns the contents of the buffer as a string' do
48
+ expect(formatter.result).to be_a(String)
49
+ end
50
+ end
51
+ end
52
+
@@ -0,0 +1,30 @@
1
+ require 'spec_helper'
2
+
3
+ describe GrapeRakeTasks::Processor do
4
+ describe '#filter_by_api' do
5
+ let(:routes) do
6
+ GrapeRakeTasks::Route.all_routes(Grape::API)
7
+ end
8
+
9
+ let(:processor) { described_class.new(routes) }
10
+
11
+ context 'when given a Grape API to filter by' do
12
+ let(:filter) { 'sample_api_one' }
13
+
14
+ it 'returns only routes belonging to that API' do
15
+ filtered = processor.filter_by_api(filter)
16
+ filtered_apis = filtered.map(&:route_api)
17
+ expect(filtered_apis).to eq [SampleApiOne::API]
18
+ end
19
+ end
20
+
21
+ context 'when no filter is given' do
22
+ it 'returns routes from every Grape API' do
23
+ unfiltered = processor.filter_by_api
24
+ unfiltered_apis = unfiltered.map(&:route_api)
25
+ expect(unfiltered_apis.length).to eq routes.length
26
+ end
27
+ end
28
+ end
29
+ end
30
+
@@ -0,0 +1,30 @@
1
+ require 'spec_helper'
2
+
3
+ describe GrapeRakeTasks::Route do
4
+ describe '.route_with_api_name' do
5
+ let(:route) { grape_route_object }
6
+
7
+ it 'adds an API name to a route\'s options hash' do
8
+ altered_route = described_class.route_with_api_name(route, SampleApiOne::API)
9
+ expect(altered_route.options).to have_key(:api)
10
+ end
11
+ end
12
+
13
+ describe '.api_routes' do
14
+ let(:api_class) { SampleApiOne::API }
15
+
16
+ it 'returns a collection of routes that know their parent API' do
17
+ routes = described_class.api_routes(SampleApiOne::API)
18
+ every_route_has_api = routes.all? { |r| r.options.has_key?(:api) }
19
+ expect(every_route_has_api).to be_true
20
+ end
21
+ end
22
+
23
+ describe '.all_routes' do
24
+ it 'returns routes belonging to every subclass' do
25
+ routes = described_class.all_routes(Grape::API)
26
+ api_names = routes.map(&:route_api)
27
+ expect(api_names).to include(SampleApiOne::API)
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,5 @@
1
+ require 'grape-raketasks'
2
+
3
+ # load support files
4
+ Dir.glob(ENV['PWD'] + '/spec/support/*.rb').each { |f| require f }
5
+
@@ -0,0 +1,22 @@
1
+ # test objects
2
+
3
+ def grape_route_object
4
+ g = Grape::Route.new
5
+ g.options = { path: '/', method: 'GET' }
6
+ g
7
+ end
8
+
9
+ class SampleApiOne
10
+ class API < Grape::API
11
+ end
12
+ end
13
+
14
+ class SampleApiTwo < Grape::API
15
+ class API < Grape::API
16
+ end
17
+ end
18
+
19
+ [SampleApiOne::API, SampleApiTwo::API].each do |api|
20
+ api.routes << grape_route_object
21
+ end
22
+
metadata ADDED
@@ -0,0 +1,122 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: grape-raketasks
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - H. Henn
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-05-25 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: rake
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
+ - !ruby/object:Gem::Dependency
42
+ name: activesupport
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '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: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description: Provides rake tasks to ease the development and debugging of Grape APIs.
70
+ email:
71
+ executables: []
72
+ extensions: []
73
+ extra_rdoc_files: []
74
+ files:
75
+ - .gitignore
76
+ - Gemfile
77
+ - LICENSE
78
+ - README.md
79
+ - grape-raketasks.gemspec
80
+ - lib/grape-raketasks.rb
81
+ - lib/grape-raketasks/console_formatter.rb
82
+ - lib/grape-raketasks/processor.rb
83
+ - lib/grape-raketasks/railtie.rb
84
+ - lib/grape-raketasks/route.rb
85
+ - lib/grape-raketasks/tasks.rb
86
+ - lib/grape-raketasks/version.rb
87
+ - lib/tasks/grape-raketasks.rake
88
+ - spec/grape-raketasks/console_formatter_spec.rb
89
+ - spec/grape-raketasks/processor_spec.rb
90
+ - spec/grape-raketasks/route_spec.rb
91
+ - spec/spec_helper.rb
92
+ - spec/support/test_objects.rb
93
+ homepage: https://github.com/reprah/grape-raketasks
94
+ licenses:
95
+ - MIT
96
+ metadata: {}
97
+ post_install_message:
98
+ rdoc_options: []
99
+ require_paths:
100
+ - lib
101
+ required_ruby_version: !ruby/object:Gem::Requirement
102
+ requirements:
103
+ - - '>='
104
+ - !ruby/object:Gem::Version
105
+ version: '0'
106
+ required_rubygems_version: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - '>='
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ requirements: []
112
+ rubyforge_project:
113
+ rubygems_version: 2.0.3
114
+ signing_key:
115
+ specification_version: 4
116
+ summary: Rake tasks for web applications using Grape APIs.
117
+ test_files:
118
+ - spec/grape-raketasks/console_formatter_spec.rb
119
+ - spec/grape-raketasks/processor_spec.rb
120
+ - spec/grape-raketasks/route_spec.rb
121
+ - spec/spec_helper.rb
122
+ - spec/support/test_objects.rb