Class: Kronk::OAuthConfig

Inherits:
Object
  • Object
show all
Defined in:
lib/kronk/oauth_config.rb

Constant Summary collapse

SELECTED_KEY =
'selected'
ACCOUNTS_KEY =
'accounts'

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config = nil) ⇒ OAuthConfig

Returns a new instance of OAuthConfig.



13
14
15
# File 'lib/kronk/oauth_config.rb', line 13

def initialize config=nil
  @config = config || {}
end

Class Method Details

.load_file(yaml_file) ⇒ Object



8
9
10
# File 'lib/kronk/oauth_config.rb', line 8

def self.load_file yaml_file
  new( YAML.load_file(yaml_file) )
end

Instance Method Details

#active_name_for_host(host) ⇒ Object



93
94
95
96
97
98
# File 'lib/kronk/oauth_config.rb', line 93

def active_name_for_host host
  config = config_for_host(host)
  return unless config

  return config[SELECTED_KEY]
end

#config_for_host(host) ⇒ Object



111
112
113
# File 'lib/kronk/oauth_config.rb', line 111

def config_for_host host
  @config[host]
end

#each(&block) ⇒ Object



126
127
128
129
130
131
132
133
# File 'lib/kronk/oauth_config.rb', line 126

def each &block
  @config.each do |host, data|
    configs = data[ACCOUNTS_KEY]
    configs.each do |name, config|
      block.call(host, name, config)
    end
  end
end

#empty?Boolean

Returns:

  • (Boolean)


116
117
118
# File 'lib/kronk/oauth_config.rb', line 116

def empty?
  @config.empty?
end

#get(name, host) ⇒ Object



28
29
30
31
# File 'lib/kronk/oauth_config.rb', line 28

def get name, host
  config = config_for_host(host)
  config && config[ACCOUNTS_KEY] && config[ACCOUNTS_KEY][name] && config[ACCOUNTS_KEY][name].dup
end

#get_active_for_host(host) ⇒ Object



77
78
79
80
# File 'lib/kronk/oauth_config.rb', line 77

def get_active_for_host host
  name = active_name_for_host(host)
  return get_symbolized(name, host)
end

#get_symbolized(name, host) ⇒ Object



34
35
36
37
38
39
40
41
42
43
# File 'lib/kronk/oauth_config.rb', line 34

def get_symbolized name, host
  data = get(name, host)
  return unless data
  
  data[:consumer_key]     = data.delete('consumer_key')
  data[:consumer_secret]  = data.delete('consumer_secret')
  data[:token]            = data.delete('token')
  data[:token_secret]     = data.delete('token_secret')
  return data
end

#has_host?(host) ⇒ Boolean

Returns:

  • (Boolean)


23
24
25
# File 'lib/kronk/oauth_config.rb', line 23

def has_host? host
  !!config_for_host(host)
end

#has_name_for_host?(name, host) ⇒ Boolean

Returns:

  • (Boolean)


18
19
20
# File 'lib/kronk/oauth_config.rb', line 18

def has_name_for_host? name, host
  !!get(name, host)
end

#hostsObject



106
107
108
# File 'lib/kronk/oauth_config.rb', line 106

def hosts
  @config.keys
end

#names_for_host(host) ⇒ Object



101
102
103
# File 'lib/kronk/oauth_config.rb', line 101

def names_for_host host
  @config[host] && @config[host][ACCOUNTS_KEY] && @config[host][ACCOUNTS_KEY].keys || []
end

#remove(host, name = nil) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/kronk/oauth_config.rb', line 52

def remove host, name=nil
  if name
    config = config_for_host(host)
    config[ACCOUNTS_KEY].delete(name)
    if config[ACCOUNTS_KEY].empty?
      @config.delete(host)
    elsif config[SELECTED_KEY] == name
      config[SELECTED_KEY] = nil
    end 

  else
    @config.delete(host)
  end
end

#rename(host, name, new_name) ⇒ Object



68
69
70
71
72
73
74
# File 'lib/kronk/oauth_config.rb', line 68

def rename host, name, new_name
  selected = active_name_for_host(host) == name
  config = get(name, host)
  remove(host, name)
  set(new_name, host, config)
  set_active_for_host(new_name, host) if selected
end

#save_file(yaml_file) ⇒ Object



121
122
123
# File 'lib/kronk/oauth_config.rb', line 121

def save_file yaml_file
  File.open(yaml_file, "w+") {|f| f.write @config.to_yaml }
end

#set(name, host, config) ⇒ Object



46
47
48
49
# File 'lib/kronk/oauth_config.rb', line 46

def set name, host, config
  @config[host] ||= {SELECTED_KEY => name, ACCOUNTS_KEY => {}}
  @config[host][ACCOUNTS_KEY][name] = config
end

#set_active_for_host(name, host) ⇒ Object



83
84
85
86
87
88
89
90
# File 'lib/kronk/oauth_config.rb', line 83

def set_active_for_host name, host
  config = config_for_host(host)
  return false unless config
  return false unless config[ACCOUNTS_KEY] && config[ACCOUNTS_KEY][name] || name.nil?

  config[SELECTED_KEY] = name
  return true
end