grape-raketasks 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 54f52266aea4d335c644a785522633a0c1f67de3
4
- data.tar.gz: 35edad40355fc1e36d4051bbdd5c1ee7cfbe4180
3
+ metadata.gz: b36e73fad304800dd400fb2778fdfb7a92aaec3f
4
+ data.tar.gz: 37f8ca1dd7db650aed3b777dd90b3df0a531ff6e
5
5
  SHA512:
6
- metadata.gz: 1cb39671ddf1853690fe2c3aeeb9c90791438558ee6e79122ec00c9e7a717a0f699fbdeef98dbf9ffd331ece12d47e0f594393c5322eea119ea64a1f1f42686e
7
- data.tar.gz: 8e6a2769fdf071b3185dc3557bc6367273fb327c40b643631d8a50ba606c85047447182752b9e6ed11870f0414867a42d82d05aa0e9db96b963218086781d973
6
+ metadata.gz: 9a7d2ef78f84c8461a664c374eace6762716f06d8a25066f4d412e082ea08975bc5be25b7df94bf718a59550fde89cb4c6fd4381500586e57917675b679d80be
7
+ data.tar.gz: 1dd7e4e72181f900a4b4894cdbf0e91910e6fe1db1d592cf0504fc83d6acd543e827c68b2cf7ed3e6a68238ddafdc38cc2d9dca90c74b96fa347f6dec70715fb
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format=documentation
data/.rubocop.yml ADDED
@@ -0,0 +1,22 @@
1
+ AllCops:
2
+ Exclude:
3
+ - vendor/**/*
4
+ - bin/**/*
5
+
6
+ Documentation:
7
+ Enabled: false
8
+
9
+ LineLength:
10
+ Enabled: false
11
+
12
+ MethodLength:
13
+ Enabled: true
14
+
15
+ ClassLength:
16
+ Enabled: false
17
+
18
+ FileName:
19
+ Enabled: false
20
+
21
+ Encoding:
22
+ Enabled: false
data/.travis.yml ADDED
@@ -0,0 +1,18 @@
1
+ language: ruby
2
+
3
+ cache: bundler
4
+
5
+ rvm:
6
+ - ruby-head
7
+ - 2.1.1
8
+ - 2.1.0
9
+ - 2.0.0
10
+ - 1.9.3
11
+ - jruby-19mode
12
+ - jruby-head
13
+ - rbx-2
14
+
15
+ matrix:
16
+ allow_failures:
17
+ - rvm: ruby-head
18
+ - rvm: jruby-head
data/Gemfile CHANGED
@@ -2,3 +2,4 @@ source 'https://rubygems.org'
2
2
 
3
3
  gemspec
4
4
 
5
+ gem 'rubocop', '0.22.0'
data/README.md CHANGED
@@ -2,6 +2,8 @@
2
2
 
3
3
  Rake tasks to ease the development and debugging of Grape APIs.
4
4
 
