json_voorhees 1.3.2 → 1.4.0

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: 97d0bc5b64a0b3f39b53d22065e2cea831606fc1
4
- data.tar.gz: 92b3368a5bc42303fee50e40a7d106c19b1ad0a9
3
+ metadata.gz: e58d03e1eccb3897bacd346af98d38c535341a35
4
+ data.tar.gz: 08b50880e0bb6e316ce6c0cf53714ca3d858c648
5
5
  SHA512:
6
- metadata.gz: 260cfcdb5422342bfb056426121c30886840380e67e3c7287a3f8c3b86d3760ebdbd97b5f71619b5b91c71b4475d6652252d71f68cb91bdb60efedfbcda5bf97
7
- data.tar.gz: 5ae939cfed5120ae9027122732434dc77ff41e9e5d962558174049d906e3ef90642adace7bfdd56251bb868ac0ba4a4973a4a70db97b8393b044c2c01c0713a5
6
+ metadata.gz: 737b04ec550dc16ef0eaba9fd3b0e2e6c30a6fcf932e48e87ddaf4bb32d286e8fc3dfda1c2ff65978ef0d33c30cecbf8cc0e5583dc867351fb5024968cb5686a
7
+ data.tar.gz: ac9d2aa35b9510b399b6fc9849daab5ede7f8105d0b17b74b827d958fb7b1962d9c9baa5b53ee9dbff8913b84e7cdccfc698bac893c13b783d091fb5dae81d8b
data/README.md CHANGED
@@ -6,7 +6,19 @@ I frequently take on projects that involve some kind of an api and it was a nigh
6
6
  repeating all of the boilerplate code to get the backend in a useable state.
7
7
  I abstracted away all of the code that I typically use into a series of generators
8
8
  namespaced under json_voorhees. Not only does it set your project up, it also
9
- helps build the project if you follow the conventions.
9
+ helps build the project if you follow the conventions.
10
+
11
+ The standards that I set up for my APIs are those that separate all resources into engines and
12
+ version everything. The benefits are seen immediately when versioning a controller etc but
13
+ when versioning a model not so. Versioning a model into classes like so, People::User,
14
+ People::V1::User and People::V2::User allow you to separate the model that contains the admin
15
+ display details (People::User) and the model that follows the rules of the api. In addition,
16
+ to avoid polluting controllers, authorizations are handled by the main app in a gem. Every
17
+ action is before_filtered to a function in the authorization gem and handed the current_user,
18
+ the current_resource and a couple auxillary variables. Tests are also handled in the
19
+ main app. The scaffolds will take care of the tedious work, just know where everything is
20
+ when you need it. Once you are accustomed to the location of everything, it will be very
21
+ easy to speed through the backends development.
10
22
 
11
23
  ## Scaffold Use
12
24
 
@@ -17,8 +29,15 @@ gem "json_voorhees"
17
29
  ```
18
30
  ```bash
19
31
  rails g json_voorhees:setup_app
