napa 0.1.21 → 0.1.22

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: f3d14f08974a25ee574b448040d62e949e13c6f9
4
- data.tar.gz: 8c33fbaf760e8cd7531d56364214deec18aef603
3
+ metadata.gz: a86977739a2b0f1f4cf4aaa6e09e4e46cb418bb1
4
+ data.tar.gz: 17e5f2adcfac479a0dffaa387426cb4bc62f332d
5
5
  SHA512:
6
- metadata.gz: e799425227024c8a459633c1a1e89720538f7ac729993f21deb360c195ec758d90aeafe4bf1fd4f5a2946e5c744b6e29ce6d28734d5d6f2c20ed6f1949d5c243
7
- data.tar.gz: 809afa7e7c68385567b7864af4d92125a864e88c457eb0a6377dff29d3183537496fb0446b709a14f5fd9b224398ad3386e9b55f767edcd28adab2caee241e34
6
+ metadata.gz: c773958e1082a196633c1b3fcc0e98fd3d71749508067f962e8e5b4a372e8bee079c9a74abe0c136bedeb3ec08137a9990fb1cb6b9c132f9134f800056c04098
7
+ data.tar.gz: 270d6fae56b4ab43ef83ad14f85775e6e9d489985eb7aa2d20505837fdcd6fa4b8abf9a6fafd6dc6db1b1cbab1393aeb814b5d2761f63d94948d6c6c58d5dcbe
@@ -6,7 +6,7 @@ if defined?(ActiveRecord)
6
6
  # pass an empty where clause to force results to be a relation that will be lazy evaluated
7
7
  results = where({})
8
8
  search_hash.each do |k, v|
9
- results = results.where(k => v) if v.present?
9
+ results = results.where(k => v)
10
10
  end
11
11
  results
12
12
  end
@@ -1,12 +1,11 @@
1
1
  class <%= name.classify.pluralize %>Api < Grape::API
2
- represent <%= name.classify %>, with: <%= name.classify %>Entity
3
2
  desc 'Get a list of <%= name.underscore.tableize %>', entity: <%= name.classify %>Entity
4
3
  params do
5
4
  optional :ids, type: String, desc: 'comma separated <%= name.underscore %> ids'
6
5
  end
7
6
  get do
8
- <%= name.underscore.tableize %> = <%= name.classify %>.filter(declared(params))
9
- present <%= name.underscore.tableize %>
7
+ <%= name.underscore.tableize %> = <%= name.classify %>.filter(declared(params, include_missing: false))
8
+ present <%= name.underscore.tableize %>, with: <%= name.classify %>Entity
10
9
  end
11
10
 
12
11
  desc 'Create an <%= name.underscore %>', entity: <%= name.classify %>Entity
@@ -14,13 +13,9 @@ class <%= name.classify.pluralize %>Api < Grape::API
14
13
  end
15
14
 
16
15
  post do
17
- begin
18
- <%= name.underscore %> = <%= name.classify %>.create(declared(params))
19
- rescue NameError
20
- error!(present_error(:kind_not_found))
21
- end
16
+ <%= name.underscore %> = <%= name.classify %>.create(declared(params, include_missing: false))
22
17
  error!(present_error(:record_invalid, <%= name.underscore %>.errors.full_messages)) unless <%= name.underscore %>.errors.empty?
23
- present <%= name.underscore %>
18
+ present <%= name.underscore %>, with: <%= name.classify %>Entity
24
19
  end
25
20
 
26
21
  params do
@@ -30,7 +25,7 @@ class <%= name.classify.pluralize %>Api < Grape::API
30
25
  desc 'Get an <%= name.underscore %>', entity: <%= name.classify %>Entity
31
26
  get do
32
27
  <%= name.underscore %> = <%= name.classify %>.find(params[:id])
33
- present <%= name.underscore %>
28
+ present <%= name.underscore %>, with: <%= name.classify %>Entity
34
29
  end
35
30
 
36
31
  desc 'Update an <%= name.underscore %>', entity: <%= name.classify %>Entity
