easy_api_operations 0.1.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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: d37e8fee6cfdb0688816b209303c051756486ea2
4
+ data.tar.gz: fa06caab4a7f4ccf9937881abfec62d16298cce5
5
+ SHA512:
6
+ metadata.gz: b55dd4119e00616a38e448995e11741a48122a3f4bd175a1a0db4950a1cdfef52f8e8aadf982cd6b465f62ac19b365dcb7bc91374de404039e637d4ecd031066
7
+ data.tar.gz: abbf2f789630801603718ddeb81ab9f6dad4344b6b91d758b64eab8d53721779585f11b350da41cac768139262fac9a08a550b284afa9e1f28a967f7ef129abe
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/.travis.yml ADDED
@@ -0,0 +1,3 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.1
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in easy_api_operations.gemspec
4
+ gemspec
data/README.md ADDED
@@ -0,0 +1,39 @@
1
+ # EasyApiOperations
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/easy_api_operations`. 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 'easy_api_operations'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install easy_api_operations
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.
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]/easy_api_operations/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/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "easy_api_operations"
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
@@ -0,0 +1,22 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'easy_api_operations/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "easy_api_operations"
8
+ spec.version = EasyApiOperations::VERSION
9
+ spec.authors = ["Bjoern Moeller / Christian Hentke"]
10
+ spec.summary = "build api operations via one liner"
11
+ spec.description = "call .set_up_api(self, ressource_name, operations) within grape api and you will be able to get the api build for you."
12
+ spec.license = "MIT"
13
+
14
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
15
+ spec.bindir = "exe"
16
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
17
+ spec.require_paths = ["lib"]
18
+
19
+ spec.add_development_dependency "bundler", "~> 1.9"
20
+ spec.add_development_dependency "rake", "~> 10.0"
21
+ spec.add_development_dependency "easy_model_selects"
22
+ end
@@ -0,0 +1,3 @@
1
+ module EasyApiOperations
2
+ VERSION = "0.1.1"
3
+ end
@@ -0,0 +1,188 @@
1
+ require "easy_api_operations/version"
2
+
3
+ module EasyApiOperations
4
+ def self.set_up_api(api, ressource_name, operations)
5
+ operations.downcase
6
+ ressource_name.capitalize
7
+
8
+ #ressources are singular and plural
9
+ #methods are primary_key, get, put, delete and post
10
+ #option all is the easiest
11
+
12
+ api_version = api.parent_name
13
+
14
+ http_codes_hash = {
15
+ 200 => 'Ok',
16
+ 400 => 'Invalid parameter entry',
17
+ 403 => 'Not allowed',
18
+ 404 => 'No appointment found'
19
+ }
20
+ http_codes_hash_extra_creates = {500 => 'unable to create/update'}
21
+
22
+
23
+ #for understanding:
24
+ #(api_version + "::Entities::" + ressource_name.pluralize).constantize == API::V1::Entities::Appointments
25
+ #(api_version + "::Entities::" + ressource_name + "UpdateBody").constantize == API::V1::Entities::AppointmentUpdateBody
26
+ #(api_version + "::Entities::" + ressource_name + "CreateBody").constantize == API::V1::Entities::AppointmentCreateBody
27
+ #ressource_name.constantize == Appointment
28
+
29
+
30
+ #___________ressource plural start
31
+ if operations.include?("plural") or operations.include?("all")
32
+ api.resource ressource_name.downcase.pluralize.to_sym do
33
+
34
+ #___________get_______________start__________________
35
+ #!!!description + params that can be requested -> currently the same as the once you get back, this can be adjusted (see other possibilities at the end)
36
+ if operations.include?("plural_get") or operations.include?("all")
37
+ desc "Returns a list of #{ressource_name}. Limited by 1000", params: (api_version + "::Entities::" + ressource_name.pluralize).constantize.documentation#, entity: API::V1::Entities::Appointments
38
+ get do
39
+ if params.empty?
40
+ present "Choose something dude"
41
+ else
42
+ results = ressource_name.constantize.where(EasyModelSelects.get_where_statement_from_param(params)).order(ressource_name.constantize.primary_key).limit(1000)
43
+ present results, with: (api_version + "::Entities::" + ressource_name.pluralize).constantize
44
+ end
45
+ end
46
+ else
47
+ #no plural_get
48
+ end
49
+ end
50
+ else
51
+ #no plural
52
+ end
53
+
54
+ #___________ressource singulare start
55
+ if operations.include?("singular") or operations.include?("all")
56
+ api.resource ressource_name.downcase.to_sym do
57
+
58
+ #___________get_primary_key_______________start__________________
59
+ if operations.include?("singular_primary_key") or operations.include?("all")
60
+ desc "Returns the primary key from #{ressource_name.pluralize}"
61
+ #!!! get then the path addition
62
+ get "primary_key" do
63
+ ressource_name.constantize.primary_key.to_sym
64
+ end
65
+ else
66
+ #no singular_primary_key
67
+ end
68
+
69
+ #___________get_______________start__________________
70
+ if operations.include?("singular_get") or operations.include?("all")
71
+ desc "Returns #{ressource_name.indefinitize} by #{ressource_name.constantize.primary_key}"
72
+ params do
73
+ requires ressource_name.constantize.primary_key, type: Integer, desc: "#{ressource_name} #{ressource_name.constantize.primary_key}"
74
+ end
75
+ get "#{ressource_name.constantize.primary_key.to_sym}", http_codes: http_codes_hash do
76
+ instance_variable = ressource_name.constantize.where("#{ressource_name.constantize.primary_key} IN (?)", params[ressource_name.constantize.primary_key]).first
77
+ if instance_variable.nil?
78
+ error! http_codes_hash[404], 404
79
+ end
80
+ present instance_variable, with: (api_version + "::Entities::" + ressource_name).constantize
81
+ end
82
+ else
83
+ #no singular_get
84
+ end
85
+ #___________put_______________start__________________
86
+ if operations.include?("singular_put") or operations.include?("all")
87
+ desc "Updates #{ressource_name.indefinitize} by #{ressource_name.constantize.primary_key}"
88
+ params do
89
+ #!!!no need to require the primary_key as it is requested within the body already, change the body, change this part
90
+ #requires Appointment.primary_key, type: String, desc: "Appointment #{Appointment.primary_key}"
91
+ optional :body, type: (api_version + "::Entities::" + ressource_name + "UpdateBody").constantize, desc: 'Body to update params'
92
+ end
93
+ put ressource_name.constantize.primary_key, http_codes: http_codes_hash.merge(http_codes_hash_extra_creates) do
94
+
95
+ instance_variable = ressource_name.constantize.where("#{ressource_name.constantize.primary_key} IN (?)", params[ressource_name.constantize.primary_key]).first
96
+ if instance_variable.nil?
97
+ error! http_codes_hash[404], 404
98
+ end
99
+
100
+ transformed_params = (api_version + "::Entities::" + ressource_name + "UpdateBody").constantize.represent(params, serializable: true)
101
+ transformed_params.keys.each do |key|
102
+ if transformed_params[key].nil?
103
+ transformed_params.delete(key)
104
+ end
105
+ end
106
+
107
+ if instance_variable.update!(transformed_params)
108
+ status 204
109
+ else
110
+ error! http_codes_hash_extra_creates[500], 500
111
+ end
112
+ end
113
+ else
114
+ #no singular_put
115
+ end
116
+
117
+ #___________delete_______________start__________________
118
+ if operations.include?("singular_delete") or operations.include?("all")
119
+ desc "Deletes #{ressource_name.indefinitize} by #{ressource_name.constantize.primary_key}"
120
+
121
+ params do
122
+ requires ressource_name.constantize.primary_key, type: String, desc: "#{ressource_name} #{ressource_name.constantize.primary_key}"
123
+ end
124
+ delete ressource_name.constantize.primary_key, http_codes: http_codes_hash do
125
+ instance_variable = ressource_name.constantize.where("#{ressource_name.constantize.primary_key} IN (?)", params[ressource_name.constantize.primary_key]).first
126
+
127
+ if instance_variable.nil?
128
+ error! http_codes_hash[404], 404
129
+ else
130
+ instance_variable.destroy
131
+ status 204
132
+ end
133
+
134
+ end
135
+ else
136
+ #no singular pluaral
137
+ end
138
+
139
+ #___________create_______________start__________________
140
+ if operations.include?("singular_post") or operations.include?("all")
141
+ desc "Creates #{ressource_name.indefinitize}"#, entity: API::V1::Entities::Appointments
142
+ params do
143
+ optional :body, type: (api_version + "::Entities::" + ressource_name + "CreateBody").constantize, desc: "Body to create #{"Appointment".indefinitize}"
144
+ end
145
+ post http_codes: http_codes_hash.merge(http_codes_hash_extra_creates) do
146
+
147
+
148
+ transformed_params = (api_version + "::Entities::" + ressource_name + "CreateBody").constantize.represent(params, serializable: true)
149
+
150
+ transformed_params.keys.each do |key|
151
+ if transformed_params[key].nil?
152
+ transformed_params.delete(key)
153
+ end
154
+ end
155
+
156
+ instance_variable = ressource_name.constantize.create(transformed_params)
157
+ if instance_variable.save
158
+ #status 204
159
+ body instance_variable.send("#{ressource_name.constantize.primary_key}")
160
+ else
161
+ error! http_codes_hash_extra_creates[500], 500
162
+ end
163
+ end
164
+ else
165
+ #no singular_post
166
+ end
167
+ end
168
+ else
169
+ #no singular ressource
170
+ end
171
+
172
+ #!!!other possibilities for more flexible param / respond structure
173
+
174
+ # paginate :per_page => 100
175
+
176
+ #!!!just show the attributes that are allowed by the modul
177
+ #prohibited_attributes = Appointment.get_prohibited_attributes
178
+ #!!! create params out of this
179
+ # params do
180
+ # optional :modified_since, documentation: { type: :string, desc: "Appointments created or modified since a certain date. Format: '2015-04-03T15:53:33.428Z'"}
181
+ # Appointment.get_prohibited_attributes_with_types.each do |column, type|
182
+ # optional column, documentation: { type: type, desc: "Optional Appointment property: #{column}"} # unless type.methods.include? :delimiter
183
+ # # optional column, type: :array, desc: "Optional Appointment property: #{column}" if type.methods.include? :delimiter
184
+ # end
185
+ # end
186
+ end
187
+
188
+ end
metadata ADDED
@@ -0,0 +1,96 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: easy_api_operations
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Bjoern Moeller / Christian Hentke
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-09-06 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
+ - !ruby/object:Gem::Dependency
42
+ name: easy_model_selects
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
+ description: call .set_up_api(self, ressource_name, operations) within grape api and
56
+ you will be able to get the api build for you.
57
+ email:
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - ".travis.yml"
64
+ - Gemfile
65
+ - README.md
66
+ - Rakefile
67
+ - bin/console
68
+ - bin/setup
69
+ - easy_api_operations.gemspec
70
+ - lib/easy_api_operations.rb
71
+ - lib/easy_api_operations/version.rb
72
+ homepage:
73
+ licenses:
74
+ - MIT
75
+ metadata: {}
76
+ post_install_message:
77
+ rdoc_options: []
78
+ require_paths:
79
+ - lib
80
+ required_ruby_version: !ruby/object:Gem::Requirement
81
+ requirements:
82
+ - - ">="
83
+ - !ruby/object:Gem::Version
84
+ version: '0'
85
+ required_rubygems_version: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ requirements: []
91
+ rubyforge_project:
92
+ rubygems_version: 2.4.6
93
+ signing_key:
94
+ specification_version: 4
95
+ summary: build api operations via one liner
96
+ test_files: []