petergate_api 0.1.0
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 +7 -0
- data/.gitignore +9 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +41 -0
- data/Rakefile +2 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/generators/petergate_api/install_generator.rb +54 -0
- data/lib/generators/petergate_api/templates/app/controllers/api/base_controller.rb +18 -0
- data/lib/generators/petergate_api/templates/app/controllers/api/v1/auth_controller.rb +55 -0
- data/lib/generators/petergate_api/templates/app/models/api/connection.rb +22 -0
- data/lib/generators/petergate_api/templates/initializers/apipie.rb +8 -0
- data/lib/generators/petergate_api/templates/migrations/add_mobile_reset_token_to_user.rb +5 -0
- data/lib/generators/petergate_api/templates/migrations/create_api_connections.rb +12 -0
- data/lib/petergate_api/version.rb +3 -0
- data/lib/petergate_api.rb +5 -0
- data/petergate_api.gemspec +24 -0
- metadata +89 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: b3f2c4bcba9ad480688333f1d795894cab47f577
|
4
|
+
data.tar.gz: 77185c0c2aa0c9210ed34858e7c9a33a6b3db74e
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 798d6186dadf32ee16f95a730d144e6b1e6a3c76df45e442dfaa6e9ef8f6eb22cf8d38eba9b3d046de8b8ee6993575b674a88e9ede18246c485449ecea698730
|
7
|
+
data.tar.gz: 9c46b90b9718a720bc4ed7d72e9a62e877a55d566e80bd345db172be9e3ab4dc63f54146dc920133fb1b083da71b5bc03d703f7e94f3f738575aa8fb5a6dffb8
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2016 Isaac Sloan
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,41 @@
|
|
1
|
+
# PetergateApi
|
2
|
+
|
3
|
+
Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/petergate_api`. To experiment with that code, run `bin/console` for an interactive prompt.
|
4
|
+
|
5
|
+
TODO: Delete this and the text above, and describe your gem
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem 'petergate_api'
|
13
|
+
```
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install petergate_api
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
|
25
|
+
TODO: Write usage instructions here
|
26
|
+
|
27
|
+
## Development
|
28
|
+
|
29
|
+
After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
30
|
+
|
31
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
32
|
+
|
33
|
+
## Contributing
|
34
|
+
|
35
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/petergate_api.
|
36
|
+
|
37
|
+
|
38
|
+
## License
|
39
|
+
|
40
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
41
|
+
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "petergate_api"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start
|
data/bin/setup
ADDED
@@ -0,0 +1,54 @@
|
|
1
|
+
require 'securerandom'
|
2
|
+
|
3
|
+
module PetergateApi
|
4
|
+
module Generators
|
5
|
+
class InstallGenerator < Rails::Generators::Base
|
6
|
+
include Rails::Generators::Migration
|
7
|
+
source_root File.expand_path("../templates", __FILE__)
|
8
|
+
class_option :orm
|
9
|
+
|
10
|
+
desc "Sets up rails project for Petergate API"
|
11
|
+
def self.next_migration_number(path)
|
12
|
+
sleep 1
|
13
|
+
Time.now.utc.strftime("%Y%m%d%H%M%S")
|
14
|
+
end
|
15
|
+
|
16
|
+
def add_to_gemfile
|
17
|
+
gem "apipie"
|
18
|
+
end
|
19
|
+
|
20
|
+
def insert_into_user_model
|
21
|
+
inject_into_file "app/models/user.rb", after: /^class\sUser < ActiveRecord::Base/ do
|
22
|
+
<<-'RUBY'
|
23
|
+
|
24
|
+
has_many :api_connections, class_name: "::Api::Connection"
|
25
|
+
|
26
|
+
def set_mobile_reset_token!
|
27
|
+
begin
|
28
|
+
self.mobile_reset_token = Base64.strict_encode64(Devise.friendly_token + self.email).strip
|
29
|
+
end while self.class.find_by(mobile_reset_token: self.mobile_reset_token).present?
|
30
|
+
self.save
|
31
|
+
self.mobile_reset_token
|
32
|
+
end
|
33
|
+
|
34
|
+
RUBY
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
def copy_app
|
39
|
+
run "cp -rf #{self.class.source_root}/app/ app/"
|
40
|
+
end
|
41
|
+
|
42
|
+
def copy_initializers
|
43
|
+
run "cp -rf #{self.class.source_root}/initializers config/"
|
44
|
+
end
|
45
|
+
|
46
|
+
def create_migrations
|
47
|
+
Dir["#{self.class.source_root}/migrations/*.rb"].sort.each do |filepath|
|
48
|
+
name = File.basename(filepath)
|
49
|
+
migration_template "migrations/#{name}", "db/migrate/#{name}"
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
class Api::BaseController < ActionController::Base
|
2
|
+
|
3
|
+
def current_user
|
4
|
+
@user ||= begin
|
5
|
+
if (@connection = Api::Connection.find_by(token: request.headers["Authorization"])).present?
|
6
|
+
@user = @connection.user
|
7
|
+
else
|
8
|
+
nil
|
9
|
+
end
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
def current_city
|
14
|
+
@current_city ||= current_user.try(:city)
|
15
|
+
end
|
16
|
+
|
17
|
+
helper_method :current_user, :current_city
|
18
|
+
end
|
@@ -0,0 +1,55 @@
|
|
1
|
+
class Api::V1::AuthController < Api::BaseController
|
2
|
+
access all: {except: [:destroy]}, user: :all
|
3
|
+
|
4
|
+
api :POST, '/v1/auth', "Login:\n\nLogs user in and creates auth token.\nReturned token should be included in Header as (Authorization: 'a29192kj3j2k39etc'})"
|
5
|
+
param :email, String, desc: "email", required: true
|
6
|
+
param :password, String, desc: "password", required: true
|
7
|
+
example "
|
8
|
+
{
|
9
|
+
'id': 10,
|
10
|
+
'auth_token': 'exampletokenofrandomness',
|
11
|
+
'email': 'example@example.com'
|
12
|
+
}"
|
13
|
+
def create
|
14
|
+
if params[:email] && (@user = User.find_by_email(params[:email])) && @user.valid_password?(auth_params[:password])
|
15
|
+
connection = @user.api_connections.create
|
16
|
+
render json: {id: @user.id, auth_token: connection.token, email: @user.email}.to_json, status: :ok
|
17
|
+
else
|
18
|
+
render status: :unauthorized, nothing: true
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
api :DELETE, "/v1/auth", "Logout"
|
23
|
+
def destroy
|
24
|
+
@connection.destroy
|
25
|
+
render status: :ok, json: {message: "connection destroyed"}
|
26
|
+
end
|
27
|
+
|
28
|
+
api :POST, "/v1/forgot_password"
|
29
|
+
param :email, String, required: true
|
30
|
+
def forgot_password
|
31
|
+
user = User.find_by_email(params[:email])
|
32
|
+
user.set_mobile_reset_token!
|
33
|
+
render json: {note: "this token is provided for test purposes", token: user.mobile_reset_token}
|
34
|
+
end
|
35
|
+
|
36
|
+
api :POST, "/v1/change_password"
|
37
|
+
# TODO: allow authorized user with a valid session to change password. 'token' should not be a required field in this case
|
38
|
+
param :token, String, desc: "Token will be sent by email when forgot password is called. (for beta purposes it will also be returned by /forgot_password.)", required: true
|
39
|
+
param :password, String, required: true
|
40
|
+
param :password_confirmation, String, required: true
|
41
|
+
def change_password
|
42
|
+
user = User.find_by(mobile_reset_token: params[:token])
|
43
|
+
if user && user.update(password: params[:password], password_confirmation: params[:password_confirmation], mobile_reset_token: nil)
|
44
|
+
render status: :ok, json: {message: "Password Changed"}
|
45
|
+
else
|
46
|
+
render json: {error: "Incorrect token or missmatched password"}, status: :unprocessable_entity
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
private
|
51
|
+
def auth_params
|
52
|
+
params.permit(:email, :password, :password_confirmation)
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
@@ -0,0 +1,22 @@
|
|
1
|
+
# == Schema Information
|
2
|
+
#
|
3
|
+
# Table name: api_connections
|
4
|
+
#
|
5
|
+
# id :integer not null, primary key
|
6
|
+
# user_id :integer
|
7
|
+
# token :string(255)
|
8
|
+
# meta_data :text(65535)
|
9
|
+
# device_type :string(255)
|
10
|
+
# created_at :datetime not null
|
11
|
+
# updated_at :datetime not null
|
12
|
+
#
|
13
|
+
|
14
|
+
class Api::Connection < ActiveRecord::Base
|
15
|
+
belongs_to :user, class_name: "::User"
|
16
|
+
before_create do
|
17
|
+
begin
|
18
|
+
self.token = Base64.strict_encode64(Devise.friendly_token + user.email).strip
|
19
|
+
end while self.class.find_by(token: self.token).present?
|
20
|
+
end
|
21
|
+
scope :push_tokens, -> { where(meta_data: 'push_notifications_token')}
|
22
|
+
end
|
@@ -0,0 +1,8 @@
|
|
1
|
+
Apipie.configure do |config|
|
2
|
+
config.app_name = "AppName"
|
3
|
+
config.validate = false
|
4
|
+
config.api_base_url = "/api"
|
5
|
+
config.doc_base_url = "/developer/docs"
|
6
|
+
# where is your API defined?
|
7
|
+
config.api_controllers_matcher = "#{Rails.root}/app/controllers/**/*.rb"
|
8
|
+
end
|
@@ -0,0 +1,12 @@
|
|
1
|
+
class CreateApiConnections < ActiveRecord::Migration
|
2
|
+
def change
|
3
|
+
create_table :api_connections do |t|
|
4
|
+
t.belongs_to :user, index: true, foreign_key: true
|
5
|
+
t.string :token
|
6
|
+
t.text :meta_data
|
7
|
+
t.string :device_type
|
8
|
+
|
9
|
+
t.timestamps null: false
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'petergate_api/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "petergate_api"
|
8
|
+
spec.version = PetergateApi::VERSION
|
9
|
+
spec.authors = ["Isaac Sloan"]
|
10
|
+
spec.email = ["isaac@isaacsloan.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{Quick setup for api.}
|
13
|
+
spec.description = %q{Api Setup.}
|
14
|
+
spec.homepage = "https://github.com/elorest/petergate_api"
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
18
|
+
spec.bindir = "exe"
|
19
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
20
|
+
spec.require_paths = ["lib"]
|
21
|
+
|
22
|
+
spec.add_development_dependency "bundler", "~> 1.11"
|
23
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
24
|
+
end
|
metadata
ADDED
@@ -0,0 +1,89 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: petergate_api
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Isaac Sloan
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-06-12 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.11'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.11'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
description: Api Setup.
|
42
|
+
email:
|
43
|
+
- isaac@isaacsloan.com
|
44
|
+
executables: []
|
45
|
+
extensions: []
|
46
|
+
extra_rdoc_files: []
|
47
|
+
files:
|
48
|
+
- ".gitignore"
|
49
|
+
- Gemfile
|
50
|
+
- LICENSE.txt
|
51
|
+
- README.md
|
52
|
+
- Rakefile
|
53
|
+
- bin/console
|
54
|
+
- bin/setup
|
55
|
+
- lib/generators/petergate_api/install_generator.rb
|
56
|
+
- lib/generators/petergate_api/templates/app/controllers/api/base_controller.rb
|
57
|
+
- lib/generators/petergate_api/templates/app/controllers/api/v1/auth_controller.rb
|
58
|
+
- lib/generators/petergate_api/templates/app/models/api/connection.rb
|
59
|
+
- lib/generators/petergate_api/templates/initializers/apipie.rb
|
60
|
+
- lib/generators/petergate_api/templates/migrations/add_mobile_reset_token_to_user.rb
|
61
|
+
- lib/generators/petergate_api/templates/migrations/create_api_connections.rb
|
62
|
+
- lib/petergate_api.rb
|
63
|
+
- lib/petergate_api/version.rb
|
64
|
+
- petergate_api.gemspec
|
65
|
+
homepage: https://github.com/elorest/petergate_api
|
66
|
+
licenses:
|
67
|
+
- MIT
|
68
|
+
metadata: {}
|
69
|
+
post_install_message:
|
70
|
+
rdoc_options: []
|
71
|
+
require_paths:
|
72
|
+
- lib
|
73
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
74
|
+
requirements:
|
75
|
+
- - ">="
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: '0'
|
78
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
requirements: []
|
84
|
+
rubyforge_project:
|
85
|
+
rubygems_version: 2.4.5.1
|
86
|
+
signing_key:
|
87
|
+
specification_version: 4
|
88
|
+
summary: Quick setup for api.
|
89
|
+
test_files: []
|