rails-eve-base 0.1.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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: a68959fd927ad4c992808f6cab371f09e9a3f259ef4694c34954c853f77b8ae3
4
+ data.tar.gz: 4cef303650b2382036fb399588baa12b12c3fc4929cb8de8a9f1a0ca42e92164
5
+ SHA512:
6
+ metadata.gz: ddf6c10d56a437b237110f184e89aafbf0817b60e1c9b30b0a693fca0c9a5a119e3838e5958a7bf0cc6a11c0a9d7c3a304fea9bc96a0d8c6875f071ade77c2f2
7
+ data.tar.gz: dc92945403dfb37ae13cc7ee9636eb26a2af7803c8a75016c59061989580e473e473769963c3a34aaa70d45a8397e83c00494098b63f72f273af06d5f2b2d561
@@ -0,0 +1,20 @@
1
+ Copyright 2019 Adam Ladachowski
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,78 @@
1
+ # rails-eve-base
2
+
3
+ [![Codacy Badge](https://api.codacy.com/project/badge/Grade/d7c41f3367264949827b5bd74cc12fa0)](https://app.codacy.com/app/aladac/rails-eve-base?utm_source=github.com&utm_medium=referral&utm_content=aladac/rails-eve-base&utm_campaign=Badge_Grade_Dashboard)
4
+
5
+ Since I never seem to finish bigger EVE related projects, I decided to release some of the stuff I've been using so far as a sort of a template.
6
+
7
+ If this stuff possibly helps anyone write cool stuff for EVE, big bonus.
8
+
9
+ With this gem you can generate a basic structure for a EVE Online Ruby on Rails project
10
+
11
+ ## Usage
12
+
13
+ You can use this to bootstrap a EVE Online related Rails project. I probably just could have made it a template for the `rails` command but I realised that half way :D
14
+
15
+ First:
16
+
17
+ ```bash
18
+ bundle add rails-eve-base # within your rails project root
19
+ ```
20
+
21
+ Then:
22
+
23
+ ```bash
24
+ rails generate rails_eve:install
25
+ ```
26
+ And you should see:
27
+
28
+ ```bash
29
+ gemfile omniauth-crest
30
+ gemfile devise
31
+ gemfile mongoid
32
+ gemfile ruby-esi
33
+ generate mongoid:config
34
+ create config/mongoid.yml
35
+ create config/initializers/omniauth.rb
36
+ create app/lib/mongoid_setup.rb
37
+ create app/models/concerns/esi_character_api.rb
38
+ create app/models/character.rb
39
+ create app/models/alliance.rb
40
+ create app/models/corporation.rb
41
+ create config/initializers/devise.rb
42
+ create app/models/user.rb
43
+ create app/controllers/users/omniauth_callbacks_controller.rb
44
+ insert config/routes.rb
45
+ ```
46
+
47
+
48
+ ## Installation
49
+
50
+ Within your project root run:
51
+
52
+ ```bash
53
+ bundle add --group development rails-eve-base
54
+ ```
55
+
56
+ Or do it the longer way:
57
+
58
+ Add this line to your application's Gemfile:
59
+
60
+ ```ruby
61
+ gem 'rails-eve-base'
62
+ ```
63
+
64
+ And then execute:
65
+ ```bash
66
+ $ bundle
67
+ ```
68
+
69
+ Or install it yourself as:
70
+ ```bash
71
+ $ gem install rails-eve-base
72
+ ```
73
+
74
+ ## Contributing
75
+ Have an idea? Forkity fork, fork and make your changes check if they work then pop a pull request
76
+
77
+ ## License
78
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
@@ -0,0 +1,29 @@
1
+ # frozen_string_literal: true
2
+
3
+ begin
4
+ require 'bundler/setup'
5
+ rescue LoadError
6
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
7
+ end
8
+
9
+ require 'rdoc/task'
10
+
11
+ RDoc::Task.new(:rdoc) do |rdoc|
12
+ rdoc.rdoc_dir = 'rdoc'
13
+ rdoc.title = 'Rails::Eve::Base'
14
+ rdoc.options << '--line-numbers'
15
+ rdoc.rdoc_files.include('README.md')
16
+ rdoc.rdoc_files.include('lib/**/*.rb')
17
+ end
18
+
19
+ require 'bundler/gem_tasks'
20
+
21
+ require 'rake/testtask'
22
+
23
+ Rake::TestTask.new(:test) do |t|
24
+ t.libs << 'test'
25
+ t.pattern = 'test/**/*_test.rb'
26
+ t.verbose = false
27
+ end
28
+
29
+ task default: :test
File without changes
@@ -0,0 +1,8 @@
1
+ Description:
2
+ Explain the generator
3
+
4
+ Example:
5
+ rails generate install Thing
6
+
7
+ This will create:
8
+ what/will/it/create
@@ -0,0 +1,34 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RailsEve
4
+ class InstallGenerator < Rails::Generators::Base
5
+ source_root File.expand_path('templates', __dir__)
6
+
7
+ def install
8
+ gem 'omniauth-crest'
9
+ gem 'devise'
10
+ gem 'mongoid'
11
+ gem 'ruby-esi'
12
+
13
+ generate 'mongoid:config'
14
+
15
+ copy_file 'characters_controller.rb', './app/controllers/character_controller.rb'
16
+ copy_file 'rails-eve-base.html.slim', './app/views/layouts/rails-eve-base.html.slim'
17
+ copy_file 'index.html.slim', './app/views/characters/index.html.slim'
18
+ copy_file 'omniauth.rb', './config/initializers/omniauth.rb'
19
+ copy_file 'mongoid_setup.rb', './app/lib/mongoid_setup.rb'
20
+ copy_file 'esi_character_api.rb', './app/models/concerns/esi_character_api.rb'
21
+ copy_file 'character.rb', './app/models/character.rb'
22
+ copy_file 'alliance.rb', './app/models/alliance.rb'
23
+ copy_file 'corporation.rb', './app/models/corporation.rb'
24
+ copy_file 'devise.rb', './config/initializers/devise.rb', force: true
25
+ copy_file 'user.rb', './app/models/user.rb', force: true
26
+ copy_file 'omniauth_callbacks_controller.rb', './app/controllers/users/omniauth_callbacks_controller.rb'
27
+ inject_into_file './config/routes.rb', after: "Rails.application.routes.draw do\n" do
28
+ <<-'RUBY'
29
+ devise_for :users, controllers: { omniauth_callbacks: 'users/omniauth_callbacks' }
30
+ RUBY
31
+ end
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,22 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Alliance
4
+ include MongoidSetup
5
+ field :name, type: String
6
+ field :ticker, type: String
7
+ field :characters_count, type: Integer, default: 0
8
+ has_many :corporations
9
+ has_many :characters
10
+
11
+ def self.update_counter_caches
12
+ all.each do |user|
13
+ user.characters_count = user.characters.count
14
+ user.save
15
+ end
16
+ end
17
+
18
+ def self.create_from_api(alliance_id)
19
+ api_alliance = ESI::AllianceApi.new.get_alliances_alliance_id(alliance_id)
20
+ where(id: alliance_id, name: api_alliance.name, ticker: api_alliance.ticker).first_or_create
21
+ end
22
+ end
@@ -0,0 +1,24 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Character
4
+ include MongoidSetup
5
+ include EsiCharacterApi
6
+ field :name, type: String
7
+ field :access_token, type: String
8
+ field :refresh_token, type: String
9
+ field :token_expires, type: Time
10
+ field :display_option, type: String
11
+ belongs_to :corporation, optional: true, counter_cache: true
12
+ belongs_to :alliance, optional: true, counter_cache: true
13
+ belongs_to :user, counter_cache: true
14
+
15
+ after_save :create_corporation
16
+
17
+ def create_corporation
18
+ Corporation.create_from_api(corporation_id)
19
+ end
20
+
21
+ def wallet_journal
22
+ wallet_api.get_characters_character_id_wallet_journal(id)
23
+ end
24
+ end
@@ -0,0 +1,8 @@
1
+ class CharactersController < ApplicationController
2
+ before_action :authenticate_user!
3
+ layout 'rails-eve-base'
4
+
5
+ def index
6
+ @characters = current_user.characters
7
+ end
8
+ end
@@ -0,0 +1,30 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Corporation
4
+ include MongoidSetup
5
+ field :name, type: String
6
+ field :npc, type: Boolean
7
+ field :ticker, type: String
8
+ field :characters_count, type: Integer, default: 0
9
+ belongs_to :alliance, optional: true
10
+ has_many :characters
11
+ after_save :create_alliance
12
+
13
+ def self.update_counter_caches
14
+ all.each do |user|
15
+ user.characters_count = user.characters.count
16
+ user.save
17
+ end
18
+ end
19
+
20
+ def self.create_from_api(corporation_id)
21
+ api_corporation = ESI::CorporationApi.new.get_corporations_corporation_id(corporation_id)
22
+ where(id: corporation_id, name: api_corporation.name, ticker: api_corporation.ticker, alliance_id: api_corporation.alliance_id).first_or_create
23
+ end
24
+
25
+ def create_alliance
26
+ return false unless alliance_id
27
+
28
+ Alliance.create_from_api(alliance_id)
29
+ end
30
+ end
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ Devise.setup do |config|
4
+ config.secret_key = '5db398209eb5511b119c0973bbee2da55be0fa11d6cd27c411d79d04439b846b8747a3cffd20700fe0f1191ee54f60a1316ade15a2baf43ce471ff9c9ef702be'
5
+ config.mailer_sender = 'evescore@evescore.online'
6
+ require 'devise/orm/mongoid'
7
+ config.case_insensitive_keys = [:email]
8
+ config.strip_whitespace_keys = [:email]
9
+ config.skip_session_storage = [:http_auth]
10
+ config.stretches = Rails.env.test? ? 1 : 11
11
+ config.reconfirmable = true
12
+ config.expire_all_remember_me_on_sign_out = true
13
+ config.password_length = 6..128
14
+ config.email_regexp = /\A[^@\s]+@[^@\s]+\z/
15
+ config.reset_password_within = 6.hours
16
+ config.sign_out_via = :delete
17
+ config.omniauth :esi, 'd8e64f0d0a4e4a4d867052b0c93ec01e', 'cZQvip2l9Yy7tbzmjiBCerxhsXJ3iwHHxpEy7OBW', scope: 'esi-wallet.read_character_wallet.v1'
18
+ end
@@ -0,0 +1,57 @@
1
+ # frozen_string_literal: true
2
+
3
+ module EsiCharacterApi
4
+ extend ActiveSupport::Concern
5
+
6
+ def update_tokens(credentials)
7
+ self.access_token = credentials['token']
8
+ self.refresh_token = credentials['refresh_token']
9
+ self.token_expires = Time.zone.at credentials['expires_at']
10
+ save
11
+ end
12
+
13
+ def refresh_token!
14
+ return false if Time.zone.now < token_expires
15
+
16
+ self.access_token = refresh_token_request.access_token
17
+ self.refresh_token = refresh_token_request.refresh_token
18
+ self.token_expires = Time.zone.now + refresh_token_request.expires_in
19
+ save
20
+ end
21
+
22
+ def refresh_token_request
23
+ request = Typhoeus.post('https://login.eveonline.com/oauth/token', headers: refresh_token_headers, body: refresh_token_body)
24
+ OpenStruct.new(JSON.parse(request.body))
25
+ end
26
+
27
+ def refresh_token_headers
28
+ { 'Content-Type' => 'application/json', 'Authorization' => authorization_header }
29
+ end
30
+
31
+ def refresh_token_body
32
+ { grant_type: 'refresh_token', refresh_token: refresh_token }.to_json
33
+ end
34
+
35
+ def current_access_token
36
+ refresh_token!
37
+ access_token
38
+ end
39
+
40
+ def authorization_header
41
+ secrets = OpenStruct.new Rails.application.config_for(:secrets)
42
+ app_id = secrets.eve_api_app_id
43
+ app_secret = secrets.eve_api_app_secret
44
+ auth = Base64.strict_encode64("#{app_id}:#{app_secret}")
45
+ "Basic #{auth}"
46
+ end
47
+
48
+ def api_client
49
+ client = ESI::ApiClient.new
50
+ client.config.access_token = current_access_token
51
+ client
52
+ end
53
+
54
+ def wallet_api
55
+ ESI::WalletApi.new(api_client)
56
+ end
57
+ end
@@ -0,0 +1,6 @@
1
+ table.table.table-small
2
+ - @characters.each do |character|
3
+ tr
4
+ td.name = character.name
5
+ td.corporation = character.corporation.name
6
+
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ module MongoidSetup
4
+ extend ActiveSupport::Concern
5
+ include Mongoid::Document
6
+ include Mongoid::Timestamps
7
+ include GlobalID::Identification
8
+ end
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ module OmniAuth
4
+ module Strategies
5
+ class ESI < OmniAuth::Strategies::Crest
6
+ option :name, :esi
7
+ option :authorize_options, %i[scope state redirect_uri]
8
+
9
+ def callback_url
10
+ options[:redirect_uri] || (full_host + script_name + callback_path)
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,28 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController
4
+ before_action :set_user
5
+ def esi
6
+ if @user.persisted?
7
+ import_character
8
+ sign_in_and_redirect_with_flash
9
+ else
10
+ redirect_to new_user_registration_url
11
+ end
12
+ end
13
+
14
+ protected
15
+
16
+ def set_user
17
+ @user = User.from_omniauth request.env['omniauth.auth']
18
+ end
19
+
20
+ def import_character
21
+ @user.import_character request.env['omniauth.auth']
22
+ end
23
+
24
+ def sign_in_and_redirect_with_flash
25
+ sign_in_and_redirect @user, event: :authentication # this will throw if @user is not activated
26
+ set_flash_message(:notice, :success, kind: 'EVE Online') if is_navigational_format?
27
+ end
28
+ end
@@ -0,0 +1,8 @@
1
+ doctype
2
+ html
3
+ head
4
+ title rails-eve-base example
5
+ link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.3.1/css/bootstrap.min.css"
6
+ body
7
+ .container-fluid
8
+ == yield
@@ -0,0 +1,63 @@
1
+ # frozen_string_literal: true
2
+
3
+ class User
4
+ include MongoidSetup
5
+ field :characters_count, type: Integer, default: 0
6
+ devise :database_authenticatable, :registerable, :omniauthable,
7
+ :rememberable, :trackable
8
+
9
+ field :email, type: String, default: ''
10
+ field :encrypted_password, type: String, default: ''
11
+ field :reset_password_token, type: String
12
+ field :reset_password_sent_at, type: Time
13
+ field :remember_created_at, type: Time
14
+ field :sign_in_count, type: Integer, default: 0
15
+ field :current_sign_in_at, type: Time
16
+ field :last_sign_in_at, type: Time
17
+ field :current_sign_in_ip, type: String
18
+ field :last_sign_in_ip, type: String
19
+ field :confirmation_token, type: String
20
+ field :confirmed_at, type: Time
21
+ field :confirmation_sent_at, type: Time
22
+ field :unconfirmed_email, type: String # Only if using reconfirmable
23
+ field :uid, type: Integer
24
+ field :provider, type: String
25
+
26
+ has_many :characters
27
+
28
+ def will_save_change_to_email?; end
29
+
30
+ def self.update_counter_caches
31
+ all.each do |user|
32
+ user.characters_count = user.characters.count
33
+ user.save
34
+ end
35
+ end
36
+
37
+ def self.from_omniauth(auth)
38
+ where(provider: auth.provider, uid: auth.uid).first_or_create do |user|
39
+ user.email = SecureRandom.hex
40
+ user.password = Devise.friendly_token[0, 20]
41
+ end
42
+ end
43
+
44
+ def self.create_from_omniauth(payload)
45
+ u = new
46
+ u.uid = payload.uid
47
+ u.password = SecureRandom.hex
48
+ u.email = 'example@example.com'
49
+ u.save
50
+ end
51
+
52
+ def character_api
53
+ ESI::CharacterApi.new
54
+ end
55
+
56
+ def import_character(omniauth_payload)
57
+ character_id = omniauth_payload['extra']['raw_info']['CharacterID']
58
+ api_character = character_api.get_characters_character_id(character_id)
59
+ credentials = omniauth_payload['credentials']
60
+ character = characters.where(id: character_id, name: api_character.name, corporation_id: api_character.corporation_id, alliance_id: api_character.alliance_id).first_or_create
61
+ character.update_tokens(credentials)
62
+ end
63
+ end
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'rails/eve/base/railtie'
4
+
5
+ module Rails
6
+ module Eve
7
+ module Base
8
+ # Your code goes here...
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Rails
4
+ module Eve
5
+ module Base
6
+ class Railtie < ::Rails::Railtie
7
+ end
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Rails
4
+ module Eve
5
+ module Base
6
+ VERSION = '0.1.3'
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,6 @@
1
+ # frozen_string_literal: true
2
+
3
+ # desc "Explaining what the task does"
4
+ # task :rails_eve_base do
5
+ # # Task goes here
6
+ # end
metadata ADDED
@@ -0,0 +1,107 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rails-eve-base
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.3
5
+ platform: ruby
6
+ authors:
7
+ - Adam Ladachowski
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2019-05-25 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rails
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 5.2.3
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 5.2.3
27
+ - !ruby/object:Gem::Dependency
28
+ name: rubocop
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: sqlite3
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: Main runtime of EVE Online RoR plugins
56
+ email:
57
+ - adam@saiden.pl
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - MIT-LICENSE
63
+ - README.md
64
+ - Rakefile
65
+ - config/environment
66
+ - lib/generators/rails_eve/install/USAGE
67
+ - lib/generators/rails_eve/install/install_generator.rb
68
+ - lib/generators/rails_eve/install/templates/alliance.rb
69
+ - lib/generators/rails_eve/install/templates/character.rb
70
+ - lib/generators/rails_eve/install/templates/characters_controller.rb
71
+ - lib/generators/rails_eve/install/templates/corporation.rb
72
+ - lib/generators/rails_eve/install/templates/devise.rb
73
+ - lib/generators/rails_eve/install/templates/esi_character_api.rb
74
+ - lib/generators/rails_eve/install/templates/index.html.pug
75
+ - lib/generators/rails_eve/install/templates/mongoid_setup.rb
76
+ - lib/generators/rails_eve/install/templates/omniauth.rb
77
+ - lib/generators/rails_eve/install/templates/omniauth_callbacks_controller.rb
78
+ - lib/generators/rails_eve/install/templates/rails-eve-base.html.slim
79
+ - lib/generators/rails_eve/install/templates/user.rb
80
+ - lib/rails/eve/base.rb
81
+ - lib/rails/eve/base/railtie.rb
82
+ - lib/rails/eve/base/version.rb
83
+ - lib/tasks/rails/eve/base_tasks.rake
84
+ homepage: https://github.com/aladac/rails-eve-base
85
+ licenses:
86
+ - MIT
87
+ metadata: {}
88
+ post_install_message:
89
+ rdoc_options: []
90
+ require_paths:
91
+ - lib
92
+ required_ruby_version: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ required_rubygems_version: !ruby/object:Gem::Requirement
98
+ requirements:
99
+ - - ">="
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ requirements: []
103
+ rubygems_version: 3.0.3
104
+ signing_key:
105
+ specification_version: 4
106
+ summary: Main runtime of EVE Online RoR plugins
107
+ test_files: []