api_monkey 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: 14630a35b125ccf8d2107cb54a0d4be4f16d8031
4
- data.tar.gz: e7fd94d3f82f6c99b4eba34f769422f1cba993e4
3
+ metadata.gz: ea6c7219d0438e4edfc69a15a33fd27180841b19
4
+ data.tar.gz: c43568afe1cb1d6681fbe6993cb905582d2082e5
5
5
  SHA512:
6
- metadata.gz: db3fdc5d3e2a09e81a6fcafd0bfdbfa70e9fa884a728b3709c0a3ef96320c63e26f6e29edace93dd46b9a432eddfcf790d44adbb6d48c2e27f5256b1ce5271be
7
- data.tar.gz: 018ded42e99d237e7ff8cc40b239c7849f3dd28eae26cf88d84ab43caf051b161de50e58bdeaa1c32d7cc477554fd647938167462b2105214b315afd228541ab
6
+ metadata.gz: 602f83bd78cb22bbfc54c9bf45b46a6b3d0545714c5732a7f9081f43625ff46e1b7597255e8f4cd3d99d27548b96f648886e2cfe69a02ef498f9b7ffb771076b
7
+ data.tar.gz: cdeb5c1fa6720b152b0f2f9c9dbb440109c32a6b95c696b4c3b63e19c8774461ffd8245f77f050cd0b0970c8f721de3105b0e6e0585c0337de0131eda595d189
data/README.md CHANGED
@@ -27,24 +27,101 @@ end
27
27
 
28
28
  ### Filter Params
29
29
  Filter params provide a clean and easy way to add request-based
30
- filtering of your active record models:
30
+ filtering of your active record models. To use filter params, ApiMonkey
31
+ expects the parameters send to rails to resolve like this:
32
+
33
+ ```ruby
34
+ # http://my.domain/products?filter[price]=200
35
+
36
+ {
37
+ filter: {
38
+ price: 200
39
+ }
40
+ }
41
+ ```
42
+ This would filter our Product query to only those with `price = 200`
43
+
44
+ More complex filters can be applied using an operator shorthand that
45
+ allows for equality and comparison operators. The list of possible
46
+ operators are:
47
+
48
+ |SQL Operator|Operator Shorthand|
49
+ |---|---|
50
+ |=|eq|
51
+ |>|gt|
52
+ |<|lt|
53
+ |<=|leq|
54
+ |>=|geq|
55
+
56
+ To use these, just change the concrete value for `price` in the above
57
+ hash to it's own hash using the operators as keys:
58
+
59
+ ```ruby
60
+ # http://my.domain/products?filter[price[gt]]=200
61
+
62
+ {
63
+ filter: {
64
+ price: {
65
+ 'gt' => 200
66
+ }
67
+ }
68
+ }
69
+ ```
70
+
71
+ Or:
72
+
73
+ ```ruby
74
+ # http://my.domain/products?filter[price[gt]]=200&filter[price[leq]]=350
75
+
76
+ {
77
+ filter: {
78
+ price: {
79
+ 'gt' => 200,
80
+ 'leq' => 350
81
+ }
82
+ }
83
+ }
84
+ ```
85
+
86
+ To use ApiMonkey in an exisitng Rails app, you have to include the ApiMonkey module in your model like this:
87
+
88
+ ```ruby
89
+ class Product < ActiveRecord::Base
90
+ include ApiMonkey
91
+ end
92
+ ```
93
+
94
+ This will create all the necessary filtering methods that will be used
95
+ in your controller. Next we want to add the filter to our controller. Use a methodology similar to
96
+ strong parameters. We recommend using a controller method like this:
31
97
 
32
98
  ```ruby
33
99
  # app/controllers/products_controller.rb
34
100
 
35
101
  class ProductsController < ApplicationController
36
102
  def index
37
- @products = Product.filter(filter_params)
103
+ @products = if params[:filter]
104
+ Product.filter(filter_params)
105
+ else
106
+ Product.all
38
107
  end
39
108
 
40
109
  protected
41
110
 
42
111
  def filter_params
43
- params.slice(:active, :price, :code)
112
+ params.require(:filter).permit(Product.filter_params)
44
113
  end
45
114
  end
46
115
  ```
47
116
 
117
+ `Model.filter_params` is a simple hash so it supports methods like
118
+ `include` and `except` to help control the fields that can be filtered
119
+ against.
120
+ `filter` is designed so that passing either `nil` or `{}` to it will
121
+ produce an empty set (`where(nil)`). Therefore, you should ensure that
122
+ filter exists as a key in `params`. Passing `nil` to one of the
123
+ `filter_xxx` will result in a `where(xxx: nil)`.
124
+
48
125
  ## Development
