Class: Ugc::Settings

Inherits:
Object
  • Object
show all
Defined in:
lib/ugc/settings.rb

Constant Summary collapse

SETTINGS_FILE =
'settings.yml'

Instance Method Summary collapse

Constructor Details

#initialize(global_options) ⇒ Settings

Returns a new instance of Settings.



6
7
8
9
10
11
# File 'lib/ugc/settings.rb', line 6

def initialize(global_options)
  @draw_table_border = global_options[:border]
  @show_curl = global_options[:curl]
  @settings_file = File.join global_options[:settings], SETTINGS_FILE
  @settings = YAML.load_file(@settings_file) rescue default_settings
end

Instance Method Details

#access_tokenObject



64
65
66
# File 'lib/ugc/settings.rb', line 64

def access_token
  profile['access_token']
end

#access_token=(token) ⇒ Object



68
69
70
# File 'lib/ugc/settings.rb', line 68

def access_token=(token)
  set_profile 'access_token', token
end

#active_profile_nameObject



13
14
15
# File 'lib/ugc/settings.rb', line 13

def active_profile_name
  @settings['active_profile'] || 'local'
end

#active_profile_name=(name) ⇒ Object



17
18
19
20
21
# File 'lib/ugc/settings.rb', line 17

def active_profile_name=(name)
  raise "unknown profile: #{name}" unless profile(name)
  @settings['active_profile'] = name
  save_profile
end

#applicationObject



56
57
58
# File 'lib/ugc/settings.rb', line 56

def application
  profile['application']
end

#application=(app) ⇒ Object



60
61
62
# File 'lib/ugc/settings.rb', line 60

def application=(app)
  set_profile 'application', app
end

#base_urlObject



39
40
41
# File 'lib/ugc/settings.rb', line 39

def base_url
  profile['base_url']
end

#base_url=(url) ⇒ Object



43
44
45
46
# File 'lib/ugc/settings.rb', line 43

def base_url=(url)
  URI.parse url
  set_profile 'base_url', url
end

#configured?Boolean

Returns:

  • (Boolean)


72
73
74
# File 'lib/ugc/settings.rb', line 72

def configured?
  base_url && organization && application
end

#delete_profile(name) ⇒ Object



23
24
25
26
27
28
# File 'lib/ugc/settings.rb', line 23

def delete_profile(name)
  raise "cannot delete active profile" if active_profile_name == name
  raise "unknown profile: #{name}" unless profiles[name]
  profiles.delete name
  save_profile
end

#load_profile_blob(name) ⇒ Object



95
96
97
# File 'lib/ugc/settings.rb', line 95

def load_profile_blob(name)
  IO.read File.join settings_dir, "#{active_profile_name}.#{name}"
end

#logged_in?Boolean

Returns:

  • (Boolean)


84
85
86
# File 'lib/ugc/settings.rb', line 84

def logged_in?
  !!access_token
end

#organizationObject



48
49
50
# File 'lib/ugc/settings.rb', line 48

def organization
  profile['organization']
end

#organization=(org) ⇒ Object



52
53
54
# File 'lib/ugc/settings.rb', line 52

def organization=(org)
  set_profile 'organization', org
end

#profile(name = nil) ⇒ Object



30
31
32
33
# File 'lib/ugc/settings.rb', line 30

def profile(name=nil)
  name ||= active_profile_name
  profiles[name] || profiles[name] = {}
end

#profilesObject



35
36
37
# File 'lib/ugc/settings.rb', line 35

def profiles
  @settings['profiles'] || @settings['profiles'] = {}
end

#save_profile_blob(name, data) ⇒ Object



88
89
90
91
92
93
# File 'lib/ugc/settings.rb', line 88

def save_profile_blob(name, data)
  file = File.join settings_dir, "#{active_profile_name}.#{name}"
  File.open(file, 'w') do |out|
    out.write data
  end
end

#show_curl?Boolean

Returns:

  • (Boolean)


80
81
82
# File 'lib/ugc/settings.rb', line 80

def show_curl?
  !!@show_curl
end

#table_border?Boolean

Returns:

  • (Boolean)


76
77
78
# File 'lib/ugc/settings.rb', line 76

def table_border?
  !!@draw_table_border
end