@@ -39,8 +34,8 @@ class <%= name.classify.pluralize %>Api < Grape::API
39
34
  put do
40
35
  # fetch <%= name.underscore %> record and update attributes. exceptions caught in app.rb
41
36
  <%= name.underscore %> = <%= name.classify %>.find(params[:id])
42
- <%= name.underscore %>.update_attributes!(declared(params).select { |param, value| value.present? })
43
- present <%= name.underscore %>
37
+ <%= name.underscore %>.update_attributes!(declared(params, include_missing: false).select { |param, value| value.present? })
38
+ present <%= name.underscore %>, with: <%= name.classify %>Entity
44
39
  end
45
40
  end
46
41
  end
@@ -1,7 +1,7 @@
1
- SERVICE_NAME={{app_name}}
1
+ SERVICE_NAME=<%= app_name %>
2
2
  RACK_ENV=test
3
3
 
4
4
  DATABASE_USER=root
5
5
  DATABASE_PASSWORD=''
6
6
  DATABASE_HOST=localhost
7
- DATABASE_NAME={{app_name}}_test
7
+ DATABASE_NAME=<%= app_name.underscore %>_test
@@ -0,0 +1,6 @@
1
+ SERVICE_NAME=<%= app_name %>
2
+
3
+ DATABASE_USER=root
4
+ DATABASE_PASSWORD=''
5
+ DATABASE_HOST=localhost
6
+ DATABASE_NAME=<%= app_name.underscore %>_development
@@ -4,7 +4,8 @@
4
4
  .env
5
5
  .vagrant
6
6
  .rspec
7
- .env.*
7
+ .env
8
+ !.env.test
8
9
  log/*
9
10
  tmp/*
10
11
  coverage
@@ -2,13 +2,11 @@ source 'https://rubygems.org'
2
2
 
3
3
  gem 'rack-cors'
4
4
  gem 'mysql2'
5
- gem 'activerecord', '3.2.13', :require => 'active_record'
5
+ gem 'activerecord', :require => 'active_record'
6
6
  gem 'honeybadger'
7
7
  gem 'json'
8
8
  gem 'shotgun'
9
9
  gem 'napa'
10
- gem 'grape-pagination'
11
- gem 'will_paginate', '~> 3.0'
12
10
 
13
11
  group :development,:test do
14
12
  gem 'pry'
@@ -0,0 +1,9 @@
1
+ class ApplicationApi
2
+ format :json
3
+ extend Napa::ApiExceptionCatchers
4
+
5
+ mount HelloApi => '/'
6
+
7
+ add_swagger_documentation
8
+ end
9
+
@@ -1,13 +1,10 @@
1
- module <%= app_name.classify %>
2
- class API < Grape::API
3
- format :json
1
+ class HelloApi < Grape::API
4
2
 
5
- resource :hello do
6
- desc 'Return a Hello World message'
7
- get do
8
- { message: 'Hello Wonderful World, from <%= app_name.classify %>!' }
9
- end
3
+ resource :hello do
4
+ desc 'Return a Hello World message'
5
+ get do
6
+ { message: 'Hello Wonderful World, from <%= app_name.classify %>!' }
10
7
  end
11
-
12
8
  end
9
+
13
10
  end
@@ -4,8 +4,6 @@ Bundler.setup(:default)
4
4
  require 'napa/setup'
5
5
  Bundler.require(:default, Napa.env.to_sym)
6
6
  require 'napa'
7
- require 'will_paginate'
8
- require 'will_paginate/active_record'
9
7
 
10
8
  # load environment
11
9
  Dotenv.load(Napa.env.test? ? '.env.test' : '.env')
@@ -20,4 +18,5 @@ Dir['./config/initializers/**/*.rb'].map { |file| require file }
20
18
  Dir['./config/middleware/**/*.rb'].map { |file| require file }
21
19
 
22
20
  # autoload app