49
126
 
50
127
  After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake rspec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
@@ -10,7 +10,7 @@ Gem::Specification.new do |spec|
10
10
  spec.email = ["michaelkelly322@gmail.com"]
11
11
 
12
12
  spec.summary = "Makes building data-driven APIs in Rails a breeze"
13
- spec.description = "Makes building data-driven APIs in Rails a breeze by providing an easy to use DSL for declaring API resources"
13
+ spec.description = "Makes building data-driven APIs in Rails a breeze by providing an easy to use DSL for working with API resources"
14
14
  spec.homepage = "https://github.com/michaelkelly322/api_monkey"
15
15
  spec.license = "MIT"
16
16
 
@@ -22,4 +22,6 @@ Gem::Specification.new do |spec|
22
22
  spec.add_development_dependency "bundler", "~> 1.10"
23
23
  spec.add_development_dependency "rake", "~> 10.0"
24
24
  spec.add_development_dependency "rspec"
25
+
26
+ spec.add_runtime_dependency 'activesupport'
25
27
  end
@@ -1,3 +1,5 @@
1
+ require 'active_support/concern'
2
+
1
3
  require "api_monkey/version"
2
4
  require "api_monkey/filterable"
3
5
  require "api_monkey/filter_scopes"
@@ -2,9 +2,44 @@ module ApiMonkey::FilterScopes
2
2
  extend ActiveSupport::Concern
3
3
 
4
4
  included do
5
+ OPERANDS = {
6
+ 'eq' => '=',
7
+ 'gt' => '>',
8
+ 'lt' => '<',
9
+ 'geq' => '>=',
10
+ 'leq' => '<=',
11
+ }.freeze
12
+
13
+ # Define filter methods
5
14
  column_names.map do |field_name|
6
15
  define_singleton_method "filter_#{field_name}" do |param|
7
- where("#{field_name} = ?", param)
16
+ if param.is_a? Hash
17
+ where(*process_hash_params(field_name, param))
18
+ else
19
+ where(*filter_args(field_name, param, 'eq'))
20
+ end
21
+ end
22
+ end
23
+
24
+ def self.filter_args(field, value, op)
25
+ ["#{field} #{OPERANDS[op]} ?", value]
26
+ end
27
+
28
+ def self.process_hash_params(field, param)
29
+ predicates, values = [],[]
30
+ param.keys.each do |k|
31
+ predicates << filter_args(field, param[k], k)[0]
32
+ values << filter_args(field, param[k], k)[1]
33
+ end
34
+
35
+ [predicates.join(' AND '), *values]
36
+ end
37
+
38
+ def self.filter_params
39
+ {}.tap do |h|
40
+ column_names.each do |field_name|
41
+ h[field_name.to_sym] = OPERANDS.keys.map &:to_sym
42
+ end
8
43
  end
9
44
  end
10
45
  end
@@ -4,6 +4,7 @@ module ApiMonkey::Filterable
4
4
  included do
5
5
  define_singleton_method :filter do |filtering_params|
6
6
  results = self.where(nil)
7
+ return results unless filtering_params
7
8
  filtering_params.each do |key, value|
8
9
  results = results.public_send("filter_#{key}", value) if value.present?
9
10
  end
@@ -1,3 +1,3 @@
1
1
  module ApiMonkey
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: api_monkey
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
  - Michael Kelly
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2015-07-23 00:00:00.000000000 Z
11
+ date: 2016-02-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -52,8 +52,22 @@ dependencies:
52
52
  - - ">="
53
53
  - !ruby/object:Gem::Version
54
54
  version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: activesupport
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
55
69
  description: Makes building data-driven APIs in Rails a breeze by providing an easy
56
- to use DSL for declaring API resources
70
+ to use DSL for working with API resources
57
71
  email:
58
72
  - michaelkelly322@gmail.com
59
73
  executables: []
@@ -96,8 +110,9 @@ required_rubygems_version: !ruby/object:Gem::Requirement
96
110
  version: '0'
97
111
  requirements: []
98
112
  rubyforge_project:
99
- rubygems_version: 2.4.6
113
+ rubygems_version: 2.4.8
100
114
  signing_key:
101
115
  specification_version: 4
102
116
  summary: Makes building data-driven APIs in Rails a breeze
103
117
  test_files: []
118
+ has_rdoc: