tramway-api 0.2.1.1 → 0.3

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 03fbedd30fa36978033c8708ebaeb33aa299d421d20579af7c79f5a93c7a45b2
4
- data.tar.gz: 6ad47c29de3d35109b6a0be88ef265c1a8d805eb1044e8b4826b12ba3e178b5f
3
+ metadata.gz: ad4e0d429fcc05fb7951b00a8aedb0718024120f9b571da62a192a516620fd95
4
+ data.tar.gz: ccd377146811d086e240f75c7f0d1882241024fe866fcf85205a0d71fd04e48d
5
5
  SHA512:
6
- metadata.gz: 6e7353971838d17ec2c0bb1dddbaa12afe1d5273ced54e1c663ca7e9039a70829fb01d0e1a5e343bdcf673ce8d7911e4d57db04a5b36834304c3e3d1fe483166
7
- data.tar.gz: 35bc486bf678c29fba6d53c5bf9053e5c7bb591ff2bfdb48309864eac44cd058577f66c936c5f30ec22c843fae8ff69332849bbb654814271bfe1cf7c41c45cd
6
+ metadata.gz: 8ba680817e74595d11adcb2eeaf0b19e06aa6f124a7c813ed799ec7c0670b8d339b9ddc25f6f74ade19d9bf5cafc73bfd35709424b950f40efa277370e253914
7
+ data.tar.gz: 626d06a3a3dbc3937f0b2da410b7c45437c90da4a708480e44e3a2c2e735234f34f67e71eca5f0357230afae57014edaa88811170182a3b79105cfec86ccb7e9
@@ -1,10 +1,7 @@
1
- require 'tramway/api/authenticate_helper'
2
-
3
1
  module Tramway
4
2
  module Api
5
3
  class ApplicationController < ::Tramway::Core::ApplicationController
6
4
  include Knock::Authenticable
7
- include ::Tramway::Api::AuthenticateHelper
8
5
  protect_from_forgery with: :null_session, if: proc { |c| c.request.format == 'application/json' }
9
6
  rescue_from ActiveRecord::RecordNotFound, with: :not_found
10
7
 
@@ -13,6 +10,33 @@ module Tramway
13
10
  def not_found
14
11
  render json: { data: [] }, status: :not_found
15
12
  end
13
+
14
+ protected
15
+
16
+ def authenticate
17
+ raise ActiveRecord::RecordNotFound unless entity.present? && entity.authenticate(auth_params[:password])
18
+ end
19
+
20
+ def auth_token
21
+ if entity.respond_to? :to_token_payload
22
+ Knock::AuthToken.new payload: entity.to_token_payload
23
+ else
24
+ Knock::AuthToken.new payload: { sub: entity.id }
25
+ end
26
+ end
27
+
28
+ def entity
29
+ @entity ||=
30
+ if Tramway::Api.user_based_model.respond_to? :from_token_request
31
+ Tramway::Api.user_based_model.from_token_request request
32
+ else
33
+ Tramway::Api.user_based_model.find_by email: auth_params[:email]
34
+ end
35
+ end
36
+
37
+ def auth_params
38
+ params.require(:auth).permit :email, :password
39
+ end
16
40
  end
17
41
  end
18
42
  end
@@ -6,13 +6,12 @@ class Tramway::Api::V1::UsersController < ::Tramway::Api::V1::ApplicationControl
6
6
 
7
7
  def create
8
8
  user_form = form_class_name(Tramway::Api.user_based_model).new Tramway::Api.user_based_model.new
9
- if user_form.validate params[Tramway::Api.user_based_model.name.underscore]
10
- user_form.save
9
+ if user_form.submit params[Tramway::Api.user_based_model.name.underscore]
11
10
  token = ::Knock::AuthToken.new(payload: { sub: user_form.model.id }).token
12
11
  serialized_user = OpenStruct.new user_form.model.attributes.merge authentication_token: token
13
12
  render json: serialized_user, status: :created
14
13
  else
15
- render json: user_form.errors.details, status: :unprocessable_entity
14
+ render json: user_form.errors.messages, status: :unprocessable_entity
16
15
  end
17
16
  end
18
17
 
@@ -1,6 +1,6 @@
1
1
  Tramway::Api::Engine.routes.draw do
2
2
  namespace :v1 do
3
3
  resource :user_token, only: [ :create ]
4
- resources :users, only: [ :create ]
4
+ resource :user, only: [ :create, :show ]
5
5
  end
6
6
  end
@@ -1,5 +1,5 @@
1
1
  module Tramway
2
2
  module Api
3
- VERSION = '0.2.1.1'
3
+ VERSION = '0.3'
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tramway-api
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1.1
4
+ version: '0.3'
5
5
  platform: ruby
6
6
  authors:
7
7
  - Pavel Kalashnikov
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-02-18 00:00:00.000000000 Z
11
+ date: 2019-02-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: knock
@@ -62,7 +62,6 @@ files:
62
62
  - config/routes.rb
63
63
  - lib/tasks/tramway/api_tasks.rake
64
64
  - lib/tramway/api.rb
65
- - lib/tramway/api/authenticate_helper.rb
66
65
  - lib/tramway/api/engine.rb
67
66
  - lib/tramway/api/version.rb
68
67
  homepage: https://github.com/kalashnikovisme/tramway-dev
@@ -1,26 +0,0 @@
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