Class: Pocketbeuter::ConfigFile

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/pocketbeuter/configfile.rb

Constant Summary collapse

CONFIG_NAME =
'.pocketbeuterrc'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConfigFile

Returns a new instance of ConfigFile.



10
11
12
13
# File 'lib/pocketbeuter/configfile.rb', line 10

def initialize
  @path = File.join(File.expand_path('~'), CONFIG_NAME)
  @config = load_config
end

Instance Attribute Details

#pathObject

Returns the value of attribute path.



8
9
10
# File 'lib/pocketbeuter/configfile.rb', line 8

def path
  @path
end

Instance Method Details

#[](node) ⇒ Object



15
16
17
# File 'lib/pocketbeuter/configfile.rb', line 15

def [](node)
  @config[node]
end

#[]=(subsec, sec) ⇒ Object



19
20
21
22
23
# File 'lib/pocketbeuter/configfile.rb', line 19

def []=(subsec, sec)
  @config[subsec] ||= {}
  @config[subsec].merge!(sec)
  save_config
end

#access_tokenObject



60
61
62
# File 'lib/pocketbeuter/configfile.rb', line 60

def access_token
  @config['account'][]['access_token']
end

#access_token=(token) ⇒ Object



64
65
66
67
# File 'lib/pocketbeuter/configfile.rb', line 64

def access_token=(token)
  @config['account'][] ||= 'access_token'
  @config['account'][]['access_token'] = token
end

#accountObject



25
26
27
# File 'lib/pocketbeuter/configfile.rb', line 25

def 
  @config['account']
end

#account=(value) ⇒ Object



29
30
31
32
# File 'lib/pocketbeuter/configfile.rb', line 29

def account=(value)
  @config['account'] ||= value.keys[0]
  @config['account'][value.keys[0]] = value[value.keys[0]]
end

#account_nameObject



34
35
36
# File 'lib/pocketbeuter/configfile.rb', line 34

def 
  @config['account'].keys[0]
end

#account_name=(name) ⇒ Object



38
39
40
# File 'lib/pocketbeuter/configfile.rb', line 38

def (name)
  @config['account'][name] = {}
end

#codeObject



78
79
80
# File 'lib/pocketbeuter/configfile.rb', line 78

def code
  @config['account'][]['code']
end

#code=(token) ⇒ Object



82
83
84
85
# File 'lib/pocketbeuter/configfile.rb', line 82

def code=(token)
  @config['account'][] ||= 'code'
  @config['account'][]['code'] = token
end

#consumer_keyObject



42
43
44
# File 'lib/pocketbeuter/configfile.rb', line 42

def consumer_key
  @config['account'][]['consumer_key']
end

#consumer_key=(key) ⇒ Object



46
47
48
49
# File 'lib/pocketbeuter/configfile.rb', line 46

def consumer_key=(key)
  @config['account'][] ||= 'consumer_key'
  @config['account'][]['consumer_key'] = key
end

#default_configObject



111
112
113
# File 'lib/pocketbeuter/configfile.rb', line 111

def default_config
  { 'options' => {}, 'account' => {}}
end

#empty?Boolean

Returns:

  • (Boolean)


119
120
121
# File 'lib/pocketbeuter/configfile.rb', line 119

def empty?
  @config == default_config
end

#load_configObject



93
94
95
96
97
98
99
100
101
102
# File 'lib/pocketbeuter/configfile.rb', line 93

def load_config
  require 'yaml'
  if YAML.load_file(@path)
    YAML.load_file(@path)
  else
    default_config
  end
rescue Errno::ENOENT
  default_config
end

#redirect_uriObject



51
52
53
# File 'lib/pocketbeuter/configfile.rb', line 51

def redirect_uri
  @config['account'][]['redirect_uri']
end

#redirect_uri=(uri) ⇒ Object



55
56
57
58
# File 'lib/pocketbeuter/configfile.rb', line 55

def redirect_uri=(uri)
  @config['account'][] ||= 'redirect_uri'
  @config['account'][]['redirect_uri'] = uri
end

#resetObject



115
116
117
# File 'lib/pocketbeuter/configfile.rb', line 115

def reset
  send(:initialize)
end

#save_configObject



104
105
106
107
108
109
# File 'lib/pocketbeuter/configfile.rb', line 104

def save_config
  require 'yaml'
  File.open(@path, File::RDWR | File::TRUNC | File::CREAT, 0600) do |f|
    f.write @config.to_yaml
  end
end

#usernameObject



69
70
71
# File 'lib/pocketbeuter/configfile.rb', line 69

def username
  @config['account'][]['username']
end

#username=(name) ⇒ Object



73
74
75
76
# File 'lib/pocketbeuter/configfile.rb', line 73

def username=(name)
  @config['account'][] ||= 'username'
  @config['account'][]['username'] = name
end