baseapi 0.1.0

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 8d4dadca27b1dfff122e81fd28377fa34029d2a3
4
+ data.tar.gz: 52bd8d536bf6f31864fffd0bf55c10110e9bf222
5
+ SHA512:
6
+ metadata.gz: 360017d2ffb93bb810c750fbfdb3964c86463f82c28ce314569d3d86ef39c7bef49a3ae7030e0b8f9716382afd404aabaa220df04e85bc3d8971699fe3ae3758
7
+ data.tar.gz: 504d7bdde8c48a28314e0792db3279c37fea2e0bf25b5421dcc88955934ed7c4c92380748cec0fe222b58b4304ca7578b37b64774899994e54bc4790b3ebf946
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,3 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.0.0
@@ -0,0 +1,13 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
4
+
5
+ We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, age, or religion.
6
+
7
+ Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
8
+
9
+ Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
10
+
11
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
12
+
13
+ This Code of Conduct is adapted from the [Contributor Covenant](http:contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in baseapi.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 新川 盛幸
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,39 @@
1
+ # Baseapi
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/baseapi`. 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 'baseapi'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install baseapi
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 `bin/console` for an interactive prompt that will allow you to experiment. Run `bundle exec baseapi` to use the code located in this directory, ignoring other installed copies of this gem.
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` to 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
+ 1. Fork it ( https://github.com/[my-github-username]/baseapi/fork )
36
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
37
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
38
+ 4. Push to the branch (`git push origin my-new-feature`)
39
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
data/baseapi.gemspec ADDED
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'baseapi/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "baseapi"
8
+ spec.version = Baseapi::VERSION
9
+ spec.authors = ["Moriyuki Arakawa"]
10
+ spec.email = ["fire_extinguisher-@ezweb.ne.jp"]
11
+
12
+ spec.summary = %q{ruby on rails gem baseapi}
13
+ spec.description = %q{ruby on rails gem baseapi}
14
+ spec.homepage = "https://github.com/arakawamoriyuki/baseapi"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ spec.bindir = "exe"
19
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.add_development_dependency "bundler", "~> 1.9"
23
+ spec.add_development_dependency "rake", "~> 10.0"
24
+ end
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "baseapi"
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,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
data/exe/baseapi ADDED
@@ -0,0 +1,5 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'baseapi/cli'
4
+
5
+ Baseapi::CLI.start
data/lib/baseapi.rb ADDED
@@ -0,0 +1,9 @@
1
+ require "baseapi/version"
2
+
3
+ # ActiveRecord::Relation Extension
4
+ require 'baseapi/active_record/relation_extension'
5
+ ActiveRecord::Relation.send(:include, ActiveRecordRelationExtension)
6
+
7
+ # ActiveRecord::Base Extension
8
+ require 'baseapi/active_record/base_extension'
9
+ ActiveRecord::Base.send(:include, ActiveRecordBaseExtension)
@@ -0,0 +1,125 @@
1
+ module ActiveRecordBaseExtension extend ActiveSupport::Concern
2
+
3
+ # override if necessary
4
+ def _destroy
5
+ destroy
6
+ end
7
+
8
+ module ClassMethods
9
+
10
+ # override if necessary
11
+ # @return ActiveRecordRelation
12
+ def _all
13
+ self.all
14
+ end
15
+
16
+ # override or create method '_where_{column}' if necessary
17
+ # @param ActiveRecordRelation models
18
+ # @param String column column name
19
+ # @param Array values search values
20
+ def _where(models, column, values)
21
+ model.where!(column => values)
22
+ end
23
+
24
+ # override or create method '_belongs_to_{table}' if necessary
25
+ # @param ActiveRecordRelation models
26
+ # @param String table table name
27
+ # @param Hash hash column name => search values
28
+ def _belongs_to(models, table, hash)
29
+ relation_match(models, table, hash)
30
+ end
31
+
32
+ # override or create method '_has_many_{table}' if necessary
33
+ # @param ActiveRecordRelation models
34
+ # @param String table table name
35
+ # @param Hash hash column name => search values
36
+ def _has_many(models, table, hash)
37
+ relation_match(models, table, hash)
38
+ end
39
+
40
+ # Exact match search
41
+ # @param ActiveRecordRelation models
42
+ # @param String table table name
43
+ # @param Hash hash column name => search values
44
+ # @option String operator 'or' or 'and'
45
+ def relation_match(models, table, hash, operator:'or')
46
+ relation_call(models, table, hash, ->(table, column, value){
47
+ "#{table}.#{column} = '#{value}'"
48
+ }, operator:operator)
49
+ end
50
+
51
+ # like search
52
+ # @param ActiveRecordRelation models
53
+ # @param String table table name
54
+ # @param Hash hash column name => search values
55
+ # @option String operator 'or' or 'and'
56
+ def relation_like(models, table, hash, operator:'or')
57
+ relation_call(models, table, hash, ->(table, column, value){
58
+ "#{table}.#{column} like '%#{value}%'"
59
+ }, operator:operator)
60
+ end
61
+
62
+ # @param ActiveRecordRelation models
63
+ # @param String table table name
64
+ # @param Hash hash column name => search values
65
+ # @param Callable callable
66
+ # @option String operator orかand
67
+ def relation_call(models, table, hash, callable, operator:'or')
68
+ models.joins!(table.to_sym)
69
+ hash.each do |column, value|
70
+ if column.present? and value.present?
71
+ relation_values = value.instance_of?(Array) ? value : [value]
72
+ models.where!(relation_values.map{|value| callable.call(table.pluralize, column, value)}.join(" #{operator} "))
73
+ end
74
+ end
75
+ models
76
+ end
77
+
78
+ # get relation tables
79
+ # @param String relate 'belongs_to','hasmany'..
80
+ # @return Hash associations relation name => talbe name array
81
+ def get_associations(relate = nil)
82
+ associations = {
83
+ 'belongs_to' => [],
84
+ 'has_many' => [],
85
+ }
86
+ self.reflect_on_all_associations.each do |association|
87
+ associations.each do |key, value|
88
+ if association.class.to_s == "ActiveRecord::Reflection::#{key.camelize}Reflection"
89
+ associations[key].push(association.name.to_s)
90
+ end
91
+ end
92
+ end
93
+ relate.present? ? associations[relate] : associations
94
+ end
95
+
96
+
97
+ # @param Hash params getparams
98
+ # String/Array {column} search column value
99
+ # String orderby order column
100
+ # String order sort order ('asc' or 'desc')
101
+ # Integer count page count (all = '0' or '-1')
102
+ # Integer page paged
103
+ def search(params)
104
+ models = self._all
105
+
106
+ # load
107
+ self.get_associations().each do |association_key, relations|
108
+ relations.each do |relation|
109
+ models.includes!(relation.to_sym)
110
+ end
111
+ end
112
+
113
+ # filter
114
+ models.filtering!(params)
115
+
116
+ # pager
117
+ models.paging!(params)
118
+
119
+ # sort
120
+ models.sorting!(params)
121
+
122
+ return models.uniq
123
+ end
124
+ end
125
+ end
@@ -0,0 +1,58 @@
1
+ module ActiveRecordRelationExtension
2
+ # ----- model relation object methods -----
3
+ # def relation_object_method
4
+ # end
5
+ # Model.all.relation_object_method
6
+
7
+ # カラムの検索
8
+ # @param Hash params 検索パラメータ
9
+ def filtering!(params)
10
+ models = self
11
+ associations = self.model.get_associations()
12
+ params.each do |key, value|
13
+ if key.present? and value.present?
14
+ if column_names.include?(key)
15
+ # 配列に対応する
16
+ values = value.instance_of?(Array) ? value : [value]
17
+ values.reject!(&:blank?)
18
+ # カラム用関数が定義されていればそれを使用
19
+ function_name = self.model.methods.include?("_where_#{key}".to_sym) ? "_where_#{key}" : '_where'
20
+ models = self.model.send(function_name, models, key, values)
21
+ end
22
+
23
+ # belongs_to, has_manyの検索
24
+ associations.keys.each do |association|
25
+ if associations[association].include?(key) and value.instance_of?(ActionController::Parameters)#hash型はActionController::Parameters
26
+ # カラム用関数が定義されていればそれを使用
27
+ function_name = self.model.methods.include?("_#{association}_#{key}".to_sym) ? "_#{association}_#{key}" : "_#{association}"
28
+ models = self.model.send(function_name, models, key, value)
29
+ end
30
+ end
31
+ end
32
+ end
33
+ return models
34
+ end
35
+
36
+ # ページャー
37
+ # @param Hash params 検索パラメータ
38
+ def paging!(params)
39
+ count = params[:count].present? ? params[:count].to_i : -1;
40
+ page = params[:page].present? ? params[:page].to_i : 1;
41
+ if count > 0
42
+ if count.present? and count
43
+ limit!(count)
44
+ if page
45
+ offset!((page - 1) * count)
46
+ end
47
+ end
48
+ end
49
+ end
50
+
51
+ # ソート
52
+ # @param Hash params 検索パラメータ
53
+ def sorting!(params)
54
+ if params[:order].present? and params[:orderby].present?
55
+ order!({params[:orderby] => params[:order]})
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,89 @@
1
+ class BaseApiController < ActionController::Base
2
+
3
+ before_action :set_Model
4
+ before_action :set_models, only: [:index]
5
+ before_action :set_model, only: [:show, :create, :update, :destroy]
6
+
7
+ # GET /{models}.json
8
+ # @param Hash
9
+ # @return String Json(Models)
10
+ def index
11
+ render 'models.json.jbuilder'
12
+ end
13
+
14
+ # GET /{models}/{id}.json
15
+ # @return String Json(Model)
16
+ def show
17
+ render 'model.json.jbuilder'
18
+ end
19
+
20
+ # POST /{models}/{id}.json
21
+ # @param
22
+ # @return String Json(Model)
23
+ def create
24
+ render 'model.json.jbuilder' if transaction(-> {
25
+ params.each do |key, value|
26
+ if key.present? and value.present? and @Model.column_names.include?(key)
27
+ @model.send("#{key}=", value)
28
+ end
29
+ end
30
+ @model.save
31
+ })
32
+ end
33
+
34
+ # PATCH/PUT /{models}/{id}.json
35
+ # @param
36
+ # @return String Json(Model)
37
+ def update
38
+ render 'model.json.jbuilder' if transaction(-> {
39
+ params.each do |key, value|
40
+ if key.present? and value.present? and @Model.column_names.include?(key)
41
+ @model.send("#{key}=", value)
42
+ end
43
+ end
44
+ @model.save
45
+ })
46
+ end
47
+
48
+ # DELETE /{models}/{id}.json
49
+ # @param
50
+ # @return String Json(Model)
51
+ def destroy
52
+ render 'model.json.jbuilder' if transaction(-> {
53
+ @model._destroy
54
+ })
55
+ end
56
+
57
+
58
+ private
59
+ # set Model class
60
+ def set_Model
61
+ @Model = params[:controller].camelize.singularize.constantize
62
+ end
63
+
64
+ # set model
65
+ def set_model
66
+ @model = params[:id].present? ? @Model.find(params[:id]) : @Model.new
67
+ end
68
+
69
+ # set models
70
+ def set_models
71
+ @models = @Model.search(params)
72
+ end
73
+
74
+ # transaction
75
+ # @param callable callable
76
+ # @return bool
77
+ def transaction (callable)
78
+ begin
79
+ @Model.transaction do
80
+ callable.call()
81
+ end
82
+ rescue => e
83
+ @message = e.message
84
+ render 'error.json.jbuilder'
85
+ return false
86
+ end
87
+ return true
88
+ end
89
+ end
@@ -0,0 +1,3 @@
1
+ json.error true
2
+ json.message @message
3
+ json.data {}
@@ -0,0 +1,3 @@
1
+ json.error false
2
+ json.message ''
3
+ json.data @model
@@ -0,0 +1,3 @@
1
+ json.error false
2
+ json.message ''
3
+ json.data @models
@@ -0,0 +1,37 @@
1
+ # coding: utf-8
2
+
3
+ require 'thor'
4
+ require 'fileutils'
5
+
6
+ module Baseapi
7
+ class CLI < Thor
8
+
9
+ desc "Base API setup", "create BaseApiController and jbuilder view."
10
+ def setup
11
+ dir = [
12
+ 'app/views/base_api'
13
+ ]
14
+ dir.each do |path|
15
+ if !Dir.exists?(path)
16
+ Dir.mkdir(path)
17
+ end
18
+ end
19
+
20
+ files = {
21
+ 'base_api_controller.rb' => 'controllers',
22
+ 'error.json.jbuilder' => 'views/base_api',
23
+ 'model.json.jbuilder' => 'views/base_api',
24
+ 'models.json.jbuilder' => 'views/base_api',
25
+ }
26
+
27
+ files.each do |file, path|
28
+ src = File.expand_path("../app/#{path}/#{file}", __FILE__)
29
+ if File.exists?("app/#{path}/#{file}")
30
+ warn "[skip] app/#{path}/#{file} already exists"
31
+ else
32
+ FileUtils.cp(src, "app/#{path}/#{file}")
33
+ end
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,3 @@
1
+ module Baseapi
2
+ VERSION = "0.1.0"
3
+ end
metadata ADDED
@@ -0,0 +1,94 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: baseapi
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Moriyuki Arakawa
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-07-29 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.9'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.9'
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
+ description: ruby on rails gem baseapi
42
+ email:
43
+ - fire_extinguisher-@ezweb.ne.jp
44
+ executables:
45
+ - baseapi
46
+ extensions: []
47
+ extra_rdoc_files: []
48
+ files:
49
+ - .gitignore
50
+ - .rspec
51
+ - .travis.yml
52
+ - CODE_OF_CONDUCT.md
53
+ - Gemfile
54
+ - LICENSE.txt
55
+ - README.md
56
+ - Rakefile
57
+ - baseapi.gemspec
58
+ - bin/console
59
+ - bin/setup
60
+ - exe/baseapi
61
+ - lib/baseapi.rb
62
+ - lib/baseapi/active_record/base_extension.rb
63
+ - lib/baseapi/active_record/relation_extension.rb
64
+ - lib/baseapi/app/controllers/base_api_controller.rb
65
+ - lib/baseapi/app/views/base_api/error.json.jbuilder
66
+ - lib/baseapi/app/views/base_api/model.json.jbuilder
67
+ - lib/baseapi/app/views/base_api/models.json.jbuilder
68
+ - lib/baseapi/cli.rb
69
+ - lib/baseapi/version.rb
70
+ homepage: https://github.com/arakawamoriyuki/baseapi
71
+ licenses:
72
+ - MIT
73
+ metadata: {}
74
+ post_install_message:
75
+ rdoc_options: []
76
+ require_paths:
77
+ - lib
78
+ required_ruby_version: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ required_rubygems_version: !ruby/object:Gem::Requirement
84
+ requirements:
85
+ - - '>='
86
+ - !ruby/object:Gem::Version
87
+ version: '0'
88
+ requirements: []
89
+ rubyforge_project:
90
+ rubygems_version: 2.4.6
91
+ signing_key:
92
+ specification_version: 4
93
+ summary: ruby on rails gem baseapi
94
+ test_files: []