Class: Firebrew::Runner

Inherits:
Object
  • Object
show all
Defined in:
lib/firebrew/runner.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config = {}) ⇒ Runner

Returns a new instance of Runner.



35
36
37
38
39
40
41
42
43
44
45
# File 'lib/firebrew/runner.rb', line 35

def initialize(config={})
  self.config = self.class.default_config.merge(config)
  
  @profile_manager = Firefox::Profile::Manager.new(
    base_dir: self.config[:base_dir],
    data_file: self.config[:data_file]
  )
  @firefox = Firefox::Command.new(self.config)
  
  self.select_profile
end

Instance Attribute Details

#configObject

Returns the value of attribute config.



7
8
9
# File 'lib/firebrew/runner.rb', line 7

def config
  @config
end

#profileObject

Returns the value of attribute profile.



7
8
9
# File 'lib/firebrew/runner.rb', line 7

def profile
  @profile
end

Class Method Details

.default_config(platform = RUBY_PLATFORM) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/firebrew/runner.rb', line 9

def self.default_config(platform = RUBY_PLATFORM)
  result = {
    base_dir: ENV['FIREBREW_FIREFOX_PROFILE_BASE_DIR'],
    firefox: ENV['FIREBREW_FIREFOX'],
    profile: ENV['FIREBREW_FIREFOX_PROFILE'] || 'default',
  }
  
  case platform
  when /darwin/ then
    result[:base_dir] ||= '~/Library/Application Support/Firefox'
    result[:firefox] ||= '/Applications/Firefox.app/Contents/MacOS/firefox-bin'
    
  when /linux/ then
    result[:base_dir] ||= '~/.mozilla/firefox'
    result[:firefox] ||= '/usr/bin/firefox'
    
  when /mswin(?!ce)|mingw|cygwin|bccwin/ then
    appdata = ENV['APPDATA'].to_s.gsub('\\','/')
    programfiles = (ENV['PROGRAMFILES(X86)'] || ENV['PROGRAMFILES']).to_s.gsub('\\','/')
    result[:base_dir] ||= File.join(appdata, 'Mozilla/Firefox')
    result[:firefox] ||= File.join(programfiles, 'Mozilla Firefox/firefox.exe')
  end
  
  result
end

Instance Method Details

#info(params = {}) ⇒ Object



66
67
68
# File 'lib/firebrew/runner.rb', line 66

def info(params={})
  self.fetch_api(term: params[:term], max: 1).first
end

#install(params = {}) ⇒ Object



51
52
53
54
55
56
# File 'lib/firebrew/runner.rb', line 51

def install(params={})
  extension = self.profile.extensions.find(params[:term])
  raise Firebrew::OperationAlreadyCompletedError if extension.present?
  result = self.fetch_api(term: params[:term], max: 1).first
  self.profile.extensions.install(result.extension)
end

#list(params = {}) ⇒ Object



70
71
72
# File 'lib/firebrew/runner.rb', line 70

def list(params={})
  self.profile.extensions.all
end

#search(params = {}) ⇒ Object



74
75
76
# File 'lib/firebrew/runner.rb', line 74

def search(params={})
  self.fetch_api(term: params[:term])
end

#select_profile(name = nil) ⇒ Object



47
48
49
# File 'lib/firebrew/runner.rb', line 47

def select_profile(name = nil)
  self.profile = @profile_manager.find!(name || self.config[:profile])
end

#uninstall(params = {}) ⇒ Object



58
59
60
61
62
63
64
# File 'lib/firebrew/runner.rb', line 58

def uninstall(params={})
  begin
    self.profile.extensions.find!(params[:term]).delete
  rescue Firebrew::ExtensionNotFoundError
    raise Firebrew::OperationAlreadyCompletedError
  end
end