Class: Plugin

Inherits:
Object
  • Object
show all
Includes:
ActiveModel::Model, Draper::Decoratable
Defined in:
app/models/plugin.rb

Defined Under Namespace

Classes: GemError

Constant Summary collapse

WORKING =
[]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#api_versionObject

Returns the value of attribute api_version.



13
14
15
# File 'app/models/plugin.rb', line 13

def api_version
  @api_version
end

#categoryObject

Returns the value of attribute category.



13
14
15
# File 'app/models/plugin.rb', line 13

def category
  @category
end

#gem_nameObject

Returns the value of attribute gem_name.



13
14
15
# File 'app/models/plugin.rb', line 13

def gem_name
  @gem_name
end

#versionObject

Returns the value of attribute version.



13
14
15
# File 'app/models/plugin.rb', line 13

def version
  @version
end

Class Method Details

.installedObject



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

.installingObject



110
111
112
# File 'app/models/plugin.rb', line 110

def self.installing
  processing_state(:install)
end

.processingObject



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


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

.uninstallingObject



114
115
116
# File 'app/models/plugin.rb', line 114

def self.uninstalling
  processing_state(:uninstall)
end

Instance Method Details

#authorsObject



74
75
76
# File 'app/models/plugin.rb', line 74

def authors
  property("authors")
end

#gem_json_urlObject



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_versionsObject



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

#inspectObject



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

Returns:

  • (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_versionObject



53
54
55
56
# File 'app/models/plugin.rb', line 53

def installed_version
  return unless installed?
  version
end

#latest_versionObject



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

Returns:

  • (Boolean)


58
59
60
# File 'app/models/plugin.rb', line 58

def latest_version?
  installed_version == latest_version
end

#processing?Boolean

Returns:

  • (Boolean)


49
50
51
# File 'app/models/plugin.rb', line 49

def processing?
  WORKING.any?{|data| data[:plugin].gem_name == gem_name}
end

#released_versionsObject



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_pageObject



83
84
85
# File 'app/models/plugin.rb', line 83

def rubygems_org_page
  "https://rubygems.org/gems/#{gem_name}"
end

#summaryObject



70
71
72
# File 'app/models/plugin.rb', line 70

def summary
  property("summary")
end

#to_paramObject



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