23
- Dir['./app/**/**/*.rb'].map { |file| require file }
21
+ relative_load_paths = %w[app/apis app/entities app/models app/workers lib]
22
+ ActiveSupport::Dependencies.autoload_paths += relative_load_paths
@@ -14,5 +14,5 @@ use Napa::Middleware::AppMonitor
14
14
  use Napa::Middleware::Authentication
15
15
  use ActiveRecord::ConnectionAdapters::ConnectionManagement
16
16
 
17
- run <%= app_name.classify %>::API # <-- boot your service here --
17
+ run ApplicationApi # <-- boot your service here --
18
18
 
@@ -0,0 +1,17 @@
1
+ module Napa
2
+ module GrapeExtenders
3
+ def self.extended(modified_class)
4
+ # when extended, set the exceptions to handle
5
+
6
+ # if AR is being used, rescue from common AR errors
7
+ if defined?(::ActiveRecord)
8
+ modified_class.rescue_from ::ActiveRecord::RecordNotFound do |e|
9
+ rack_response(Napa::JsonError.new(:record_not_found, 'record not found').to_json, 404)
10
+ end
11
+ modified_class.rescue_from ::ActiveRecord::RecordInvalid do |e|
12
+ rack_response(Napa::JsonError.new(:record_invalid, 'record not found').to_json, 500)
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
@@ -7,4 +7,4 @@ module Napa
7
7
  # extend all endpoints to include this
8
8
  Grape::Endpoint.send :include, self
9
9
  end
10
- end
10
+ end
@@ -6,12 +6,16 @@ module Napa
6
6
  end
7
7
 
8
8
  def to_json(options = {})
9
+ to_h.to_json(options)
10
+ end
11
+
12
+ def to_h
9
13
  {
10
14
  error: {
11
15
  code: @code,
12
16
  message: @message
13
17
  }
14
- }.to_json
18
+ }
15
19
  end
16
20
  end
17
- end
21
+ end
@@ -0,0 +1,22 @@
1
+ module Napa
2
+ module StatsDTimer
3
+ def report_time(timer_name)
4
+ start_time = Time.now
5
+ yield
6
+ response_time = Time.now - start_time
7
+ Napa::Stats.emitter.timing(timer_name, response_time)
8
+ end
9
+
10
+ module ClassMethods
11
+ def report_time(timer_name)
12
+ new.report_time(timer_name) do
13
+ yield
14
+ end
15
+ end
16
+ end
17
+
18
+ def self.included(base)
19
+ base.extend(ClassMethods)
20
+ end
21
+ end
22
+ end
data/lib/napa/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  module Napa
2
- VERSION = '0.1.21'
2
+ VERSION = '0.1.22'
3
3
 
4
4
  class Version
5
5
  class << self
data/lib/napa.rb CHANGED
@@ -17,13 +17,13 @@ require 'napa/stats'
17
17
  require 'napa/active_record_extensions/filter_by_hash'
18
18
  require 'napa/grape_extensions/error_formatter'
19
19
  require 'napa/grape_extensions/error_presenter'
20
+ require 'napa/grape_extenders'
20
21
  require 'napa/middleware/logger'
21
22
  require 'napa/middleware/app_monitor'
22
23
  require 'napa/middleware/authentication'
23
24
  require 'napa/middleware/request_stats'
24
25
  require 'napa/activerecord'
25
26
  require 'napa/authentication'
26
- require 'napa/grape_api'
27
27
 
28
28
  # load rake tasks if Rake installed
29
29
  if defined?(Rake)
data/lib/tasks/db.rake CHANGED
@@ -47,18 +47,14 @@ unless defined?(Rails)
47
47
  # Define migrations path (needed later)
48
48
  migrations_path = './db/migrate'
49
49
 
50
- # Find the highest existing migration version or set to 1
51
- if (existing_migrations = Dir[File.join(migrations_path, '*.rb')]).length > 0
52
- version = File.basename(existing_migrations.sort.reverse.first)[/^(\d+)_/,1].to_i + 1
53
- else
54
- version = 1
55
- end
50
+ # timestamp the migration
51
+ version = Time.now.utc.to_s.gsub(':','').gsub('-','').gsub('UTC','').gsub(' ','')
56
52
 
57
53
  # Use the migration template to fill the body of the migration
58
54
  migration_content = Napa::ActiveRecord.migration_template(migration_name.camelize)
59
55
 
60
56
  # Generate migration filename
61
- migration_filename = "#{"%03d" % version}_#{migration_name}.rb"
57
+ migration_filename = "#{version}_#{migration_name}.rb"
62
58
 
63
59
  # Write the migration
64
60
  File.open(File.join(migrations_path, migration_filename), "w+") do |migration|
@@ -86,4 +82,4 @@ unless defined?(Rails)
86
82
  end
87
83
  end
88
84
  end
89
- end
85
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: napa
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.21
4
+ version: 0.1.22
5
5
  platform: ruby
6
6
  authors:
7
7
  - Darby Frey
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-02-03 00:00:00.000000000 Z
11
+ date: 2014-02-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake
@@ -261,14 +261,15 @@ files:
261
261
  - lib/napa/generators/templates/api/app/apis/%name_tableize%_api.rb.tt
262
262
  - lib/napa/generators/templates/api/app/entities/%name_underscore%_entity.rb.tt
263
263
  - lib/napa/generators/templates/api/app/models/%name_underscore%.rb.tt
264
- - lib/napa/generators/templates/scaffold/.env
265
- - lib/napa/generators/templates/scaffold/.env.test
264
+ - lib/napa/generators/templates/scaffold/.env.test.tt
265
+ - lib/napa/generators/templates/scaffold/.env.tt
266
266
  - lib/napa/generators/templates/scaffold/.gitignore.tt
267
267
  - lib/napa/generators/templates/scaffold/.rubocop.yml
268
268
  - lib/napa/generators/templates/scaffold/Gemfile
269
269
  - lib/napa/generators/templates/scaffold/README.md
270
270
  - lib/napa/generators/templates/scaffold/Rakefile
271
271
  - lib/napa/generators/templates/scaffold/app.rb
272
+ - lib/napa/generators/templates/scaffold/app/apis/application_api.rb
272
273
  - lib/napa/generators/templates/scaffold/app/apis/hello_api.rb.tt
273
274
  - lib/napa/generators/templates/scaffold/config.ru.tt
274
275
  - lib/napa/generators/templates/scaffold/config/database.yml
@@ -279,7 +280,7 @@ files:
279
280
  - lib/napa/generators/templates/scaffold/spec/apis/hello_api_spec.rb
280
281
  - lib/napa/generators/templates/scaffold/spec/factories/.gitkeep
281
282
  - lib/napa/generators/templates/scaffold/spec/spec_helper.rb
282
- - lib/napa/grape_api.rb
283
+ - lib/napa/grape_extenders.rb
283
284
  - lib/napa/grape_extensions/error_formatter.rb
284
285
  - lib/napa/grape_extensions/error_presenter.rb
285
286
  - lib/napa/identity.rb
@@ -292,6 +293,7 @@ files:
292
293
  - lib/napa/middleware/request_stats.rb
293
294
  - lib/napa/setup.rb
294
295
  - lib/napa/stats.rb
296
+ - lib/napa/stats_d_timer.rb
295
297
  - lib/napa/version.rb
296
298
  - lib/tasks/db.rake
297
299
  - lib/tasks/deploy.rake
@@ -1,6 +0,0 @@
1
- SERVICE_NAME={{app_name}}
2
-
3
- DATABASE_USER=root
4
- DATABASE_PASSWORD=''
5
- DATABASE_HOST=localhost
6
- DATABASE_NAME={{app_name}}_development
@@ -1,9 +0,0 @@
1
- require 'grape'
2
- class Napa::GrapeAPI < Grape::API
3
- def after
4
- @app_response[1]['Cache-Control'] = 'no-cache, no-store, max-age=0, must-revalidate'
5
- @app_response[1]['Pragma'] = 'no-cache'
6
- @app_response[1]['Expires'] = 'Fri, 01 Jan 1990 00:00:00 GMT'
7
- @app_response
8
- end
9
- end