appi 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.
Files changed (51) hide show
  1. checksums.yaml +7 -0
  2. data/MIT-LICENSE +20 -0
  3. data/Rakefile +34 -0
  4. data/app/controllers/concerns/appi/current_user.rb +33 -0
  5. data/app/controllers/concerns/appi/filters_resources.rb +22 -0
  6. data/app/controllers/concerns/appi/handles_resources.rb +77 -0
  7. data/app/controllers/concerns/appi/renders_exceptions.rb +8 -0
  8. data/app/controllers/concerns/appi/serves_apps_from_redis.rb +32 -0
  9. data/app/controllers/concerns/appi/user_token_controller.rb +31 -0
  10. data/lib/appi.rb +8 -0
  11. data/lib/appi/exception.rb +51 -0
  12. data/lib/appi/token_util.rb +17 -0
  13. data/lib/appi/version.rb +3 -0
  14. data/lib/tasks/appi_tasks.rake +4 -0
  15. data/test/appi_test.rb +7 -0
  16. data/test/dummy/README.rdoc +28 -0
  17. data/test/dummy/Rakefile +6 -0
  18. data/test/dummy/app/assets/javascripts/application.js +13 -0
  19. data/test/dummy/app/assets/stylesheets/application.css +15 -0
  20. data/test/dummy/app/controllers/application_controller.rb +5 -0
  21. data/test/dummy/app/helpers/application_helper.rb +2 -0
  22. data/test/dummy/app/views/layouts/application.html.erb +14 -0
  23. data/test/dummy/bin/bundle +3 -0
  24. data/test/dummy/bin/rails +4 -0
  25. data/test/dummy/bin/rake +4 -0
  26. data/test/dummy/bin/setup +29 -0
  27. data/test/dummy/config.ru +4 -0
  28. data/test/dummy/config/application.rb +26 -0
  29. data/test/dummy/config/boot.rb +5 -0
  30. data/test/dummy/config/database.yml +25 -0
  31. data/test/dummy/config/environment.rb +5 -0
  32. data/test/dummy/config/environments/development.rb +41 -0
  33. data/test/dummy/config/environments/production.rb +79 -0
  34. data/test/dummy/config/environments/test.rb +42 -0
  35. data/test/dummy/config/initializers/assets.rb +11 -0
  36. data/test/dummy/config/initializers/backtrace_silencers.rb +7 -0
  37. data/test/dummy/config/initializers/cookies_serializer.rb +3 -0
  38. data/test/dummy/config/initializers/filter_parameter_logging.rb +4 -0
  39. data/test/dummy/config/initializers/inflections.rb +16 -0
  40. data/test/dummy/config/initializers/mime_types.rb +4 -0
  41. data/test/dummy/config/initializers/session_store.rb +3 -0
  42. data/test/dummy/config/initializers/wrap_parameters.rb +14 -0
  43. data/test/dummy/config/locales/en.yml +23 -0
  44. data/test/dummy/config/routes.rb +56 -0
  45. data/test/dummy/config/secrets.yml +22 -0
  46. data/test/dummy/public/404.html +67 -0
  47. data/test/dummy/public/422.html +67 -0
  48. data/test/dummy/public/500.html +66 -0
  49. data/test/dummy/public/favicon.ico +0 -0
  50. data/test/test_helper.rb +19 -0
  51. metadata +191 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: fdaca731bdb6a7c91436f6ae0a1ae395beded4ee
