rails_api_model 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.gitignore +9 -0
- data/.rspec +2 -0
- data/.travis.yml +4 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +41 -0
- data/Rakefile +19 -0
- data/bin/setup +7 -0
- data/lib/rails_api_model.rb +19 -0
- data/lib/rails_api_model/base.rb +11 -0
- data/lib/rails_api_model/builders/filters/active_record_model.rb +19 -0
- data/lib/rails_api_model/builders/filters/allow_fields.rb +19 -0
- data/lib/rails_api_model/error.rb +4 -0
- data/lib/rails_api_model/filter_error.rb +4 -0
- data/lib/rails_api_model/filters.rb +17 -0
- data/lib/rails_api_model/filters/base_filter.rb +27 -0
- data/lib/rails_api_model/filters/field.rb +55 -0
- data/lib/rails_api_model/version.rb +3 -0
- data/rails_api_model.gemspec +28 -0
- metadata +147 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: c67cac2d784871eb3d45ca5f59ea515d1b10c99c
|
4
|
+
data.tar.gz: 64b9ba8fe2f15ccd1c493330fa894c926dcd6c8c
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 5566017449c8af453b7105cf1b93c63bf4785a31794c50fb8db6afbe0552d60e1124cb08dc1df277ba95b2b149534b663c337681987cb400b2a9be1b3f9818ff
|
7
|
+
data.tar.gz: c3f68e911ca3ca26035ac95e391f9d2566ad165d3508f014fc55c4c486cfd230ae1b4a12d85671712ee6d38d230cd85de3610314ca85dc4d23e9a50408f0b726
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2015 Georgy Angelov
|
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
|
+
# RailsApiModel
|
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/rails_api_model`. 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 'rails_api_model'
|
13
|
+
```
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install rails_api_model
|
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]/rails_api_model.
|
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,19 @@
|
|
1
|
+
require 'bundler/gem_tasks'
|
2
|
+
require 'rspec/core/rake_task'
|
3
|
+
|
4
|
+
RSpec::Core::RakeTask.new(:spec)
|
5
|
+
|
6
|
+
task :default => :spec
|
7
|
+
|
8
|
+
task :environment do
|
9
|
+
$LOAD_PATH.unshift File.expand_path('../lib', __FILE__)
|
10
|
+
require 'rails_api_model'
|
11
|
+
|
12
|
+
include RailsApiModel
|
13
|
+
end
|
14
|
+
|
15
|
+
task console: :environment do
|
16
|
+
require 'pry'
|
17
|
+
|
18
|
+
Pry.start
|
19
|
+
end
|
data/bin/setup
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
require 'active_support/concern'
|
2
|
+
require 'active_support/core_ext/class/attribute'
|
3
|
+
require 'active_support/core_ext/array/wrap'
|
4
|
+
require 'active_support/time_with_zone'
|
5
|
+
require 'active_support/lazy_load_hooks'
|
6
|
+
|
7
|
+
require 'active_record'
|
8
|
+
|
9
|
+
require 'rails_api_model/version'
|
10
|
+
require 'rails_api_model/error'
|
11
|
+
require 'rails_api_model/filter_error'
|
12
|
+
require 'rails_api_model/filters'
|
13
|
+
require 'rails_api_model/builders/filters/active_record_model'
|
14
|
+
require 'rails_api_model/builders/filters/allow_fields'
|
15
|
+
require 'rails_api_model/base'
|
16
|
+
|
17
|
+
module RailsApiModel
|
18
|
+
|
19
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
module RailsApiModel
|
2
|
+
module Builders
|
3
|
+
module Filters
|
4
|
+
module ActiveRecordModel
|
5
|
+
extend ActiveSupport::Concern
|
6
|
+
|
7
|
+
module ClassMethods
|
8
|
+
def active_record_model(model)
|
9
|
+
unless model < ActiveRecord::Base
|
10
|
+
raise ArgumentError, 'The specified class is not an ActiveRecord model'
|
11
|
+
end
|
12
|
+
|
13
|
+
self.ar_model = model
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
module RailsApiModel
|
2
|
+
module Builders
|
3
|
+
module Filters
|
4
|
+
module AllowFields
|
5
|
+
extend ActiveSupport::Concern
|
6
|
+
|
7
|
+
module ClassMethods
|
8
|
+
def allow_fields(*field_names)
|
9
|
+
raise ArgumentError, 'You need to specify at least one field name' if field_names.empty?
|
10
|
+
|
11
|
+
filters = field_names.map { |field_name| ::Filters::Field.new(self, field_name) }
|
12
|
+
|
13
|
+
::Filters.add_filters(self, filters)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
require 'rails_api_model/filters/base_filter'
|
2
|
+
require 'rails_api_model/filters/field'
|
3
|
+
|
4
|
+
module RailsApiModel
|
5
|
+
module Filters
|
6
|
+
extend ActiveSupport::Concern
|
7
|
+
|
8
|
+
included do
|
9
|
+
class_attribute :filters
|
10
|
+
self.filters = []
|
11
|
+
end
|
12
|
+
|
13
|
+
def self.add_filters(model, filters)
|
14
|
+
model.filters += Array.wrap(filters)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
module RailsApiModel
|
2
|
+
module Filters
|
3
|
+
class BaseFilter
|
4
|
+
attr_reader :api_model, :filter_key
|
5
|
+
|
6
|
+
def initialize(api_model, filter_key)
|
7
|
+
@api_model = api_model
|
8
|
+
@filter_key = filter_key
|
9
|
+
end
|
10
|
+
|
11
|
+
def apply_scope(relation, key, value)
|
12
|
+
raise 'Not implemented in the base class'
|
13
|
+
end
|
14
|
+
|
15
|
+
def ==(other)
|
16
|
+
api_model == other.api_model &&
|
17
|
+
filter_key == other.filter_key
|
18
|
+
end
|
19
|
+
|
20
|
+
alias_method :eql?, :==
|
21
|
+
|
22
|
+
def ar_model
|
23
|
+
api_model.ar_model
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,55 @@
|
|
1
|
+
module RailsApiModel
|
2
|
+
module Filters
|
3
|
+
# TODO: Check whether the ActiveRecord model has the specified field
|
4
|
+
class Field < BaseFilter
|
5
|
+
def apply_scope(relation, key, value)
|
6
|
+
key, modifier = key.split(':'.freeze, 2)
|
7
|
+
value = transform_type(value)
|
8
|
+
column = activerecord_model.arel_table[key]
|
9
|
+
|
10
|
+
relation.where case modifier
|
11
|
+
when nil then column.eq(value)
|
12
|
+
when 'not'.freeze then column.not_eq(value)
|
13
|
+
when 'lt'.freeze then column.lt(value)
|
14
|
+
when 'lte'.freeze then column.lteq(value)
|
15
|
+
when 'gt'.freeze then column.gt(value)
|
16
|
+
when 'gte'.freeze then column.gteq(value)
|
17
|
+
# TODO: Handle this error via Rails and send 400
|
18
|
+
else raise FilterError.new("Invalid modifier #{modifier}")
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
private
|
23
|
+
|
24
|
+
def transform_type(value)
|
25
|
+
column_type = activerecord_model.columns_hash[filter_key].type
|
26
|
+
|
27
|
+
# TODO: Verify all of these types actually exist
|
28
|
+
# TODO: Support range queries with some syntax.
|
29
|
+
# Maybe `start_time:gt=2015-11-03&start_time:lt=2015-12-19`
|
30
|
+
# TODO: Support enum values as strings
|
31
|
+
# TODO: Support `LIKE` queries?
|
32
|
+
case column_type
|
33
|
+
when :string, :text then value
|
34
|
+
# TODO: Add validation and raise error instead of silently discarding non-numbers
|
35
|
+
when :primary_key, :integer then value.to_i
|
36
|
+
when :float, :decimal then value.to_f
|
37
|
+
# TODO: Smarter date parsing. Passing the date `2015-11-03` should actually
|
38
|
+
# make a range query `column >= 2015-11-03 and column < 2015-11-04`.
|
39
|
+
# TODO: Catch ArgumentError for invalid dates and reraise as Rails error
|
40
|
+
when :datetime then Time.zone.parse(value)
|
41
|
+
when :boolean then to_boolean(value)
|
42
|
+
else raise "Unsupported filter on column type #{column_type}"
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
def to_boolean(value)
|
47
|
+
case value
|
48
|
+
# `true` is supported for arguments passed during testing
|
49
|
+
when true, 'true'.freeze, 't'.freeze, '1'.freeze then true
|
50
|
+
else false
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
lib = File.expand_path('../lib', __FILE__)
|
2
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
3
|
+
|
4
|
+
require 'rails_api_model/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = 'rails_api_model'
|
8
|
+
spec.version = RailsApiModel::VERSION
|
9
|
+
spec.authors = ['Georgy Angelov']
|
10
|
+
spec.email = ['georgyangelov@gmail.com']
|
11
|
+
|
12
|
+
spec.summary = 'A better way to do RESTful APIs in Rails'
|
13
|
+
spec.homepage = 'https://github.com/stormbreakerbg/rails_api_model'
|
14
|
+
spec.license = 'MIT'
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
17
|
+
spec.bindir = 'exe'
|
18
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
19
|
+
spec.require_paths = ['lib']
|
20
|
+
|
21
|
+
spec.add_development_dependency 'bundler', '~> 1.10'
|
22
|
+
spec.add_development_dependency 'rake', '~> 10.0'
|
23
|
+
spec.add_development_dependency 'rspec'
|
24
|
+
spec.add_development_dependency 'pry'
|
25
|
+
|
26
|
+
spec.add_dependency 'activerecord'
|
27
|
+
spec.add_dependency 'activesupport'
|
28
|
+
end
|
metadata
ADDED
@@ -0,0 +1,147 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: rails_api_model
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Georgy Angelov
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-12-15 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.10'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.10'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
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: pry
|
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
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: activerecord
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :runtime
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: activesupport
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
type: :runtime
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - ">="
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0'
|
97
|
+
description:
|
98
|
+
email:
|
99
|
+
- georgyangelov@gmail.com
|
100
|
+
executables: []
|
101
|
+
extensions: []
|
102
|
+
extra_rdoc_files: []
|
103
|
+
files:
|
104
|
+
- ".gitignore"
|
105
|
+
- ".rspec"
|
106
|
+
- ".travis.yml"
|
107
|
+
- Gemfile
|
108
|
+
- LICENSE.txt
|
109
|
+
- README.md
|
110
|
+
- Rakefile
|
111
|
+
- bin/setup
|
112
|
+
- lib/rails_api_model.rb
|
113
|
+
- lib/rails_api_model/base.rb
|
114
|
+
- lib/rails_api_model/builders/filters/active_record_model.rb
|
115
|
+
- lib/rails_api_model/builders/filters/allow_fields.rb
|
116
|
+
- lib/rails_api_model/error.rb
|
117
|
+
- lib/rails_api_model/filter_error.rb
|
118
|
+
- lib/rails_api_model/filters.rb
|
119
|
+
- lib/rails_api_model/filters/base_filter.rb
|
120
|
+
- lib/rails_api_model/filters/field.rb
|
121
|
+
- lib/rails_api_model/version.rb
|
122
|
+
- rails_api_model.gemspec
|
123
|
+
homepage: https://github.com/stormbreakerbg/rails_api_model
|
124
|
+
licenses:
|
125
|
+
- MIT
|
126
|
+
metadata: {}
|
127
|
+
post_install_message:
|
128
|
+
rdoc_options: []
|
129
|
+
require_paths:
|
130
|
+
- lib
|
131
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
132
|
+
requirements:
|
133
|
+
- - ">="
|
134
|
+
- !ruby/object:Gem::Version
|
135
|
+
version: '0'
|
136
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
137
|
+
requirements:
|
138
|
+
- - ">="
|
139
|
+
- !ruby/object:Gem::Version
|
140
|
+
version: '0'
|
141
|
+
requirements: []
|
142
|
+
rubyforge_project:
|
143
|
+
rubygems_version: 2.4.5.1
|
144
|
+
signing_key:
|
145
|
+
specification_version: 4
|
146
|
+
summary: A better way to do RESTful APIs in Rails
|
147
|
+
test_files: []
|