32
+
33
+ or
34
+
35
+ rails g json_voorhees:setup_app --fbonly
20
36
  ```
21
37
 
38
+ The latter command will instead create a user from a fb access token in the login
39
+ process.
40
+
22
41
  Now when you want to create an engine run the below command from the main app.
23
42
  It takes care of mounting the engine, setting up the controllers, routes and gemspecs.
24
43
 
@@ -63,6 +82,9 @@ else resides in engines for good modularity.
63
82
  4. To use gmail, your account needs to have an app_password. That is the password that should go on file.
64
83
  5. Account mailer in the people engine needs to be updated with the correct domain as well the host in application.rb config file for production.
65
84
  6. The account controller isn't under the api, forgotten_passwords etc should be implemented on the server.
85
+ 7. To run the generators, sometimes spring will get in your way. If it hands run "spring stop"
86
+ 8. Cors may not work the same on all machines and rails versions. If it is acting up in production, if serve static assets is false, it wont find the Rails Static middleware to insert before and it will throw an error.
87
+ 9. For fbonly, make sure you go to the facebook folder and look at the file. You need to set the environment variables FB_APPID1 and FB_SECRET1.
66
88
 
67
89
  Does not work with Rails 4.2 yet. Byebug is added by default and this generator tries to add it twice.
68
90
 
@@ -16,7 +16,8 @@ module JsonVoorhees
16
16
 
17
17
  #So url_for works in the mailer
18
18
  config.action_mailer.default_url_options = { host: 'localhost:3000' }
19
- config.middleware.insert_before "ActionDispatch::Static", "Rack::Cors" do
19
+ #config.middleware.insert_before "ActionDispatch::Static", "Rack::Cors" do
20
+ config.middleware.use Rack::Cors do
20
21
  allow do
21
22
  origins '*'
22
23
  resource '*', :headers => :any, :methods => [:get, :post, :put, :delete, :options]
@@ -5,16 +5,25 @@ module JsonVoorhees
5
5
  argument :resource_name, :type => :string
6
6
  argument :api_version, :type => :string, :default => "1"
7
7
  argument :attributes, type: :array, default: [], banner: "field:type field:type"
8
+ class_option :fbonly, :type => :boolean, :default => false, :description => "Use facebook login?"
8
9
 
9
10
  def sprint
10
11
  template "model.rb.erb", "spec/engines/#{module_snake}/api/v#{api_version}/models/#{resource_singular}_spec.rb"
11
- template "request.rb.erb", "spec/engines/#{module_snake}/api/v#{api_version}/requests/#{resource_singular}_spec.rb"
12
12
  template "routing.rb.erb", "spec/engines/#{module_snake}/api/v#{api_version}/routing/#{resource_singular}_spec.rb"
13
13
  template "factory.rb.erb", "spec/factories/#{module_snake}_#{resource_singular}_#{api_version}_factory.rb"
14
+ requests
14
15
  end
15
16
 
16
17
  private
17
18
 
19
+ def requests
20
+ if options.fbonly?
21
+ template "fbonly_request.rb.erb", "spec/engines/#{module_snake}/api/v#{api_version}/requests/#{resource_singular}_spec.rb"
22
+ else
23
+ template "request.rb.erb", "spec/engines/#{module_snake}/api/v#{api_version}/requests/#{resource_singular}_spec.rb"
24
+ end
25
+ end
26
+
18
27
  def default_values(field1)
19
28
  field = field1.downcase
20
29
  if field == "integer"
@@ -0,0 +1,136 @@
1
+ require "rails_helper"
2
+
3
+ #-#-#-#-#REST#-#-#-#-#
4
+ RSpec.describe ::<%= module_camel %>::V<%= api_version %>::<%= resource_camel %>, :type => :request do
5
+ describe "Index" do
6
+ before(:example) do
7
+ @user = FactoryGirl.create(:fbuser_user_1)
8
+ token = @user.tokens[0].auth_token
9
+ @header = {"Auth-Token" => token}
10
+ end
11
+ # get /api/<%= api_version %>/<%= resource_plural %>
12
+ it "Gets all of the <%= resource_singular %>s" do
13
+ FactoryGirl.create_list(:<%= module_snake %>_<%= resource_singular %>_<%= api_version %>, 10)
14
+ get 'api/<%= api_version %>/<%= resource_plural %>', nil, @header
15
+ expect(response.status).to eq(200) #ok
16
+ expect(::<%= module_camel %>::V<%= api_version %>::<%= resource_camel %>.count).to eq(10)
17
+ expect(json["<%= resource_plural %>"].length).to eq(10)
18
+ end
19
+ end
20
+ end
21
+ RSpec.describe ::<%= module_camel %>::V<%= api_version %>::<%= resource_camel %>, :type => :request do
22
+ describe "Show" do
23
+ before(:example) do
24
+ @user = FactoryGirl.create(:fbuser_user_1)
25
+ token = @user.tokens[0].auth_token
26
+ @header = {"Auth-Token" => token}
27
+ end
28
+ # get /api/<%= api_version %>/<%= resource_plural %>/1
29
+ it "Gets a <%= resource_singular %> by id" do
30
+ <%= resource_singular %> = FactoryGirl.create(:<%= module_snake %>_<%= resource_singular %>_<%= api_version %>)
31
+ get "api/<%= api_version %>/<%= resource_plural %>/#{<%= resource_singular %>.id}", nil, @header
32
+ expect(response.status).to eq(200) #ok
33
+ end
34
+ end
35
+ end
36
+ RSpec.describe ::<%= module_camel %>::V<%= api_version %>::<%= resource_camel %>, :type => :request do
37
+ describe "Create" do
38
+ before(:example) do
39
+ @user = FactoryGirl.create(:fbuser_user_1)
40
+ token = @user.tokens[0].auth_token
41
+ @header = {"Auth-Token" => token}
42
+ end
43
+ # post /api/<%= api_version %>/<%= resource_plural %>
44
+ it "Creates <%= resource_singular %>" do
45
+ attrs = FactoryGirl.attributes_for(:<%= module_snake %>_<%= resource_singular %>_<%= api_version %>)
46
+ #attrs[:column] = "LaunchU"
47
+ hash = {"<%= resource_singular %>" => attrs}
48
+ post 'api/<%= api_version %>/<%= resource_plural %>', hash, @header
49
+ expect(response.status).to eq(200) #ok
50
+ end
51
+ end
52
+ end
53
+ RSpec.describe ::<%= module_camel %>::V<%= api_version %>::<%= resource_camel %>, :type => :request do
54
+ describe "Update" do
55
+ before(:example) do
56
+ @user = FactoryGirl.create(:fbuser_user_1)
57
+ token = @user.tokens[0].auth_token
58
+ @header = {"Auth-Token" => token}
59
+ end
60
+ # patch/put /api/<%= api_version %>/<%= resource_plural %>/1
61
+ it "Updates <%= resource_singular %>" do
62
+ #Create the <%= resource_singular %> through the api
63
+ attrs = FactoryGirl.attributes_for(:<%= module_snake %>_<%= resource_singular %>_<%= api_version %>)
64
+ #attrs[:column] = "LaunchU"
65
+ <%= resource_singular %> = FactoryGirl.create(:<%= module_snake %>_<%= resource_singular %>_<%= api_version %>)
66
+ hash = {"<%= resource_singular %>" => attrs}
67
+ put "api/<%= api_version %>/<%= resource_plural %>/#{<%= resource_singular %>.id}", hash, @header
68
+ expect(response.status).to eq(200) #ok
69
+ end
70
+ end
71
+ end
72
+ RSpec.describe ::<%= module_camel %>::V<%= api_version %>::<%= resource_camel %>, :type => :request do
73
+ describe "Destroy" do
74
+ before(:example) do
75
+ @user = FactoryGirl.create(:fbuser_user_1)
76
+ token = @user.tokens[0].auth_token
77
+ @header = {"Auth-Token" => token}
78
+ end
79
+ # delete /api/<%= api_version %>/<%= resource_plural %>/1
80
+ it "Deletes <%= resource_singular %>" do
81
+ #Create the <%= resource_singular %> through the api
82
+ attrs = FactoryGirl.attributes_for(:<%= module_snake %>_<%= resource_singular %>_<%= api_version %>)
83
+ #attrs[:column] = "LaunchU"
84
+ <%= resource_singular %> = FactoryGirl.create(:<%= module_snake %>_<%= resource_singular %>_<%= api_version %>)
85
+ expect(<%= module_camel %>::V<%= api_version %>::<%= resource_camel %>.count).to eq(1)
86
+ #Now delete the <%= resource_singular %> through the api
87
+ delete "api/<%= api_version %>/<%= resource_plural %>/#{<%= resource_singular %>.id}", nil, @header
88
+ expect(json).to eq({})
89
+ expect(response.status).to eq(200) #ok
90
+ expect(<%= module_camel %>::V<%= api_version %>::<%= resource_camel %>.count).to eq(0)
91
+ end
92
+ end
93
+ end
94
+ #-#-#-#-#Collection Routes#-#-#-#-#
95
+ RSpec.describe ::<%= module_camel %>::V<%= api_version %>::<%= resource_camel %>, :type => :request do
96
+ describe "Collection Routes" do
97
+ before(:example) do
98
+ @user = FactoryGirl.create(:fbuser_user_1)
99
+ token = @user.tokens[0].auth_token
100
+ @header = {"Auth-Token" => token}
101
+ end
102
+ # get /api/1/collection
103
+ it "checks response of a collection route" do
104
+
105
+ end
106
+ end
107
+ end
108
+ #-#-#-#-#Serialization#-#-#-#-#
109
+ RSpec.describe ::<%= module_camel %>::V<%= api_version %>::<%= resource_camel %>, :type => :request do
110
+ describe "Serialization" do
111
+ before(:example) do
112
+ @user = FactoryGirl.create(:fbuser_user_1)
113
+ token = @user.tokens[0].auth_token
114
+ @header = {"Auth-Token" => token}
115
+ end
116
+ it "checks the index json sent back" do
117
+
118
+ end
119
+ end
120
+ end
121
+ #-#-#-#-#Errors#-#-#-#-#
122
+ RSpec.describe ::<%= module_camel %>::V<%= api_version %>::<%= resource_camel %>, :type => :request do
123
+ describe "Errors" do
124
+ before(:example) do
125
+ @user = FactoryGirl.create(:fbuser_user_1)
126
+ token = @user.tokens[0].auth_token
127
+ @header = {"Auth-Token" => token}
128
+ end
129
+ # get /api/<%= api_version %>/<%= resource_plural %>/1
130
+ it "checks for a 404" do
131
+ <%= resource_singular %> = FactoryGirl.create(:<%= module_snake %>_<%= resource_singular %>_<%= api_version %>)
132
+ get "api/<%= api_version %>/<%= resource_plural %>/#{<%= resource_singular %>.id + 1}", nil, @header
133
+ expect(response.status).to eq(404) #ok
134
+ end
135
+ end
136
+ end
@@ -1,20 +1,44 @@
1
1
  module JsonVoorhees
2
2
  class AppMakeUserGenerator < Rails::Generators::Base
3
3
  source_root File.expand_path('../templates', __FILE__)
4
+ class_option :fbonly, :type => :boolean, :default => false, :description => "Use facebook login?"
4
5
 
5
6
  def sprint
6
- people
7
+ route "mount Arcadex::Engine, at: \'/\'"
8
+ if options.fbonly?
9
+ fb_only
10
+ else
11
+ people
12
+ end
7
13
  end
8
14
 
9
15
  private
10
16
 
17
+ def fb_only
18
+ run "git clone https://github.com/cleor41/fbuser.git"
19
+ run "mv fbuser engines/fbuser"
20
+ run "rm -rf engines/fbuser/.git"
21
+ #Need to add the engine to the main_apps gemfile
22
+ route "mount Fbuser::Engine, at: \'/\'"
23
+ insert_fb_user_engine
24
+ run "rails g fbuser:all"
25
+ end
26
+
27
+ def insert_fb_user_engine
28
+ inject_into_file 'Gemfile', after: "source \'https://rubygems.org\'\n" do <<-'RUBY'
29
+
30
+ gem 'fbuser', :path => "engines/fbuser"
31
+
32
+ RUBY
33
+ end
34
+ end
35
+
11
36
  def people
12
37
  run "git clone https://github.com/cleor41/people.git"
13
38
  run "mv people engines/people"
14
39
  run "rm -rf engines/people/.git"
15
40
  #Need to add the engine to the main_apps gemfile
16
41
  route "mount People::Engine, at: \'/\'"
17
- route "mount Arcadex::Engine, at: \'/\'"
18
42
  insert_people_engine
19
43
  run "rails g people:all"
20
44
  end
@@ -8,7 +8,11 @@ module JsonVoorhees
8
8
 
9
9
  def sprint
10
10
  run "rails g json_voorhees:app_make_authorizations #{module_name} #{resource_name} #{api_version} #{attributes.join(" ")}"
11
- run "rails g json_voorhees:app_make_tests #{module_name} #{resource_name} #{api_version} #{attributes.join(" ")}"
11
+ if Kernel.const_defined?("Fbuser")
12
+ run "rails g json_voorhees:app_make_tests --fbonly #{module_name} #{resource_name} #{api_version} #{attributes.join(" ")}"
13
+ else
14
+ run "rails g json_voorhees:app_make_tests #{module_name} #{resource_name} #{api_version} #{attributes.join(" ")}"
15
+ end
12
16
  end
13
17
 
14
18
  private
@@ -3,6 +3,7 @@ require 'bundler'
3
3
  module JsonVoorhees
4
4
  class SetupAppGenerator < Rails::Generators::Base
5
5
  source_root File.expand_path('../templates', __FILE__)
6
+ class_option :fbonly, :type => :boolean, :default => false, :description => "Use facebook login?"
6
7
 
7
8
  def sprint
8
9
  create_file_structure
@@ -57,7 +58,11 @@ module JsonVoorhees
57
58
  end
58
59
 
59
60
  def make_user
60
- run "rails g json_voorhees:app_make_user"
61
+ if options.fbonly?
62
+ run "rails g json_voorhees:app_make_user --fbonly"
63
+ else
64
+ run "rails g json_voorhees:app_make_user"
65
+ end
61
66
  run_bundle
62
67
  end
63
68
 
@@ -89,7 +94,7 @@ module JsonVoorhees
89
94
  end
90
95
 
91
96
  def api_controller
92
- copy_file "api_controller_with_arcadex.rb", "app/controllers/api/v1/api_controller.rb"
97
+ template "api_controller_with_arcadex.rb", "app/controllers/api/v1/api_controller.rb"
93
98
  end
94
99
 
95
100
  def rspec
@@ -139,6 +144,7 @@ module JsonVoorhees
139
144
  gem 'authorization', :path => "gems/authorization"
140
145
  gem 'whenever', :require => false
141
146
  gem 'rack-cors', :require => 'rack/cors'
147
+ gem 'httparty'
142
148
  #gem 'websocket-rails'
143
149
  end
144
150
 
@@ -2,10 +2,6 @@ class Api::V1::ApiController < ::ActionController::API
2
2
 
3
3
  before_action :authenticate_user
4
4
 
5
- def route_options
6
- cors_preflight_check
7
- end
8
-
9
5
  private
10
6
 
11
7
  def authenticate_user
@@ -15,6 +11,7 @@ class Api::V1::ApiController < ::ActionController::API
15
11
  end
16
12
  end
17
13
 
14
+ <% if !options.fbonly? %>
18
15
  def authenticate_password
19
16
  if current_user.nil?
20
17
  return true
@@ -25,15 +22,18 @@ class Api::V1::ApiController < ::ActionController::API
25
22
  return false
26
23
  end
27
24
  end
25
+ <% end %>
28
26
 
29
27
  def set_hash
30
28
  #["current_owner","current_token"] Make this true to check for email also
31
- #@instance_hash = ::Arcadex::Authentication.get_instance(params,request,"Auth-Token")
32
- @instance_hash = ::Arcadex::Authentication.authenticate_owner_with_index(params,request,"Auth-Token","Email","email",true)
29
+ @instance_hash = ::Arcadex::Authentication.get_instance(params,request,"Auth-Token")
30
+ #@instance_hash = ::Arcadex::Authentication.authenticate_owner_with_index(params,request,"Auth-Token","Email","email",true)
33
31
  #Ignore the token if the user's account is locked
32
+ <% if !options.fbonly? %>
34
33
  if !current_user.nil? && current_user.locked
35
34
  @instance_hash = nil
36
35
  end
36
+ <% end %>
37
37
  end
38
38
 
39
39
  def current_user
@@ -1,7 +1,7 @@
1
1
  module Requests
2
2
  module JsonHelpers
3
3
  def json
4
- @json ||= JSON.parse(response.body)
4
+ @json = JSON.parse(response.body)
5
5
  end
6
6
  end
7
7
  end
@@ -1,3 +1,3 @@
1
1
  module JsonVoorhees
2
- VERSION = "1.3.2"
2
+ VERSION = "1.4.0"
3
3
  end
@@ -1,5 +1,7 @@
1
1
  Go to the test directory and run this script. Make sure rspec tests are green.
2
2
 
3
+ For non FB only
4
+
3
5
  cp fresh_copy/test_app . &&
4
6
  cd test_app &&
5
7
  rails g json_voorhees:setup_app &&
@@ -11,8 +13,24 @@ rake db:migrate RAILS_ENV=test &&
11
13
  rake db:migrate RAILS_ENV=production &&
12
14
  rspec
13
15
 
16
+ For FB only
17
+
18
+ cp fresh_copy/test_app test_app2 &&
19
+ cd test_app2 &&
20
+ rails g json_voorhees:setup_app --fbonly &&
21
+ rails g json_voorhees:create_engine ocean &&
22
+ rails g json_voorhees:massive_scaffold ocean atlantic 1 cold:boolean hot:boolean &&
23
+ rake railties:install:migrations &&
24
+ rake db:migrate RAILS_ENV=development &&
25
+ rake db:migrate RAILS_ENV=test &&
26
+ rake db:migrate RAILS_ENV=production &&
27
+ rspec
28
+
14
29
  Remember to delete the copied test_app before commiting!
15
30
  From the test/test_app directory
16
31
 
17
32
  cd .. &&
18
33
  rm -rf test_app
34
+
35
+ cd .. &&
36
+ rm -rf test_app2
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: json_voorhees
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.3.2
4
+ version: 1.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Cleophus Robinson
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-10-26 00:00:00.000000000 Z
11
+ date: 2014-10-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -78,6 +78,7 @@ files:
78
78
  - lib/generators/json_voorhees/app_make_tests/USAGE
79
79
  - lib/generators/json_voorhees/app_make_tests/app_make_tests_generator.rb
80
80
  - lib/generators/json_voorhees/app_make_tests/templates/factory.rb.erb
81
+ - lib/generators/json_voorhees/app_make_tests/templates/fbonly_request.rb.erb
81
82
  - lib/generators/json_voorhees/app_make_tests/templates/model.rb.erb
82
83
  - lib/generators/json_voorhees/app_make_tests/templates/request.rb.erb
83
84
  - lib/generators/json_voorhees/app_make_tests/templates/routing.rb.erb