napa_rails 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: baa8fd351d63e02420301af383a7447ad0d6a9f5
4
+ data.tar.gz: 8e0e6d2d3bc336dd5e6ac205354731b21b52ed73
5
+ SHA512:
6
+ metadata.gz: 924b4e91038d3ccdd8eb7cc8f52c92a2fe519c6499840afb474c70731fc4f381e1e77c26799cdecf8d48d73d234010bdcb56c98c97997cb22760e87a880801b9
7
+ data.tar.gz: 72fd1c9a3f6ff8e5b111187ddbb3dbc4eb3f9a242e238a89af3bd2709f8613c370f45d8ed016d5795c7280ab923cebe34c999dee675e16743146515055b5b870
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.0.0
4
+ before_install: gem install bundler -v 1.11.2
data/CHANGELOG.md ADDED
@@ -0,0 +1,10 @@
1
+ master
2
+ ===
3
+
4
+ 0.1.0
5
+ ===
6
+ * Add grape_extensions folder with error_formatter and grape_helpers
7
+ * Add output_formatters folder with include_nil, pagination, representer
8
+ * Add rspec_extensions with response_helpers
9
+ * Add json_error
10
+ * Add kaminari, grape and roar dependency
@@ -0,0 +1,49 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, and in the interest of
4
+ fostering an open and welcoming community, we pledge to respect all people who
5
+ contribute through reporting issues, posting feature requests, updating
6
+ documentation, submitting pull requests or patches, and other activities.
7
+
8
+ We are committed to making participation in this project a harassment-free
9
+ experience for everyone, regardless of level of experience, gender, gender
10
+ identity and expression, sexual orientation, disability, personal appearance,
11
+ body size, race, ethnicity, age, religion, or nationality.
12
+
13
+ Examples of unacceptable behavior by participants include:
14
+
15
+ * The use of sexualized language or imagery
16
+ * Personal attacks
17
+ * Trolling or insulting/derogatory comments
18
+ * Public or private harassment
19
+ * Publishing other's private information, such as physical or electronic
20
+ addresses, without explicit permission
21
+ * Other unethical or unprofessional conduct
22
+
23
+ Project maintainers have the right and responsibility to remove, edit, or
24
+ reject comments, commits, code, wiki edits, issues, and other contributions
25
+ that are not aligned to this Code of Conduct, or to ban temporarily or
26
+ permanently any contributor for other behaviors that they deem inappropriate,
27
+ threatening, offensive, or harmful.
28
+
29
+ By adopting this Code of Conduct, project maintainers commit themselves to
30
+ fairly and consistently applying these principles to every aspect of managing
31
+ this project. Project maintainers who do not follow or enforce the Code of
32
+ Conduct may be permanently removed from the project team.
33
+
34
+ This code of conduct applies both within project spaces and in public spaces
35
+ when an individual is representing the project or its community.
36
+
37
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
38
+ reported by contacting a project maintainer at TODO: Write your email address. All
39
+ complaints will be reviewed and investigated and will result in a response that
40
+ is deemed necessary and appropriate to the circumstances. Maintainers are
41
+ obligated to maintain confidentiality with regard to the reporter of an
42
+ incident.
43
+
44
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage],
45
+ version 1.3.0, available at
46
+ [http://contributor-covenant.org/version/1/3/0/][version]
47
+
48
+ [homepage]: http://contributor-covenant.org
49
+ [version]: http://contributor-covenant.org/version/1/3/0/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in napa_rails.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 TODO: Write your name
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,41 @@
1
+ # NapaRails
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/napa_rails`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'napa_rails'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install napa_rails
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
31
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/napa_rails. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
36
+
37
+
38
+ ## License
39
+
40
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
41
+
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
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "napa_rails"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,18 @@
1
+ if defined?(Grape)
2
+ module Grape
3
+ module ErrorFormatter
4
+ module Json
5
+ class << self
6
+ def call(message, backtrace, options = {}, _env = nil)
7
+ result = message.is_a?(Napa::JsonError) ? message : Napa::JsonError.new(:api_error, message)
8
+
9
+ if (options[:rescue_options] || {})[:backtrace] && backtrace && !backtrace.empty?
10
+ result = result.to_h.merge(backtrace: backtrace)
11
+ end
12
+ MultiJson.dump(result)
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,65 @@
1
+ module Napa
2
+ module GrapeHelpers
3
+ def represent(data, with: nil, **args)
4
+ fail ArgumentError, ':with option is required' if with.nil?
5
+
6
+ if data.respond_to?(:map)
7
+ return { data: data.map { |item| with.new(item).to_hash(args) } }
8
+ else
9
+ return { data: with.new(data).to_hash(args) }
10
+ end
11
+ end
12
+
13
+ def present_error(code, message = '', reasons = {})
14
+ Napa::JsonError.new(code, message, reasons)
15
+ end
16
+
17
+ def permitted_params(options = {})
18
+ options = { include_missing: false }.merge(options)
19
+ declared(params, options)
20
+ end
21
+
22
+ def paginate(data, with: nil, **args)
23
+ raise ArgumentError.new(":with option is required") if with.nil?
24
+
25
+ if data.respond_to?(:to_a)
26
+ return {}.tap do |r|
27
+ data = Napa::Pagination.new(represent_pagination(data))
28
+ r[:data] = data.map{ |item| with.new(item).to_hash(args) }
29
+ r[:pagination] = data.to_h
30
+ end
31
+ else
32
+ return { data: with.new(data).to_hash(args) }
33
+ end
34
+ end
35
+
36
+ def represent_pagination(data)
37
+ # don't paginate if collection is already paginated
38
+ return data if data.respond_to?(:total_count)
39
+
40
+ page = params.try(:page) || 1
41
+ per_page = params.try(:per_page) || 25
42
+
43
+ order_by_params!(data) if data.is_a?(ActiveRecord::Relation) && data.size > 0
44
+
45
+ if data.is_a?(Array)
46
+ Kaminari.paginate_array(data).page(page).per(per_page)
47
+ else
48
+ data.page(page).per(per_page)
49
+ end
50
+ end
51
+
52
+ def order_by_params!(data)
53
+ if params[:sort_by] && data.column_names.map(&:to_sym).include?(params[:sort_by].to_sym)
54
+ sort_order = params[:sort_order] || :asc
55
+ data.order!(params[:sort_by] => sort_order.to_sym)
56
+ end
57
+ data
58
+ end
59
+
60
+ # extend all endpoints to include this
61
+ Grape::Endpoint.send :include, self if defined?(Grape)
62
+ # rails 4 controller concern
63
+ extend ActiveSupport::Concern if defined?(Rails)
64
+ end
65
+ end
@@ -0,0 +1,24 @@
1
+ module Napa
2
+ class JsonError
3
+ def initialize(code, message, reasons = {})
4
+ @code = code
5
+ @message = message
6
+ @reasons = reasons
7
+ end
8
+
9
+ def to_json(options = {})
10
+ to_h.to_json(options)
11
+ end
12
+
13
+ def to_h
14
+ e = {
15
+ error: {
16
+ code: @code,
17
+ message: @message
18
+ }
19
+ }
20
+ e[:error][:reasons] = @reasons if @reasons.present?
21
+ e
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,16 @@
1
+ # include this in your representer, and you will always return all defined keys (even if their value is nil)
2
+ module Napa
3
+ module Representable
4
+ module IncludeNil
5
+ def self.included(base)
6
+ base.extend ClassMethods
7
+ end
8
+
9
+ module ClassMethods
10
+ def property(name, options = {}, &block)
11
+ super(name, options.merge(render_nil: true), &block)
12
+ end
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,27 @@
1
+ module Napa
2
+ class Pagination
3
+ include Enumerable
4
+ def initialize(object)
5
+ @object = object
6
+ end
7
+
8
+ def to_json(options = {})
9
+ to_h.to_json(options)
10
+ end
11
+
12
+ def to_h
13
+ {}.tap do |p|
14
+ p[:page] = @object.current_page if @object.respond_to?(:current_page)
15
+ p[:per_page] = @object.limit_value if @object.respond_to?(:limit_value)
16
+ p[:total_pages] = @object.total_pages if @object.respond_to?(:total_pages)
17
+ p[:total_count] = @object.total_count if @object.respond_to?(:total_count)
18
+ end
19
+ end
20
+
21
+ def each(&block)
22
+ @object.each do |member|
23
+ block.call(member)
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,24 @@
1
+ require 'roar/decorator'
2
+ require 'roar/version'
3
+
4
+ if Roar::VERSION >= '1.0.0'
5
+ require 'roar/json'
6
+ require 'roar/coercion'
7
+ else
8
+ require 'roar/representer/json'
9
+ require 'roar/representer/feature/coercion'
10
+ end
11
+
12
+ module Napa
13
+ class Representer < Roar::Decorator
14
+ include Coercion
15
+
16
+ if Roar::VERSION >= '1.0.0'
17
+ include Roar::JSON
18
+ else
19
+ include Roar::Representer::JSON
20
+ end
21
+
22
+ property :object_type, getter: ->(*) { self.class.name.underscore }
23
+ end
24
+ end
@@ -0,0 +1,50 @@
1
+ module Napa
2
+ module RspecExtensions
3
+ module ResponseHelpers
4
+ def parsed_response
5
+ Hashie::Mash.new(JSON.parse(last_response.body))
6
+ end
7
+
8
+ def response_code
9
+ last_response.status
10
+ end
11
+
12
+ def response_body
13
+ last_response.body
14
+ end
15
+
16
+ def result_count
17
+ parsed_response.data.count
18
+ end
19
+
20
+ def first_result
21
+ parsed_response.data.first
22
+ end
23
+
24
+ def result_with_id(id)
25
+ parsed_response.data.select { |r| r.id == id }.first
26
+ end
27
+
28
+ def expect_count_of(count)
29
+ expect(result_count).to eq(count)
30
+ end
31
+
32
+ def expect_error_code(error_code)
33
+ expect(parsed_response.error.code).to eq(error_code.to_s)
34
+ end
35
+
36
+ def expect_only(object)
37
+ expect_count_of 1
38
+ expect(first_result.id).to eq(object.id)
39
+ end
40
+
41
+ def expect_to_have(object)
42
+ expect(!result_with_id(object.id).nil?).to be_truthy
43
+ end
44
+
45
+ def expect_to_not_have(object)
46
+ expect(!result_with_id(object.id).nil?).to be_falsy
47
+ end
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,3 @@
1
+ module Napa
2
+ VERSION = "0.1.0"
3
+ end
data/lib/napa.rb ADDED
@@ -0,0 +1,13 @@
1
+ require "grape"
2
+ require "kaminari"
3
+ require "napa/version"
4
+ require "napa/rspec_extensions/response_helpers"
5
+ require "napa/output_formatters/include_nil"
6
+ require "napa/output_formatters/representer"
7
+ require "napa/output_formatters/pagination"
8
+ require "napa/grape_extensions/grape_helpers"
9
+ require "napa/grape_extensions/error_formatter"
10
+ require "napa/json_error"
11
+
12
+ module Napa
13
+ end
@@ -0,0 +1,36 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'napa/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "napa_rails"
8
+ spec.version = Napa::VERSION
9
+ spec.authors = ["Darby Frey, Flori Garcia"]
10
+ spec.email = ["darby@bellycard.com, flori@bellycard.com"]
11
+
12
+ spec.summary = %q{Napa features for Rails}
13
+ spec.description = %q{Napa features for Rails}
14
+ spec.homepage = "https://github.com/bellycard/napa_rails"
15
+ spec.license = "MIT"
16
+
17
+ # Prevent pushing this gem to RubyGems.org by setting 'allowed_push_host', or
18
+ # delete this section to allow pushing this gem to any host.
19
+ if spec.respond_to?(:metadata)
20
+ spec.metadata['allowed_push_host'] = "TODO: Set to 'http://mygemserver.com'"
21
+ else
22
+ raise "RubyGems 2.0 or newer is required to protect against public gem pushes."
23
+ end
24
+
25
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
26
+ spec.bindir = "exe"
27
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
28
+ spec.require_paths = ["lib"]
29
+ spec.add_dependency "grape"
30
+ spec.add_dependency "kaminari"
31
+ spec.add_dependency 'roar', ['>= 0.12.0', '< 2.0']
32
+
33
+ spec.add_development_dependency "bundler", "~> 1.11"
34
+ spec.add_development_dependency "rake", "~> 10.0"
35
+ spec.add_development_dependency "rspec", "~> 3.0"
36
+ end
metadata ADDED
@@ -0,0 +1,156 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: napa_rails
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Darby Frey, Flori Garcia
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-04-12 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: kaminari
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: roar
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: 0.12.0
48
+ - - <
49
+ - !ruby/object:Gem::Version
50
+ version: '2.0'
51
+ type: :runtime
52
+ prerelease: false
53
+ version_requirements: !ruby/object:Gem::Requirement
54
+ requirements:
55
+ - - '>='
56
+ - !ruby/object:Gem::Version
57
+ version: 0.12.0
58
+ - - <
59
+ - !ruby/object:Gem::Version
60
+ version: '2.0'
61
+ - !ruby/object:Gem::Dependency
62
+ name: bundler
63
+ requirement: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - ~>
66
+ - !ruby/object:Gem::Version
67
+ version: '1.11'
68
+ type: :development
69
+ prerelease: false
70
+ version_requirements: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - ~>
73
+ - !ruby/object:Gem::Version
74
+ version: '1.11'
75
+ - !ruby/object:Gem::Dependency
76
+ name: rake
77
+ requirement: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - ~>
80
+ - !ruby/object:Gem::Version
81
+ version: '10.0'
82
+ type: :development
83
+ prerelease: false
84
+ version_requirements: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - ~>
87
+ - !ruby/object:Gem::Version
88
+ version: '10.0'
89
+ - !ruby/object:Gem::Dependency
90
+ name: rspec
91
+ requirement: !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - - ~>
94
+ - !ruby/object:Gem::Version
95
+ version: '3.0'
96
+ type: :development
97
+ prerelease: false
98
+ version_requirements: !ruby/object:Gem::Requirement
99
+ requirements:
100
+ - - ~>
101
+ - !ruby/object:Gem::Version
102
+ version: '3.0'
103
+ description: Napa features for Rails
104
+ email:
105
+ - darby@bellycard.com, flori@bellycard.com
106
+ executables: []
107
+ extensions: []
108
+ extra_rdoc_files: []
109
+ files:
110
+ - .gitignore
111
+ - .rspec
112
+ - .travis.yml
113
+ - CHANGELOG.md
114
+ - CODE_OF_CONDUCT.md
115
+ - Gemfile
116
+ - LICENSE.txt
117
+ - README.md
118
+ - Rakefile
119
+ - bin/console
120
+ - bin/setup
121
+ - lib/napa.rb
122
+ - lib/napa/grape_extensions/error_formatter.rb
123
+ - lib/napa/grape_extensions/grape_helpers.rb
124
+ - lib/napa/json_error.rb
125
+ - lib/napa/output_formatters/include_nil.rb
126
+ - lib/napa/output_formatters/pagination.rb
127
+ - lib/napa/output_formatters/representer.rb
128
+ - lib/napa/rspec_extensions/response_helpers.rb
129
+ - lib/napa/version.rb
130
+ - napa_rails.gemspec
131
+ homepage: https://github.com/bellycard/napa_rails
132
+ licenses:
133
+ - MIT
134
+ metadata:
135
+ allowed_push_host: 'TODO: Set to ''http://mygemserver.com'''
136
+ post_install_message:
137
+ rdoc_options: []
138
+ require_paths:
139
+ - lib
140
+ required_ruby_version: !ruby/object:Gem::Requirement
141
+ requirements:
142
+ - - '>='
143
+ - !ruby/object:Gem::Version
144
+ version: '0'
145
+ required_rubygems_version: !ruby/object:Gem::Requirement
146
+ requirements:
147
+ - - '>='
148
+ - !ruby/object:Gem::Version
149
+ version: '0'
150
+ requirements: []
151
+ rubyforge_project:
152
+ rubygems_version: 2.0.14
153
+ signing_key:
154
+ specification_version: 4
155
+ summary: Napa features for Rails
156
+ test_files: []