forced 0.1.0

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
+ SHA1:
3
+ metadata.gz: c1b62f3102ad7c3f39bfb99d9b7ea2cec3873f29
4
+ data.tar.gz: cabafaba97d16fc8191b4971bb972684ca887cc8
5
+ SHA512:
6
+ metadata.gz: f5716bc3e260313d6db7d072a917fad99fd7ecd2f1c0578ae6fcf03eb9a88de8428d1c522b0cc30432517a08b0cfc1b5371513526b4533e071c1f1ee25c49255
7
+ data.tar.gz: 8dd67dc2a93046387c8de356db7038e512533755ec9498e6d01c456862dfbabfd2cd1c29d8ec9058379e4ec2510e13efaa00f4723b66f4cacc27f2223c6724d1
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2018 aoozdemir
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,73 @@
1
+ # Forced
2
+
3
+ This plugin is for Rails APIs that supports multiple mobile applications and want to force an update to those applications.
4
+
5
+ Read the link below to get some insight.
6
+
7
+ * [Handling Force Update on Mobile Apps / Rusty Neuron](https://rustyneuron.net/2018/07/12/handling-force-update-on-mobile-apps/)
8
+
9
+ ## Usage
10
+
11
+ Module needs to get the coming request to prepare the response. As long as request headers contains `X-Platform` and `X-Client-Version`, you are good to go.
12
+
13
+ After adding `forced` to your Gemfile, add the line below to your routes file.
14
+
15
+ ```ruby
16
+ mount Forced::Engine => "/forced"
17
+ ```
18
+
19
+ Then send a `GET` request to `{{url}}/forced/status`. This will return the below JSON.
20
+
21
+ ```json
22
+ {
23
+ "update": "force_update",
24
+ "latest_version": "1.0.0",
25
+ "current_time": "2018-07-13T16:28:22.829Z"
26
+ }
27
+ ```
28
+
29
+ If you want to return some version of this hash, you can access the response by calling the `Response.call(request)` method. See below.
30
+
31
+ ```ruby
32
+ response = Forced::Response.call(request)
33
+ ```
34
+
35
+ Client enum is `[:android, :ios]` at default. To change it, open up an initializer for `Forced` module and change the constant named `CLIENT_ENUM`.
36
+
37
+ ```ruby
38
+ module Forced
39
+ CLIENT_ENUM = [:android, :ios]
40
+ end
41
+ ```
42
+
43
+ To create a record, you can use your Rails console.
44
+
45
+ ```ruby
46
+ Forced::AppVersion.new
47
+
48
+ # => #<Forced::AppVersion id: nil, client: nil, version: nil, force_update: false, changelog: nil, created_at: nil, updated_at: nil>
49
+ ```
50
+
51
+ ## Responses
52
+
53
+ All available under `Forced::MESSAGES` hash table. You can override the values as you wish. Also checkout the `check_update_status` private method in `base.rb` to understand the cases.
54
+
55
+ ## Installation
56
+ Add this line to your application's Gemfile:
57
+
58
+ ```ruby
59
+ gem 'forced'
60
+ ```
61
+
62
+ And then execute:
63
+ ```bash
64
+ $ bundle
65
+ ```
66
+
67
+ Or install it yourself as:
68
+ ```bash
69
+ $ gem install forced
70
+ ```
71
+
72
+ ## License
73
+ 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 = 'Forced'
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("test/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/forced .js
2
+ //= link_directory ../stylesheets/forced .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,5 @@
1
+ module Forced
2
+ class ApplicationController < ActionController::Base
3
+ protect_from_forgery with: :exception
4
+ end
5
+ end
@@ -0,0 +1,9 @@
1
+ module Forced
2
+ class StatusController < ApplicationController
3
+ def index
4
+ response = Forced::Response.call(request)
5
+
6
+ render json: response
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,4 @@
1
+ module Forced
2
+ module ApplicationHelper
3
+ end
4
+ end
@@ -0,0 +1,4 @@
1
+ module Forced
2
+ class ApplicationJob < ActiveJob::Base
3
+ end
4
+ end
@@ -0,0 +1,6 @@
1
+ module Forced
2
+ class ApplicationMailer < ActionMailer::Base
3
+ default from: 'from@example.com'
4
+ layout 'mailer'
5
+ end
6
+ end
@@ -0,0 +1,21 @@
1
+ # == Schema Information
2
+ #
3
+ # Table name: forced_app_versions
4
+ #
5
+ # id :integer not null, primary key
6
+ # client :integer
7
+ # version :string(255)
8
+ # force_update :boolean default(FALSE)
9
+ # changelog :text
10
+ # created_at :datetime not null
11
+ # updated_at :datetime not null
12
+ #
13
+
14
+ module Forced
15
+ class AppVersion < ApplicationRecord
16
+ enum client: Forced::CLIENT_ENUM
17
+
18
+ validates :client, presence: true
19
+ validates :version, presence: true, length: { maximum: 255 }
20
+ end
21
+ end
@@ -0,0 +1,5 @@
1
+ module Forced
2
+ class ApplicationRecord < ActiveRecord::Base
3
+ self.abstract_class = true
4
+ end
5
+ end
@@ -0,0 +1,16 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>Forced</title>
5
+ <%= csrf_meta_tags %>
6
+ <%= csp_meta_tag %>
7
+
8
+ <%= stylesheet_link_tag "forced/application", media: "all" %>
9
+ <%= javascript_include_tag "forced/application" %>
10
+ </head>
11
+ <body>
12
+
13
+ <%= yield %>
14
+
15
+ </body>
16
+ </html>
data/config/routes.rb ADDED
@@ -0,0 +1,3 @@
1
+ Forced::Engine.routes.draw do
2
+ get :status, to: 'status#index'
3
+ end
@@ -0,0 +1,12 @@
1
+ class CreateForcedAppVersions < ActiveRecord::Migration[5.2]
2
+ def change
3
+ create_table :forced_app_versions do |t|
4
+ t.integer :client
5
+ t.string :version, limit: 255
6
+ t.boolean :force_update, nil: false, default: false
7
+ t.text :changelog
8
+
9
+ t.timestamps
10
+ end
11
+ end
12
+ end
data/lib/forced.rb ADDED
@@ -0,0 +1,8 @@
1
+ require 'forced/engine'
2
+ require 'forced/base'
3
+ require 'forced/response'
4
+ require 'forced/client_enum'
5
+ require 'forced/messages'
6
+
7
+ module Forced
8
+ end
@@ -0,0 +1,47 @@
1
+ module Forced
2
+ class Base
3
+ class << self
4
+ def get_records(request)
5
+ @client_platform = get_client_platform(request)
6
+ @client_version = get_client_version(request)
7
+
8
+ @client_version_records = (@client_platform && @client_version ? Forced::AppVersion.where(client: @client_platform) : nil)
9
+ @versions_after_client = @client_version_records&.where('version > ?', @client_version)
10
+ @latest_app_version = @client_version_records&.last
11
+ @any_forced_in_the_future = @versions_after_client&.pluck(:force_update)&.any?
12
+ end
13
+
14
+ private
15
+
16
+ def get_client_platform(request)
17
+ return request.headers['X-Platform'].to_s.downcase
18
+ end
19
+
20
+ def get_client_version(request)
21
+ return request.headers['X-Client-Version']
22
+ end
23
+
24
+ def check_update_status(client_version, latest_app_version, any_forced_in_the_future)
25
+ nil_report = []
26
+ nil_report << MESSAGES[:app_version_returned_nil] if latest_app_version.nil?
27
+ nil_report << MESSAGES[:client_version_returned_nil] if client_version.nil?
28
+
29
+ return nil_report.join(', ') if !nil_report.empty?
30
+
31
+ client_v = Gem::Version.new(client_version)
32
+ latest_v = Gem::Version.new(latest_app_version.version)
33
+
34
+ case
35
+ when client_v == latest_v
36
+ return MESSAGES[:no_update]
37
+ when client_v < latest_v
38
+ any_forced_in_the_future ? MESSAGES[:force_update] : MESSAGES[:just_update]
39
+ when client_v > latest_v
40
+ return MESSAGES[:client_is_ahead_of_backend]
41
+ else
42
+ return MESSAGES[:something_went_wrong]
43
+ end
44
+ end
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,3 @@
1
+ module Forced
2
+ CLIENT_ENUM = [:android, :ios]
3
+ end
@@ -0,0 +1,14 @@
1
+ module Forced
2
+ class Engine < ::Rails::Engine
3
+ isolate_namespace Forced
4
+
5
+ # import engine's migratons into main app
6
+ initializer :append_migrations do |app|
7
+ unless app.root.to_s.match root.to_s
8
+ config.paths["db/migrate"].expanded.each do |expanded_path|
9
+ app.config.paths["db/migrate"] << expanded_path
10
+ end
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,11 @@
1
+ module Forced
2
+ MESSAGES = {
3
+ app_version_returned_nil: :app_version_returned_nil,
4
+ client_version_returned_nil: :client_version_returned_nil,
5
+ no_update: :no_update,
6
+ force_update: :force_update,
7
+ just_update: :just_update,
8
+ client_is_ahead_of_backend: :client_is_ahead_of_backend,
9
+ something_went_wrong: :something_went_wrong
10
+ }
11
+ end
@@ -0,0 +1,15 @@
1
+ module Forced
2
+ class Response < Base
3
+ class << self
4
+ def call(request)
5
+ get_records(request)
6
+
7
+ return status = {
8
+ update: check_update_status(@client_version, @latest_app_version, @any_forced_in_the_future),
9
+ latest_version: @latest_app_version&.version,
10
+ current_time: Time.zone.now
11
+ }
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,8 @@
1
+ module Forced
2
+ MAJOR = 0
3
+ MINOR = 1
4
+ TINY = 0
5
+ PRE = nil
6
+
7
+ VERSION = [MAJOR, MINOR, TINY, PRE].compact.join('.').freeze
8
+ end
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :forced do
3
+ # # Task goes here
4
+ # end
metadata ADDED
@@ -0,0 +1,110 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: forced
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - aoozdemir
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2018-07-13 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.0
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.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: sqlite3
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: annotate
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: Easy force update control for APIs that support mobile clients.
56
+ email:
57
+ - aoozdemir@live.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - MIT-LICENSE
63
+ - README.md
64
+ - Rakefile
65
+ - app/assets/config/forced_manifest.js
66
+ - app/assets/javascripts/forced/application.js
67
+ - app/assets/stylesheets/forced/application.css
68
+ - app/controllers/forced/application_controller.rb
69
+ - app/controllers/forced/status_controller.rb
70
+ - app/helpers/forced/application_helper.rb
71
+ - app/jobs/forced/application_job.rb
72
+ - app/mailers/forced/application_mailer.rb
73
+ - app/models/forced/app_version.rb
74
+ - app/models/forced/application_record.rb
75
+ - app/views/layouts/forced/application.html.erb
76
+ - config/routes.rb
77
+ - db/migrate/20180713160038_create_forced_app_versions.rb
78
+ - lib/forced.rb
79
+ - lib/forced/base.rb
80
+ - lib/forced/client_enum.rb
81
+ - lib/forced/engine.rb
82
+ - lib/forced/messages.rb
83
+ - lib/forced/response.rb
84
+ - lib/forced/version.rb
85
+ - lib/tasks/forced_tasks.rake
86
+ homepage: https://github.com/aoozdemir/forced
87
+ licenses:
88
+ - MIT
89
+ metadata: {}
90
+ post_install_message:
91
+ rdoc_options: []
92
+ require_paths:
93
+ - lib
94
+ required_ruby_version: !ruby/object:Gem::Requirement
95
+ requirements:
96
+ - - ">="
97
+ - !ruby/object:Gem::Version
98
+ version: '0'
99
+ required_rubygems_version: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ requirements: []
105
+ rubyforge_project:
106
+ rubygems_version: 2.6.14.1
107
+ signing_key:
108
+ specification_version: 4
109
+ summary: Easy force update control for Rails APIs that support mobile clients.
110
+ test_files: []