4
+ data.tar.gz: a4fc12e9c446c1f1ede1147e3f865cac92633400
5
+ SHA512:
6
+ metadata.gz: d13b332b348093b988fee24c4d6b83a1427423d6bf71752d2ea215b6e5cd9b867009fe4a69bf88622cc650701b5e65a2f11d1b783b1edb147b2de9b39525ffa9
7
+ data.tar.gz: 0c5c1f6c1c4a37edb9f09d05ad236f08f5f2fa6cc5e447bbbc072380ee1fc0da48718983ce6d4059b82288baa8f7aec5f2de7cebdec5a5b4869ee5dc6b4425e8
@@ -0,0 +1,20 @@
1
+ Copyright 2015 Hugh Francis
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,34 @@
1
+ begin
2
+ require 'bundler/setup'
3
+ rescue LoadError
4
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
5
+ end
6
+
7
+ require 'rdoc/task'
8
+
9
+ RDoc::Task.new(:rdoc) do |rdoc|
10
+ rdoc.rdoc_dir = 'rdoc'
11
+ rdoc.title = 'Appi'
12
+ rdoc.options << '--line-numbers'
13
+ rdoc.rdoc_files.include('README.rdoc')
14
+ rdoc.rdoc_files.include('lib/**/*.rb')
15
+ end
16
+
17
+
18
+
19
+
20
+
21
+
22
+ Bundler::GemHelper.install_tasks
23
+
24
+ require 'rake/testtask'
25
+
26
+ Rake::TestTask.new(:test) do |t|
27
+ t.libs << 'lib'
28
+ t.libs << 'test'
29
+ t.pattern = 'test/**/*_test.rb'
30
+ t.verbose = false
31
+ end
32
+
33
+
34
+ task default: :test
@@ -0,0 +1,33 @@
1
+ module APPI
2
+ module CurrentUser
3
+ extend ActiveSupport::Concern
4
+
5
+ protected
6
+
7
+ def current_user
8
+ @current_user ||= set_current_user
9
+ end
10
+
11
+ def set_current_user
12
+ if request_token
13
+ @current_user = User.find requester_params[:id]
14
+ else
15
+ @current_user = User.new
16
+ end
17
+ end
18
+
19
+ def request_token
20
+ auth_header = headers['Authorization'] || env['HTTP_AUTHORIZATION']
21
+
22
+ if auth_header.present?
23
+ return auth_header.split.last
24
+ else
25
+ nil
26
+ end
27
+ end
28
+
29
+ def requester_params
30
+ APPI::TokenUtil.decode(request_token) if request_token
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,22 @@
1
+ module APPI
2
+ module FiltersResources
3
+ extend ActiveSupport::Concern
4
+
5
+ protected
6
+
7
+ def permitted_filter_params
8
+ []
9
+ end
10
+
11
+ def apply_filter_params(collection)
12
+ permitted_filter_params.each do |param|
13
+ if params[param]
14
+ values = params[param].split '|'
15
+ collection = collection.select { |item| values.include? item.try(:status) }
16
+ end
17
+ end
18
+ collection
19
+ end
20
+
21
+ end
22
+ end
@@ -0,0 +1,77 @@
1
+ module APPI
2
+ module HandlesResources
3
+ extend ActiveSupport::Concern
4
+
5
+ def find_in_params(key, obj=params)
6
+ if obj.respond_to?(:key?) && obj.key?(key)
7
+ found = obj[key]
8
+
9
+ if found.is_a?(Hash) && found.has_key?(:data)
10
+ found[:data]
11
+ else
12
+ found
13
+ end
14
+ elsif obj.respond_to?(:each)
15
+ r = nil
16
+ obj.find{ |*a| r=find_in_params(key, a.last) }
17
+ r
18
+ end
19
+ end
20
+
21
+ def extract_ids(array)
22
+ result = []
23
+ result = array.map{ |item| item[:id].to_i }.sort if array
24
+ result
25
+ end
26
+
27
+ protected
28
+
29
+ def resource
30
+ @resource ||= (@resource = find_resource)
31
+ end
32
+
33
+ def find_resource
34
+ if params[:id]
35
+ resource_class.find params[:id]
36
+ elsif params[:slug]
37
+ # TODO: Make this more friendly aware
38
+ resource_class.friendly.find params[:slug]
39
+ else
40
+ nil
41
+ end
42
+ end
43
+
44
+ def resource_class
45
+ controller_name.classify.constantize
46
+ end
47
+
48
+ def permitted_attributes
49
+ []
50
+ end
51
+
52
+ def permitted_relationships
53
+ []
54
+ end
55
+
56
+ def resource_params
57
+ attributes = find_in_params(:attributes).try(:permit, permitted_attributes) || {}
58
+ raw_relationships = find_in_params(:relationships) || {}
59
+ relationships = {}
60
+
61
+ raw_relationships.each_key do |key|
62
+ data = raw_relationships.delete(key)[:data]
63
+
64
+ if permitted_relationships.include?(key.to_sym) && data
65
+ if data.kind_of?(Array)
66
+ relationships["#{key.singularize}_ids"] = extract_ids data
67
+ else
68
+ relationships["#{key}_id"] = data[:id]
69
+ end
70
+ end
71
+ end
72
+
73
+ attributes.merge relationships
74
+ end
75
+
76
+ end
77
+ end
@@ -0,0 +1,8 @@
1
+ module APPI
2
+ module RendersExceptions
3
+ extend ActiveSupport::Concern
4
+ def render_appi_exception(exception)
5
+ render json: exception.as_json, status: exception.status.to_sym
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,32 @@
1
+ module APPI
2
+ module ServesAppsFromRedis
3
+ extend ActiveSupport::Concern
4
+
5
+ def serve_index
6
+ app_name = params[:app_name] || 'app'
7
+ revision = params[:revision]
8
+
9
+ if needs_trailing_slash?
10
+ redirect_to url_for(params.merge(trailing_slash: true)), status: 301 and return
11
+ end
12
+
13
+ index_key = if revision
14
+ "#{app_name}:#{revision}"
15
+ else
16
+ Sidekiq.redis { |r| r.get("#{app_name}:current") }
17
+ end
18
+ index = Sidekiq.redis { |r| r.get(index_key) } || "Index file not found."
19
+ render text: index.html_safe, layout: false
20
+ end
21
+
22
+ private
23
+
24
+ def needs_trailing_slash?
25
+ if params[:path]
26
+ false
27
+ else
28
+ request.env['REQUEST_URI'].match(/[^\?]+/).to_s.last != '/' && params[:trailing_slash_at_root]
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,31 @@
1
+ module APPI
2
+ module UserTokenController
3
+ extend ActiveSupport::Concern
4
+
5
+ def token
6
+ user = User.find_by(email: token_params[:email])
7
+ raise APPI::Exception.new("auth.email_does_not_exist", {
8
+ email: token_params[:email]
9
+ }) unless user
10
+
11
+ if user.authenticate token_params[:password]
12
+ token = APPI::TokenUtil.encode({
13
+ id: user.id,
14
+ email: user.email
15
+ })
16
+ render json: { token: token }, status: :ok
17
+ else
18
+ raise APPI::Exception.new "auth.incorrect_password", email: token_params[:email]
19
+ end
20
+ end
21
+
22
+ protected
23
+ def token_params
24
+ raise APPI::Exception.new("auth.bad_request", { attr_name: "email" }) unless params[:email]
25
+ raise APPI::Exception.new("auth.email_is_invalid", { invalid_email: params[:email] }) unless EmailValidator.valid?(params[:email])
26
+ raise APPI::Exception.new("auth.bad_request", { attr_name: "password" }) unless params[:password]
27
+ params.permit :email, :password
28
+ end
29
+
30
+ end
31
+ end
@@ -0,0 +1,8 @@
1
+ require 'appi/exception'
2
+ require 'appi/token_util'
3
+
4
+ module APPI
5
+ class Engine < ::Rails::Engine
6
+
7
+ end
8
+ end
@@ -0,0 +1,51 @@
1
+ module APPI
2
+ class Exception < ::Exception
3
+ attr_accessor :details
4
+ attr_accessor :code
5
+ attr_accessor :validations
6
+
7
+ attr_accessor :status
8
+ attr_accessor :title
9
+ attr_accessor :detail
10
+
11
+ def initialize(code, details={}, validations={})
12
+ @details = details
13
+ @code = code
14
+ @validations = validations
15
+
16
+ @status = I18n.t "appi_exceptions.#{code}.status"
17
+ @title = I18n.t "appi_exceptions.#{code}.title", details
18
+ @detail = I18n.t "appi_exceptions.#{code}.detail", details
19
+
20
+ @message = @detail
21
+ end
22
+
23
+ def validation_array
24
+ array = []
25
+ @validations.to_hash.each_key do |key|
26
+ array << {
27
+ title: @validations[key].uniq.join(', '),
28
+ source: {
29
+ pointer: "data/attributes/#{key}"
30
+ }
31
+ }
32
+ end
33
+ array
34
+ end
35
+
36
+ def base_error
37
+ {
38
+ title: title,
39
+ detail: detail,
40
+ code: code,
41
+ status: status
42
+ }
43
+ end
44
+
45
+ def as_json
46
+ {
47
+ errors: validation_array.prepend(base_error)
48
+ }
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,17 @@
1
+ module APPI
2
+ class TokenUtil
3
+ class << self
4
+ def encode(payload, exp=2.weeks.from_now)
5
+ payload[:exp] = exp.to_i
6
+ JWT.encode(payload, Rails.application.secrets.secret_key_base)
7
+ end
8
+
9
+ def decode(token)
10
+ body = JWT.decode(token, Rails.application.secrets.secret_key_base)[0]
11
+ HashWithIndifferentAccess.new body
12
+ rescue
13
+ nil
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,3 @@
1
+ module APPI
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :appi do
3
+ # # Task goes here
4
+ # end
@@ -0,0 +1,7 @@
1
+ require 'test_helper'
2
+
3
+ class APPITest < ActiveSupport::TestCase
4
+ test "truth" do
5
+ assert_kind_of Module, APPI
6
+ end
7
+ end
@@ -0,0 +1,28 @@
1
+ == README
2
+
3
+ This README would normally document whatever steps are necessary to get the
4
+ application up and running.
5
+
6
+ Things you may want to cover:
7
+
8
+ * Ruby version
9
+
10
+ * System dependencies
11
+
12
+ * Configuration
13
+
14
+ * Database creation
15
+
16
+ * Database initialization
17
+
18
+ * How to run the test suite
19
+
20
+ * Services (job queues, cache servers, search engines, etc.)
21
+
22
+ * Deployment instructions
23
+
24
+ * ...
25
+
26
+
27
+ Please feel free to use a different markup language if you do not plan to run
28
+ <tt>rake doc:app</tt>.
@@ -0,0 +1,6 @@
1
+ # Add your own tasks in files placed in lib/tasks ending in .rake,
2
+ # for example lib/tasks/capistrano.rake, and they will automatically be available to Rake.
3
+
4
+ require File.expand_path('../config/application', __FILE__)
5
+
6
+ Rails.application.load_tasks
@@ -0,0 +1,13 @@
1
+ // This is a manifest file that'll be compiled into application.js, which will include all the files
2
+ // listed below.
3
+ //
4
+ // Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
5
+ // or any plugin's vendor/assets/javascripts directory can be referenced here using a relative path.
6
+ //
7
+ // It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
8
+ // compiled file.
9
+ //
10
+ // Read Sprockets README (https://github.com/rails/sprockets#sprockets-directives) for details
11
+ // about supported directives.
12
+ //
13
+ //= require_tree .
@@ -0,0 +1,15 @@
1
+ /*
2
+ * This is a manifest file that'll be compiled into application.css, which will include all the files
3
+ * listed below.
4
+ *
5
+ * Any CSS and SCSS file within this directory, lib/assets/stylesheets, vendor/assets/stylesheets,
6
+ * or any plugin's vendor/assets/stylesheets directory can be referenced here using a relative path.
7
+ *
8
+ * You're free to add application-wide styles to this file and they'll appear at the bottom of the
9
+ * compiled file so the styles you add here take precedence over styles defined in any styles
10
+ * defined in the other CSS/SCSS files in this directory. It is generally better to create a new
11
+ * file per style scope.
12
+ *
13
+ *= require_tree .
14
+ *= require_self
15
+ */