Class: Plugin
- Inherits:
-
Object
- Object
- Plugin
- Includes:
- ActiveModel::Model, Draper::Decoratable
- Defined in:
- app/models/plugin.rb
Defined Under Namespace
Classes: GemError
Constant Summary collapse
- WORKING =
[]
Instance Attribute Summary collapse
-
#api_version ⇒ Object
Returns the value of attribute api_version.
-
#category ⇒ Object
Returns the value of attribute category.
-
#gem_name ⇒ Object
Returns the value of attribute gem_name.
-
#version ⇒ Object
Returns the value of attribute version.
Class Method Summary collapse
- .installed ⇒ Object
- .installing ⇒ Object
- .processing ⇒ Object
- .recommended ⇒ Object
- .uninstalling ⇒ Object
Instance Method Summary collapse
- #authors ⇒ Object
- #gem_json_url ⇒ Object
- #gem_versions ⇒ Object
- #gem_versions! ⇒ Object
- #inspect ⇒ Object
- #install! ⇒ Object
- #installed? ⇒ Boolean
- #installed_version ⇒ Object
- #latest_version ⇒ Object
- #latest_version? ⇒ Boolean
- #processing? ⇒ Boolean
- #released_versions ⇒ Object
- #rubygems_org_page ⇒ Object
- #summary ⇒ Object
- #to_param ⇒ Object
- #uninstall! ⇒ Object
- #upgrade!(new_version) ⇒ Object
Instance Attribute Details
#api_version ⇒ Object
Returns the value of attribute api_version.
13 14 15 |
# File 'app/models/plugin.rb', line 13 def api_version @api_version end |
#category ⇒ Object
Returns the value of attribute category.
13 14 15 |
# File 'app/models/plugin.rb', line 13 def category @category end |
#gem_name ⇒ Object
Returns the value of attribute gem_name.
13 14 15 |
# File 'app/models/plugin.rb', line 13 def gem_name @gem_name end |
#version ⇒ Object
Returns the value of attribute version.
13 14 15 |
# File 'app/models/plugin.rb', line 13 def version @version end |
Class Method Details
.installed ⇒ Object
87 88 89 90 91 92 93 94 95 96 |
# File 'app/models/plugin.rb', line 87 def self.installed Bundler.with_clean_env do gems = FluentGem.list gems.grep(/fluent-plugin/).map do |gem| name, versions_str = gem.strip.split(" ") version = versions_str[/[^(), ]+/] new(gem_name: name, version: version) end end end |
.installing ⇒ Object
110 111 112 |
# File 'app/models/plugin.rb', line 110 def self.installing processing_state(:install) end |
.processing ⇒ Object
104 105 106 107 108 |
# File 'app/models/plugin.rb', line 104 def self.processing WORKING.find_all do |data| data[:state] == :running end end |
.recommended ⇒ Object
98 99 100 101 102 |
# File 'app/models/plugin.rb', line 98 def self.recommended Settings.recommended_plugins.map do |data| new(category: data["category"], gem_name: "fluent-plugin-#{data["name"]}", api_version: data["api_version"]) end end |
.uninstalling ⇒ Object
114 115 116 |
# File 'app/models/plugin.rb', line 114 def self.uninstalling processing_state(:uninstall) end |
Instance Method Details
#authors ⇒ Object
74 75 76 |
# File 'app/models/plugin.rb', line 74 def property("authors") end |
#gem_json_url ⇒ Object
133 134 135 |
# File 'app/models/plugin.rb', line 133 def gem_json_url "https://rubygems.org/api/v1/versions/#{gem_name}.json" end |
#gem_versions ⇒ Object
118 119 120 121 122 |
# File 'app/models/plugin.rb', line 118 def gem_versions Rails.cache.fetch(gem_json_url, expires_in: 60.minutes) do # NOTE: 60.minutes could be changed if it doesn't fit gem_versions! end end |
#gem_versions! ⇒ Object
124 125 126 127 128 129 130 131 |
# File 'app/models/plugin.rb', line 124 def gem_versions! res = HTTPClient.get(gem_json_url) if res.code == 200 json = res.body Rails.cache.write(gem_json_url, json, expires_in: 60.minutes) # NOTE: 60.minutes could be changed if it doesn't fit json end end |
#inspect ⇒ Object
78 79 80 81 |
# File 'app/models/plugin.rb', line 78 def inspect self.version ||= latest_version %Q|<#{gem_name}, "#{version}">| end |
#install! ⇒ Object
21 22 23 24 25 |
# File 'app/models/plugin.rb', line 21 def install! return if installed? self.version ||= latest_version valid? && gem_install end |
#installed? ⇒ Boolean
43 44 45 46 47 |
# File 'app/models/plugin.rb', line 43 def installed? self.class.installed.any? do |plugin| plugin.gem_name == gem_name end end |
#installed_version ⇒ Object
53 54 55 56 |
# File 'app/models/plugin.rb', line 53 def installed_version return unless installed? version end |
#latest_version ⇒ Object
62 63 64 |
# File 'app/models/plugin.rb', line 62 def latest_version @latest_version ||= JSON.parse(gem_versions).map {|ver| Gem::Version.new ver["number"] }.max.to_s end |
#latest_version? ⇒ Boolean
58 59 60 |
# File 'app/models/plugin.rb', line 58 def latest_version? installed_version == latest_version end |
#processing? ⇒ Boolean
49 50 51 |
# File 'app/models/plugin.rb', line 49 def processing? WORKING.any?{|data| data[:plugin].gem_name == gem_name} end |
#released_versions ⇒ Object
66 67 68 |
# File 'app/models/plugin.rb', line 66 def released_versions @released_versions ||= JSON.parse(gem_versions).map {|ver| ver["number"]}.sort_by{|ver| Gem::Version.new ver}.reverse end |
#rubygems_org_page ⇒ Object
83 84 85 |
# File 'app/models/plugin.rb', line 83 def rubygems_org_page "https://rubygems.org/gems/#{gem_name}" end |
#summary ⇒ Object
70 71 72 |
# File 'app/models/plugin.rb', line 70 def summary property("summary") end |
#to_param ⇒ Object
17 18 19 |
# File 'app/models/plugin.rb', line 17 def to_param gem_name end |
#uninstall! ⇒ Object
27 28 29 30 |
# File 'app/models/plugin.rb', line 27 def uninstall! return unless installed? gem_uninstall # NOTE: not validate end |
#upgrade!(new_version) ⇒ Object
32 33 34 35 36 37 38 39 40 41 |
# File 'app/models/plugin.rb', line 32 def upgrade!(new_version) return unless installed? upgrade = self.class.new(gem_name: self.gem_name, version: new_version) if self.valid? && upgrade.valid? self.uninstall! upgrade.install! self.version = upgrade.version end end |