Class: Tw::Conf

Inherits:
Object
  • Object
show all
Defined in:
lib/tw.rb,
lib/tw/conf.rb

Constant Summary collapse

REQUIRE_VERSION =
'0.1.0'

Class Method Summary collapse

Class Method Details

.[](key) ⇒ Object



15
16
17
# File 'lib/tw/conf.rb', line 15

def self.[](key)
  ENV[key] || conf[key]
end

.[]=(key, value) ⇒ Object



19
20
21
# File 'lib/tw/conf.rb', line 19

def self.[]=(key,value)
  conf[key] = value
end

.confObject



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/tw/conf.rb', line 31

def self.conf
  @@conf ||= (
              res = default
              if File.exist? self.conf_file
                begin
                  data = nil
                  self.open_conf_file do |f|
                    data = YAML::load f.read
                  end
                  if data['version'] < REQUIRE_VERSION
                    puts "This is tw version #{Tw::VERSION}."
                    puts "Your config file is old ("+data['version']+"). Reset tw settings?"
                    puts "[Y/n]"
                    res = data if STDIN.gets =~ /^n/i
                  else
                    res = data
                  end
                rescue => e
                  STDERR.puts e
                  File.delete self.conf_file
                end
              end
              res
              )
end

.conf_fileObject



23
24
25
# File 'lib/tw/conf.rb', line 23

def self.conf_file
  @@conf_file ||= "#{ENV['HOME']}/.tw.yml"
end

.conf_file=(fpath) ⇒ Object



27
28
29
# File 'lib/tw/conf.rb', line 27

def self.conf_file=(fpath)
  @@conf_file = fpath
end

.defaultObject



5
6
7
8
9
10
11
12
13
# File 'lib/tw/conf.rb', line 5

def self.default
  {
    'version' => Tw::VERSION,
    'consumer_key' => 'AYhhkOC8H2yTZyelz3uw',
    'consumer_secret' => '28Ba8YyFDSPgoCYAmH5ANqOmT6qVS8gIhKnUiDbIpU',
    'default_user' => nil,
    'users' => {}
  }
end

.saveObject



61
62
63
64
65
# File 'lib/tw/conf.rb', line 61

def self.save
  open_conf_file('w+') do |f|
    f.write conf.to_yaml
  end
end

.to_yamlObject



57
58
59
# File 'lib/tw/conf.rb', line 57

def self.to_yaml
  self.conf.to_yaml
end

.update_twitter_config(force_update = false) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/tw/conf.rb', line 67

def self.update_twitter_config(force_update=false)
  if self['twitter_config'].kind_of? Hash and
      self['twitter_config']['last_updated_at']+60*60*24 > Time.now.to_i and
      !force_update
    return
  end
  self['twitter_config'] = {}
  self['twitter_config']['short_url_length'] = Tw::Client.client.configuration.short_url_length
  self['twitter_config']['short_url_length_https'] = Tw::Client.client.configuration.short_url_length_https
  self['twitter_config']['last_updated_at'] = Time.now.to_i
  self.save
end