5
+ [![Build Status](https://travis-ci.org/reprah/grape-raketasks.svg)](https://travis-ci.org/reprah/grape-raketasks)
6
+
5
7
  ## Available Tasks
6
8
 
7
9
  ### Routes
@@ -10,15 +12,27 @@ Rake tasks to ease the development and debugging of Grape APIs.
10
12
 
11
13
  #### Filtering
12
14
 
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`.
15
+ If you want to see routes belonging to only one API:
16
+
17
+ Pass an environment variable set to your API name after writing the task. Given the API below, and assuming we only want to see routes belonging to this CatPictures API...
18
+
19
+ ```ruby
20
+ module CatPictures
21
+ class API < Grape::API
22
+ # API stuff
23
+ end
24
+ end
25
+ ```
26
+
27
+ I'd execute `rake grape_raketasks:routes API=CatPictures::API`. Notice how we have to list which constants the API is nested in (if any), separated by a double colon, like in Ruby code.
14
28
 
15
29
  ## Installation
16
30
 
17
- 1.) grape-raketasks isn't served on Rubygems yet. Reference this repository, or your own fork, from a Gemfile:
31
+ 1.) Add `grape-raketasks` to your Gemfile:
18
32
 
19
33
  ```ruby
20
34
  # Gemfile
21
- gem 'grape-raketasks', git: 'git://github.com/reprah/grape-raketasks'
35
+ gem 'grape-raketasks'
22
36
  ```
23
37
 
24
38
  2.) Install the gem via Bundler:
@@ -26,7 +40,11 @@ gem 'grape-raketasks', git: 'git://github.com/reprah/grape-raketasks'
26
40
  ```shell
27
41
  $ bundle install
28
42
  ```
29
- If you don't want to use Bundler, follow the [instructions here](http://ruby.about.com/od/advancedruby/a/gitgem.htm).
43
+ or on the command line:
44
+
45
+ ```shell
46
+ $ gem install grape-raketasks
47
+ ```
30
48
 
31
49
  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
50
 
@@ -34,7 +52,7 @@ If you don't want to use Bundler, follow the [instructions here](http://ruby.abo
34
52
  # Rakefile
35
53
 
36
54
  require 'rake'
37
- require 'bundler'
55
+ require 'bundler'
38
56
  Bundler.setup
39
57
  require 'grape-raketasks'
40
58
  require 'grape-raketasks/tasks'
@@ -52,17 +70,12 @@ Rails applications with mounted Grape APIs don't require an extra step here.
52
70
 
53
71
  ## Contributing
54
72
 
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
73
+ 1. Fork it
74
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
75
+ 3. Write specs for your feature
76
+ 4. Commit your changes (`git commit -am 'Add some feature'`)
77
+ 5. Push to the branch (`git push origin my-new-feature`)
78
+ 6. Create a new pull request
66
79
 
67
80
  ## License
68
81
 
data/Rakefile ADDED
@@ -0,0 +1,19 @@
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
+ task :spec
14
+
15
+ require 'rainbow/ext/string' unless String.respond_to?(:color)
16
+ require 'rubocop/rake_task'
17
+ Rubocop::RakeTask.new(:rubocop)
18
+
19
+ task default: [:rubocop, :spec]
@@ -2,7 +2,7 @@ require File.dirname(__FILE__) + '/lib/grape-raketasks/version'
2
2
 
3
3
  Gem::Specification.new do |gem|
4
4
  gem.name = 'grape-raketasks'
5
- gem.version = GrapeRakeTasks::VERSION
5
+ gem.version = GrapeRakeTasks::VERSION
6
6
 
7
7
  gem.homepage = 'https://github.com/reprah/grape-raketasks'
8
8
  gem.license = 'MIT'
@@ -10,13 +10,12 @@ Gem::Specification.new do |gem|
10
10
  gem.description = 'Provides rake tasks to ease the development and debugging of Grape APIs.'
11
11
  gem.author = 'H. Henn'
12
12
 
13
- gem.files = `git ls-files`.split($\)
13
+ gem.files = `git ls-files`.split($OUTPUT_RECORD_SEPARATOR)
14
14
  gem.test_files = gem.files.grep(/^spec/)
15
15
 
16
- gem.add_runtime_dependency 'grape'
17
- gem.add_runtime_dependency 'rake'
18
- gem.add_runtime_dependency 'activesupport'
16
+ gem.add_runtime_dependency 'grape'
17
+ gem.add_runtime_dependency 'rake'
18
+ gem.add_runtime_dependency 'activesupport'
19
19
 
20
20
  gem.add_development_dependency 'rspec'
21
21
  end
22
-
@@ -12,14 +12,19 @@ module GrapeRakeTasks
12
12
  buffer.join << "\n\n"
13
13
  end
14
14
 
15
- def construct_output(routes)
15
+ def construct_output(routes, filter = nil)
16
16
  if routes.any?
17
- buffer << routes.map { |r| format_route(r) }.join("\n\n")
17
+ buffer << formatted_routes(routes)
18
18
  else
19
- buffer << no_routes_message
19
+ buffer << no_routes_message(filter)
20
20
  end
21
21
  end
22
22
 
23
+ def formatted_routes(routes)
24
+ formatted = routes.map { |r| format_route(r) }
25
+ formatted.join("\n\n")
26
+ end
27
+
23
28
  def format_route(route)
24
29
  opts = route.options
25
30
  # two characters (colon + space) after the title
@@ -43,12 +48,12 @@ module GrapeRakeTasks
43
48
  key.to_s.upcase.concat(': ')
44
49
  end
45
50
 
46
- def no_routes_message
51
+ def no_routes_message(filter)
52
+ subject = filter ? filter : 'your application'
47
53
  <<-MSG.strip_heredoc
48
- You don't have any Grape routes defined!
54
+ You don't have any Grape routes defined for #{subject}.
49
55
  Visit https://github.com/intridea/grape for help.
50
56
  MSG
51
57
  end
52
58
  end
53
59
  end
54
-
@@ -10,15 +10,25 @@ module GrapeRakeTasks
10
10
 
11
11
  def format(formatter, filter = nil)
12
12
  routes_to_display = filter_by_api(filter)
13
- formatter.construct_output(routes_to_display)
13
+ formatter.construct_output(routes_to_display, filter)
14
14
  formatter.result
15
15
  end
16
16
 
17
17
  def filter_by_api(filter = nil)
18
18
  return routes unless filter
19
- filter_as_class = "#{filter.camelcase}::API"
20
- routes.select { |r| r.route_api.to_s == filter_as_class }
19
+ pattern = Regexp.new(filter, Regexp::IGNORECASE)
20
+ filtered = routes.select do |r|
21
+ matches_filter_pattern?(r, pattern)
22
+ end
23
+ filtered.uniq
24
+ end
25
+
26
+ private
27
+
28
+ def matches_filter_pattern?(route, pattern)
29
+ # match filter against string representation of a route's API
30
+ api_as_string = route.route_api.to_s
31
+ api_as_string.match(pattern)
21
32
  end
22
33
  end
23
34
  end
24
-
@@ -1,8 +1,10 @@
1
1
  require 'active_support/core_ext/class'
2
2
 
3
3
  # enable adding the API name to a route's options
4
- class Grape::Route
5
- attr_accessor :options
4
+ module Grape
5
+ class Route
6
+ attr_accessor :options
7
+ end
6
8
  end
7
9
 
8
10
  module GrapeRakeTasks
@@ -26,4 +28,3 @@ module GrapeRakeTasks
26
28
  end
27
29
  end
28
30
  end
29
-
@@ -1,2 +1 @@
1
1
  load File.expand_path('../../tasks/grape-raketasks.rake', __FILE__)
2
-
@@ -1,4 +1,3 @@
1
1
  module GrapeRakeTasks
2
- VERSION = '0.0.1'
2
+ VERSION = '0.0.2'
3
3
  end
4
-
@@ -6,4 +6,3 @@ require 'grape-raketasks/console_formatter'
6
6
  module GrapeTasks
7
7
  require 'grape-raketasks/railtie' if defined?(Rails)
8
8
  end
9
-
@@ -1,9 +1,9 @@
1
1
  namespace :grape_raketasks do
2
- desc 'Print routes provided by Grape APIs to the terminal. Target a specific API with GRAPE_API=x.'
2
+ desc 'Print routes provided by Grape APIs to the terminal. Target a specific API with API=x.'
3
3
  task routes: :environment do
4
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'])
5
+ processor = GrapeRakeTasks::Processor.new(all_routes)
6
+ puts processor.format(GrapeRakeTasks::ConsoleFormatter.new, ENV['API'])
7
7
  end
8
8
  end
9
9
 
@@ -5,19 +5,19 @@ describe GrapeRakeTasks::ConsoleFormatter do
5
5
 
6
6
  describe '#construct_output' do
7
7
  before do
8
- formatter.stub(format_route: 'formatted route')
8
+ formatter.stub(format_route: 'formatted route')
9
9
  end
10
10
 
11
11
  context 'when routes exist' do
12
12
  before do
13
- formatter.construct_output(['a route'])
13
+ formatter.construct_output(['a route'])
14
14
  end
15
15
 
16
16
  it 'adds formatted routes to the buffer' do
17
17
  expect(formatter.buffer).to include('formatted route')
18
18
  end
19
19
  end
20
-
20
+
21
21
  context 'when no routes exist' do
22
22
  before do
23
23
  formatter.construct_output([])
@@ -25,7 +25,7 @@ describe GrapeRakeTasks::ConsoleFormatter do
25
25
 
26
26
  it 'adds a message to the buffer' do
27
27
  msg = formatter.buffer.first
28
- expect(msg).to include("You don't have any Grape routes defined!")
28
+ expect(msg).to include("You don't have any Grape routes")
29
29
  end
30
30
  end
31
31
  end
@@ -35,7 +35,7 @@ describe GrapeRakeTasks::ConsoleFormatter do
35
35
 
36
36
  it 'returns a text representation of a route object' do
37
37
  result = formatter.format_route(route_object)
38
- expect(result).to match(/METHOD:\s+"GET"/)
38
+ expect(result).to match(/METHOD:\s+"GET"/)
39
39
  end
40
40
  end
41
41
 
@@ -49,4 +49,3 @@ describe GrapeRakeTasks::ConsoleFormatter do
49
49
  end
50
50
  end
51
51
  end
52
-
@@ -8,16 +8,36 @@ describe GrapeRakeTasks::Processor do
8
8
 
9
9
  let(:processor) { described_class.new(routes) }
10
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
11
+ shared_examples 'successful API filtering' do
12
+ it 'returns routes belonging to one API' do
15
13
  filtered = processor.filter_by_api(filter)
16
14
  filtered_apis = filtered.map(&:route_api)
17
15
  expect(filtered_apis).to eq [SampleApiOne::API]
18
16
  end
19
17
  end
20
18
 
19
+ context 'when given a Grape API to filter by' do
20
+ let(:filter) { 'SampleAPIOne::API' }
21
+
22
+ it_behaves_like 'successful API filtering'
23
+
24
+ context "when filter's case does not exactly match an API" do
25
+ let(:filter) { 'SampleApiOne::API' }
26
+
27
+ it_behaves_like 'successful API filtering'
28
+ end
29
+
30
+ context 'when API is not nested within another constant' do
31
+ let(:filter) { 'SampleAPIThree' }
32
+
33
+ it 'returns routes belonging to that api' do
34
+ filtered = processor.filter_by_api(filter)
35
+ filtered_apis = filtered.map(&:route_api)
36
+ expect(filtered_apis).to include(SampleApiThree)
37
+ end
38
+ end
39
+ end
40
+
21
41
  context 'when no filter is given' do
22
42
  it 'returns routes from every Grape API' do
23
43
  unfiltered = processor.filter_by_api
@@ -27,4 +47,3 @@ describe GrapeRakeTasks::Processor do
27
47
  end
28
48
  end
29
49
  end
30
-
@@ -15,7 +15,7 @@ describe GrapeRakeTasks::Route do
15
15
 
16
16
  it 'returns a collection of routes that know their parent API' do
17
17
  routes = described_class.api_routes(SampleApiOne::API)
18
- every_route_has_api = routes.all? { |r| r.options.has_key?(:api) }
18
+ every_route_has_api = routes.all? { |r| r.options.key?(:api) }
19
19
  expect(every_route_has_api).to be_true
20
20
  end
21
21
  end
data/spec/spec_helper.rb CHANGED
@@ -2,4 +2,3 @@ require 'grape-raketasks'
2
2
 
3
3
  # load support files
4
4
  Dir.glob(ENV['PWD'] + '/spec/support/*.rb').each { |f| require f }
5
-
@@ -6,17 +6,19 @@ def grape_route_object
6
6
  g
7
7
  end
8
8
 
9
- class SampleApiOne
9
+ module SampleApiOne
10
10
  class API < Grape::API
11
11
  end
12
12
  end
13
13
 
14
- class SampleApiTwo < Grape::API
14
+ module SampleApiTwo
15
15
  class API < Grape::API
16
16
  end
17
17
  end
18
18
 
19
- [SampleApiOne::API, SampleApiTwo::API].each do |api|
20
- api.routes << grape_route_object
19
+ class SampleApiThree < Grape::API
21
20
  end
22
21
 
22
+ Grape::API.subclasses.each do |api|
23
+ api.routes << grape_route_object
24
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: grape-raketasks
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - H. Henn
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-05-25 00:00:00.000000000 Z
11
+ date: 2014-06-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: grape
@@ -73,9 +73,13 @@ extensions: []
73
73
  extra_rdoc_files: []
74
74
  files:
75
75
  - .gitignore
76
+ - .rspec
77
+ - .rubocop.yml
78
+ - .travis.yml
76
79
  - Gemfile
77
80
  - LICENSE
78
81
  - README.md
82
+ - Rakefile
79
83
  - grape-raketasks.gemspec
80
84
  - lib/grape-raketasks.rb
81
85
  - lib/grape-raketasks/console_formatter.rb