auto_api 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 39b05ca8941181e3462bdb72162c5b9237ae711e
4
+ data.tar.gz: e32ccf3e35eca0db32cb0ea943369e93a442b6cb
5
+ SHA512:
6
+ metadata.gz: b7b6a3a94eeda305243bb625d1062061d2cb6c5bd8e85c1b800447995324be36f7aeda250e46a52da52d45bf61527be571c6a6071dcc9c6269c4edaed00adabb
7
+ data.tar.gz: 8e3e7fa12d2a65b3016ef8d7a8965594e2a5ead1f88f7bac83e8a0b071154d633c599f556fbef07ea2fded3b60da4da588feba4201dc9e49ba5ad5cd74f7712d
@@ -0,0 +1,24 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ *.bundle
19
+ *.so
20
+ *.o
21
+ *.a
22
+ mkmf.log
23
+ config.ru
24
+ config/mongoid.yml
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in auto_api.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Phil Spitler
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,98 @@
1
+ # AutoApi
2
+
3
+ AutoApi generates a RESTful API on the fly. If you call gets on items that do not exist, you get back what you'd expect if the object didn't exist. If you POST to something, even if it doesn't exist, it will create it on the fly, which then can be retreived from the database. It runs on Sinatra and MongoDB.
4
+
5
+ ## What this Provides
6
+ You just start calling endpoints and it just works. It’s pretty useless if you start off by making GET requests. You just get back empty arrays or 404s. However, start storing anything you want at all and then when making GET reqeusts, you will received the appropriate objects.
7
+
8
+ AutoApi handles all the normal RESTful single level routes, for example:
9
+
10
+ ```
11
+ GET /forums
12
+
13
+ GET /forums/asdf1234
14
+
15
+ POST /forums #forum created with reqeust.POST data
16
+
17
+ PUT /forums/asdf1234 #forum asdf1234 updated with request.POST data
18
+
19
+ DELETE /forums/asdf1234 #deletes forum asdf1234 (note: related data is not cleaned up at this point)
20
+
21
+ DELETE /forums #deletes ALL forums (note: related data is not cleaned up at this point)
22
+
23
+ ```
24
+
25
+ AutoApi will handle nested calls for GET (list), POST, and DELETE (list) for example:
26
+
27
+ ```
28
+ GET /forums/asdf1234/topics
29
+
30
+ POST /forums/asdf1234/topics #a new topic is created from request.POST and associated to forum asdf1234
31
+
32
+ DELETE /forums/asdf1234/topics #deletes all the topics for the forum (note: data related to the deleted topics is not cleaned up at this point)
33
+ ```
34
+
35
+ Note: forums and topics are used it this example, but topics could have posts. Or you could use this for user data or really whatever else you want a RESTful interface for. I can see this being great for rapid front-end prototyping.
36
+
37
+ ## Installation
38
+
39
+ Add this line to your application's Gemfile:
40
+
41
+ gem 'auto_api'
42
+
43
+ And then execute:
44
+
45
+ $ bundle
46
+
47
+ Or install it yourself as:
48
+
49
+ $ gem install auto_api
50
+
51
+ ## Requirements
52
+
53
+ - MongoDB
54
+
55
+ ## Usage
56
+
57
+ ### Config File
58
+
59
+ - Rename config/mongoid.yml.example to config/mongoid.yml
60
+ - Make whatever changes to the config file to work in your environment
61
+
62
+ ### Configure config.ru
63
+
64
+ Add the following to your config.ru file:
65
+
66
+ ```ruby
67
+ require 'auto_api'
68
+
69
+ map '/api' do #can use whatever path or add version or anything since this is modular
70
+ run AutoApi::Base
71
+ end
72
+ ```
73
+
74
+ ## Roadmap
75
+
76
+ There are many things I see going into this in the future. My first priorities are:
77
+
78
+ - Destroy orphaned data in the database
79
+ - Destroy objects that no longer contain data in the DB to free memory
80
+ - Enable the ability to run in a “mock” environment which cleans up after every call.
81
+ - Any other useful features that emerge as I developing and using it
82
+ - note: requests are always welcome (especially pull requests)
83
+
84
+ ## Warning
85
+
86
+ This is currently in a prototype/alpha phase.
87
+
88
+ ## Tests / Specs (coming soon)
89
+
90
+ In order to get this out in the wild as soon as possible, I am pusing the prototype. Full tests/specs are to come in the very near future.
91
+
92
+ ## Contributing
93
+
94
+ 1. Fork it ( https://github.com/philspitler/auto_api/fork )
95
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
96
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
97
+ 4. Push to the branch (`git push origin my-new-feature`)
98
+ 5. Create a new Pull Request
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
@@ -0,0 +1,30 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'auto_api/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "auto_api"
8
+ spec.version = AutoApi::VERSION
9
+ spec.authors = ["Phillip R. Spitler (Phil)"]
10
+ spec.email = ["prs@sproutkey.com"]
11
+ spec.summary = "Automattic API Generator"
12
+ spec.description = "AutoApi generates a RESTful API on the fly. If you call gets on items that do not exist, you get back what you'd expect if the object didn't exist. If you POST to something, even if it doesn't exist, it will create it on the fly, which then can be retreived from the database. It runs on Sinatra and MongoDB."
13
+ spec.homepage = "https://github.com/philspitler/auto_api"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_dependency 'sinatra'
22
+ spec.add_dependency 'sinatra-contrib'
23
+ spec.add_dependency "mongoid", "~> 3.1.6"
24
+
25
+ spec.add_development_dependency "bundler", "~> 1.6"
26
+ spec.add_development_dependency "rake"
27
+ spec.add_development_dependency "rspec"
28
+ spec.add_development_dependency "bond" #auto completion when running $ bundle exec irb
29
+
30
+ end
@@ -0,0 +1,7 @@
1
+ identity_map_enabled: true
2
+ development:
3
+ sessions:
4
+ default:
5
+ database: mongoid
6
+ hosts:
7
+ - 127.0.0.1:27017
@@ -0,0 +1,16 @@
1
+ #!/usr/bin/env ruby
2
+ #
3
+ # This file was generated by Bundler.
4
+ #
5
+ # The application 'htmldiff' is installed as part of a gem, and
6
+ # this file is here to facilitate running it.
7
+ #
8
+
9
+ require 'pathname'
10
+ ENV['BUNDLE_GEMFILE'] ||= File.expand_path("../../Gemfile",
11
+ Pathname.new(__FILE__).realpath)
12
+
13
+ require 'rubygems'
14
+ require 'bundler/setup'
15
+
16
+ load Gem.bin_path('diff-lcs', 'htmldiff')
@@ -0,0 +1,16 @@
1
+ #!/usr/bin/env ruby
2
+ #
3
+ # This file was generated by Bundler.
4
+ #
5
+ # The application 'ldiff' is installed as part of a gem, and
6
+ # this file is here to facilitate running it.
7
+ #
8
+
9
+ require 'pathname'
10
+ ENV['BUNDLE_GEMFILE'] ||= File.expand_path("../../Gemfile",
11
+ Pathname.new(__FILE__).realpath)
12
+
13
+ require 'rubygems'
14
+ require 'bundler/setup'
15
+
16
+ load Gem.bin_path('diff-lcs', 'ldiff')
@@ -0,0 +1,16 @@
1
+ #!/usr/bin/env ruby
2
+ #
3
+ # This file was generated by Bundler.
4
+ #
5
+ # The application 'rackup' is installed as part of a gem, and
6
+ # this file is here to facilitate running it.
7
+ #
8
+
9
+ require 'pathname'
10
+ ENV['BUNDLE_GEMFILE'] ||= File.expand_path("../../Gemfile",
11
+ Pathname.new(__FILE__).realpath)
12
+
13
+ require 'rubygems'
14
+ require 'bundler/setup'
15
+
16
+ load Gem.bin_path('rack', 'rackup')
@@ -0,0 +1,16 @@
1
+ #!/usr/bin/env ruby
2
+ #
3
+ # This file was generated by Bundler.
4
+ #
5
+ # The application 'rake' is installed as part of a gem, and
6
+ # this file is here to facilitate running it.
7
+ #
8
+
9
+ require 'pathname'
10
+ ENV['BUNDLE_GEMFILE'] ||= File.expand_path("../../Gemfile",
11
+ Pathname.new(__FILE__).realpath)
12
+
13
+ require 'rubygems'
14
+ require 'bundler/setup'
15
+
16
+ load Gem.bin_path('rake', 'rake')
@@ -0,0 +1,16 @@
1
+ #!/usr/bin/env ruby
2
+ #
3
+ # This file was generated by Bundler.
4
+ #
5
+ # The application 'rspec' is installed as part of a gem, and
6
+ # this file is here to facilitate running it.
7
+ #
8
+
9
+ require 'pathname'
10
+ ENV['BUNDLE_GEMFILE'] ||= File.expand_path("../../Gemfile",
11
+ Pathname.new(__FILE__).realpath)
12
+
13
+ require 'rubygems'
14
+ require 'bundler/setup'
15
+
16
+ load Gem.bin_path('rspec-core', 'rspec')
@@ -0,0 +1,16 @@
1
+ #!/usr/bin/env ruby
2
+ #
3
+ # This file was generated by Bundler.
4
+ #
5
+ # The application 'tilt' is installed as part of a gem, and
6
+ # this file is here to facilitate running it.
7
+ #
8
+
9
+ require 'pathname'
10
+ ENV['BUNDLE_GEMFILE'] ||= File.expand_path("../../Gemfile",
11
+ Pathname.new(__FILE__).realpath)
12
+
13
+ require 'rubygems'
14
+ require 'bundler/setup'
15
+
16
+ load Gem.bin_path('tilt', 'tilt')
@@ -0,0 +1,2 @@
1
+ require "auto_api/version"
2
+ require "auto_api/base"
@@ -0,0 +1,97 @@
1
+ require 'sinatra'
2
+ require "sinatra/reloader"
3
+ require "sinatra/json"
4
+ require 'mongoid'
5
+ require 'active_support/inflector'
6
+ require 'active_model'
7
+
8
+ class AutoApi::Base < Sinatra::Base
9
+ configure :development do
10
+ register Sinatra::Reloader
11
+ end
12
+
13
+ Mongoid.load!('./config/mongoid.yml')
14
+
15
+ #WE ARE RETURNING JSON
16
+ before '/*' do
17
+ content_type 'application/json'
18
+ end
19
+
20
+ before '/:resource1/:id1/:resource2/?' do | resource1, id1, resource2 |
21
+ @resource1 = Object.const_set(resource1.classify, Class.new)
22
+ @resource1.include(Mongoid::Document)
23
+ @resource1.has_many resource2
24
+ @resource2 = Object.const_set(resource2.classify, Class.new)
25
+ @resource2.include(Mongoid::Document)
26
+ @resource2.belongs_to resource1.singularize
27
+ end
28
+
29
+ ['/:resource/?', '/:resource/:id/?'].each do |path|
30
+ before path do
31
+ @resource = Object.const_set(params['resource'].classify, Class.new)
32
+ @resource.include(Mongoid::Document)
33
+ end
34
+ end
35
+
36
+ get '/:resource/?' do | resource |
37
+ webtry(lambda { json @resource.all })
38
+ end
39
+
40
+ get '/:resource/:id/?' do | resource, id |
41
+ webtry(lambda { json @resource.find(id) })
42
+ end
43
+
44
+ post '/:resource/?' do | resource |
45
+ webtry(lambda {
46
+ resource = @resource.new(request.POST)
47
+ resource.save!
48
+ json resource
49
+ })
50
+ end
51
+
52
+ put '/:resource/:id/?' do | resource, id |
53
+ webtry(lambda {
54
+ resource = @resource.find(id)
55
+ resource.update_attributes!(request.POST)
56
+ })
57
+ end
58
+
59
+ delete '/:resource/:id/?' do | resource, id |
60
+ webtry(lambda { json @resource.find(id).delete })
61
+ end
62
+
63
+ delete '/:resource/?' do
64
+ webtry(lambda { json @resource.all.delete })
65
+ end
66
+
67
+ #NESTED
68
+ get '/:resource1/:id1/:resource2/?' do | resource1, id1, resource2 |
69
+ webtry(lambda { json @resource1.find(id1).send(resource2) })
70
+ end
71
+
72
+ post '/:resource1/:id1/:resource2/?' do | resource1, id1, resource2 |
73
+ webtry(lambda {
74
+ parent_resource = @resource1.find(id1)
75
+ child_resource = @resource2.new(request.POST)
76
+ child_resource.save!
77
+ parent_resource.send(resource2) << child_resource
78
+ json child_resource
79
+ })
80
+ end
81
+
82
+ delete '/:resource1/:id1/:resource2/?' do | resource1, id1, resource2 |
83
+ webtry(lambda { json @resource1.find(id1).send(resource2).all.delete })
84
+ end
85
+
86
+ def webtry(block)
87
+ begin
88
+ status 200
89
+ block.call
90
+ rescue Mongoid::Errors::DocumentNotFound
91
+ status 404
92
+ rescue
93
+ status 500
94
+ end
95
+ end
96
+ end
97
+
@@ -0,0 +1,3 @@
1
+ module AutoApi
2
+ VERSION = "0.0.4"
3
+ end
metadata ADDED
@@ -0,0 +1,161 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: auto_api
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.4
5
+ platform: ruby
6
+ authors:
7
+ - Phillip R. Spitler (Phil)
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-11-08 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: sinatra
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: sinatra-contrib
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: mongoid
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: 3.1.6
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: 3.1.6
55
+ - !ruby/object:Gem::Dependency
56
+ name: bundler
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '1.6'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '1.6'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rake
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
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: rspec
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: bond
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ description: AutoApi generates a RESTful API on the fly. If you call gets on items
112
+ that do not exist, you get back what you'd expect if the object didn't exist. If
113
+ you POST to something, even if it doesn't exist, it will create it on the fly, which
114
+ then can be retreived from the database. It runs on Sinatra and MongoDB.
115
+ email:
116
+ - prs@sproutkey.com
117
+ executables: []
118
+ extensions: []
119
+ extra_rdoc_files: []
120
+ files:
121
+ - ".gitignore"
122
+ - Gemfile
123
+ - LICENSE.txt
124
+ - README.md
125
+ - Rakefile
126
+ - auto_api.gemspec
127
+ - config/mongoid.yml.example
128
+ - exec/htmldiff
129
+ - exec/ldiff
130
+ - exec/rackup
131
+ - exec/rake
132
+ - exec/rspec
133
+ - exec/tilt
134
+ - lib/auto_api.rb
135
+ - lib/auto_api/base.rb
136
+ - lib/auto_api/version.rb
137
+ homepage: https://github.com/philspitler/auto_api
138
+ licenses:
139
+ - MIT
140
+ metadata: {}
141
+ post_install_message:
142
+ rdoc_options: []
143
+ require_paths:
144
+ - lib
145
+ required_ruby_version: !ruby/object:Gem::Requirement
146
+ requirements:
147
+ - - ">="
148
+ - !ruby/object:Gem::Version
149
+ version: '0'
150
+ required_rubygems_version: !ruby/object:Gem::Requirement
151
+ requirements:
152
+ - - ">="
153
+ - !ruby/object:Gem::Version
154
+ version: '0'
155
+ requirements: []
156
+ rubyforge_project:
157
+ rubygems_version: 2.4.2
158
+ signing_key:
159
+ specification_version: 4
160
+ summary: Automattic API Generator
161
+ test_files: []