tramway-api 0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: a9e653ce900aa4bdf9e55be5f193cee92778d4850ce5893b1189cad872d9692d
4
+ data.tar.gz: 4047b383d98729bff0a0181ff7194a4378f5b429358e06d88da02255914b3e70
5
+ SHA512:
6
+ metadata.gz: 614e33c8a1e87e8b19c293594594274f3cba429d96cb344162586beb79e3b930a0d3d52f9f661d2378c91f39c175ccc1fc7e2a90410c3e2e1f86fb3fdb555325
7
+ data.tar.gz: f9d5292121d7f9881dee7c2a761aa72230ebc3a2f1bd0ce0b21c91268730e2fabd3d04d335285fe2d8e80a9f2d1b7fb1d5058bcf94e046e33323d78baa8db67e
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2019 Pavel Kalashnikov
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.
data/README.md ADDED
@@ -0,0 +1,28 @@
1
+ # Tramway::Api
2
+ Short description and motivation.
3
+
4
+ ## Usage
5
+ How to use my plugin.
6
+
7
+ ## Installation
8
+ Add this line to your application's Gemfile:
9
+
10
+ ```ruby
11
+ gem 'tramway-api'
12
+ ```
13
+
14
+ And then execute:
15
+ ```bash
16
+ $ bundle
17
+ ```
18
+
19
+ Or install it yourself as:
20
+ ```bash
21
+ $ gem install tramway-api
22
+ ```
23
+
24
+ ## Contributing
25
+ Contribution directions go here.
26
+
27
+ ## License
28
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,32 @@
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 = 'Tramway::Api'
12
+ rdoc.options << '--line-numbers'
13
+ rdoc.rdoc_files.include('README.md')
14
+ rdoc.rdoc_files.include('lib/**/*.rb')
15
+ end
16
+
17
+ APP_RAKEFILE = File.expand_path("spec/dummy/Rakefile", __dir__)
18
+ load 'rails/tasks/engine.rake'
19
+
20
+ load 'rails/tasks/statistics.rake'
21
+
22
+ require 'bundler/gem_tasks'
23
+
24
+ require 'rake/testtask'
25
+
26
+ Rake::TestTask.new(:test) do |t|
27
+ t.libs << 'test'
28
+ t.pattern = 'test/**/*_test.rb'
29
+ t.verbose = false
30
+ end
31
+
32
+ task default: :test
@@ -0,0 +1,2 @@
1
+ //= link_directory ../javascripts/tramway/api .js
2
+ //= link_directory ../stylesheets/tramway/api .css
@@ -0,0 +1,15 @@
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. JavaScript code in this file should be added after the last require_* statement.
9
+ //
10
+ // Read Sprockets README (https://github.com/rails/sprockets#sprockets-directives) for details
11
+ // about supported directives.
12
+ //
13
+ //= require rails-ujs
14
+ //= require activestorage
15
+ //= 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 other CSS/SCSS
10
+ * files in this directory. Styles in this file should be added after the last require_* statement.
11
+ * It is generally better to create a new file per style scope.
12
+ *
13
+ *= require_tree .
14
+ *= require_self
15
+ */
@@ -0,0 +1,16 @@
1
+ module Tramway
2
+ module Api
3
+ class ApplicationController < ::Tramway::Core::ApplicationController
4
+ include Knock::Authenticable
5
+ include AuthenticateHelper
6
+ protect_from_forgery with: :null_session, if: proc { |c| c.request.format == 'application/json' }
7
+ rescue_from ActiveRecord::RecordNotFound, with: :not_found
8
+
9
+ private
10
+
11
+ def not_found
12
+ render json: { data: [] }, status: :not_found
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,8 @@
1
+ module Tramway
2
+ module Api
3
+ module V1
4
+ class ApplicationController < ::Tramway::Api::ApplicationController
5
+ end
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,11 @@
1
+ module Tramway::Api::V1
2
+ class RecordsController < ::Tramway::Api::V1::ApplicationController
3
+ def index
4
+ records = model_class.active.order(id: :desc).send params[:scope] || :all
5
+ render json: records,
6
+ meta: meta_pagination(records),
7
+ each_serializer: serializer,
8
+ status: :ok
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+ require 'tramway/api/authenticate_helper'
3
+
4
+ class Tramway::Api::V1::UserTokensController < ::Tramway::Api::V1::ApplicationController
5
+ before_action :authenticate
6
+ include Tramway::Api::AuthenticateHelper
7
+
8
+ def create
9
+ token = auth_token
10
+ render json: {
11
+ auth_token: token,
12
+ user: {
13
+ email: @entity.email
14
+ }
15
+ }, status: :created
16
+ end
17
+ end
@@ -0,0 +1,22 @@
1
+ require 'tramway/helpers/class_name_helpers'
2
+
3
+ class Tramway::Api::V1::UsersController < ::Tramway::Api::V1::ApplicationController
4
+ before_action :authenticate_user, only: :show
5
+ include Tramway::ClassNameHelpers
6
+
7
+ def create
8
+ user_form = form_class_name(Tramway::Api.user_based_model).new Tramway::Api.user_based_model.new
9
+ if user_form.validate params
10
+ user_form.save
11
+ token = ::Knock::AuthToken.new(payload: { sub: user_form.model.id }).token
12
+ serialized_user = OpenStruct.new user_form.model.attributes.merge authentication_token: token
13
+ render json: serialized_user, status: :created
14
+ else
15
+ render json: user_form.errors.details, status: :unprocessable_entity
16
+ end
17
+ end
18
+
19
+ def show
20
+ render json: current_user, status: :ok
21
+ end
22
+ end
@@ -0,0 +1,3 @@
1
+ class Tramway::Api::AuthenticationForm < Tramway::Core::ApplicationForm
2
+ properties :email, :password
3
+ end
@@ -0,0 +1,6 @@
1
+ module Tramway
2
+ module Api
3
+ module ApplicationHelper
4
+ end
5
+ end
6
+ end
@@ -0,0 +1,6 @@
1
+ module Tramway
2
+ module Api
3
+ class ApplicationJob < ActiveJob::Base
4
+ end
5
+ end
6
+ end
@@ -0,0 +1,8 @@
1
+ module Tramway
2
+ module Api
3
+ class ApplicationMailer < ActionMailer::Base
4
+ default from: 'from@example.com'
5
+ layout 'mailer'
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,7 @@
1
+ module Tramway
2
+ module Api
3
+ class ApplicationRecord < ActiveRecord::Base
4
+ self.abstract_class = true
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,16 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>Tramway api</title>
5
+ <%= csrf_meta_tags %>
6
+ <%= csp_meta_tag %>
7
+
8
+ <%= stylesheet_link_tag "tramway/api/application", media: "all" %>
9
+ <%= javascript_include_tag "tramway/api/application" %>
10
+ </head>
11
+ <body>
12
+
13
+ <%= yield %>
14
+
15
+ </body>
16
+ </html>
data/config/routes.rb ADDED
@@ -0,0 +1,6 @@
1
+ Tramway::Api::Engine.routes.draw do
2
+ namespace :v1 do
3
+ resource :user_token, only: [ :create ]
4
+ resources :users, only: [ :create ]
5
+ end
6
+ end
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :tramway_api do
3
+ # # Task goes here
4
+ # end
@@ -0,0 +1,26 @@
1
+ module Tramway::Api::AuthenticateHelper
2
+ def authenticate
3
+ raise ActiveRecord::RecordNotFound unless entity.present? && entity.authenticate(auth_params[:password])
4
+ end
5
+
6
+ def auth_token
7
+ if entity.respond_to? :to_token_payload
8
+ Knock::AuthToken.new payload: entity.to_token_payload
9
+ else
10
+ Knock::AuthToken.new payload: { sub: entity.id }
11
+ end
12
+ end
13
+
14
+ def entity
15
+ @entity ||=
16
+ if Tramway::Api.user_based_model.respond_to? :from_token_request
17
+ Tramway::Api.user_based_model.from_token_request request
18
+ else
19
+ Tramway::Api.user_based_model.find_by email: auth_params[:email]
20
+ end
21
+ end
22
+
23
+ def auth_params
24
+ params.require(:auth).permit :email, :password
25
+ end
26
+ end
@@ -0,0 +1,7 @@
1
+ module Tramway
2
+ module Api
3
+ class Engine < ::Rails::Engine
4
+ isolate_namespace Tramway::Api
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,5 @@
1
+ module Tramway
2
+ module Api
3
+ VERSION = '0.2'
4
+ end
5
+ end
@@ -0,0 +1,15 @@
1
+ require 'tramway/api/engine'
2
+
3
+ module Tramway
4
+ module Api
5
+ class << self
6
+ def user_based_model
7
+ @@user_based_model ||= ::Tramway::User::User
8
+ end
9
+
10
+ def user_based_model=(model_class)
11
+ @@user_based_model = model_class
12
+ end
13
+ end
14
+ end
15
+ end
metadata ADDED
@@ -0,0 +1,80 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: tramway-api
3
+ version: !ruby/object:Gem::Version
4
+ version: '0.2'
5
+ platform: ruby
6
+ authors:
7
+ - Pavel Kalashnikov
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2019-02-14 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: knock
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
+ description: Engine for api
28
+ email:
29
+ - kalashnikovisme@gmail.com
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - MIT-LICENSE
35
+ - README.md
36
+ - Rakefile
37
+ - app/assets/config/tramway_api_manifest.js
38
+ - app/assets/javascripts/tramway/api/application.js
39
+ - app/assets/stylesheets/tramway/api/application.css
40
+ - app/controllers/tramway/api/application_controller.rb
41
+ - app/controllers/tramway/api/v1/application_controller.rb
42
+ - app/controllers/tramway/api/v1/records_controller.rb
43
+ - app/controllers/tramway/api/v1/user_tokens_controller.rb
44
+ - app/controllers/tramway/api/v1/users_controller.rb
45
+ - app/forms/tramway/api/authentication_form.rb
46
+ - app/helpers/tramway/api/application_helper.rb
47
+ - app/jobs/tramway/api/application_job.rb
48
+ - app/mailers/tramway/api/application_mailer.rb
49
+ - app/models/tramway/api/application_record.rb
50
+ - app/views/layouts/tramway/api/application.html.erb
51
+ - config/routes.rb
52
+ - lib/tasks/tramway/api_tasks.rake
53
+ - lib/tramway/api.rb
54
+ - lib/tramway/api/authenticate_helper.rb
55
+ - lib/tramway/api/engine.rb
56
+ - lib/tramway/api/version.rb
57
+ homepage: https://github.com/kalashnikovisme/tramway-dev
58
+ licenses: []
59
+ metadata: {}
60
+ post_install_message:
61
+ rdoc_options: []
62
+ require_paths:
63
+ - lib
64
+ required_ruby_version: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ required_rubygems_version: !ruby/object:Gem::Requirement
70
+ requirements:
71
+ - - ">="
72
+ - !ruby/object:Gem::Version
73
+ version: '0'
74
+ requirements: []
75
+ rubyforge_project:
76
+ rubygems_version: 2.7.6
77
+ signing_key:
78
+ specification_version: 4
79
+ summary: Engine for api
80
+ test_files: []