rcfile.rb
2.1 KB
1
2
3
4
5
6
7
8
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
module Twurl
class RCFile
class << self
def directory
@@directory ||= File.expand_path('~')
end
def directory=(dir)
@@directory = dir
end
def file_path
Stream.file_path
end
def load
YAML.load_file(file_path)
rescue Errno::ENOENT
default_rcfile_structure
end
def default_rcfile_structure
{'profiles' => {}, 'configuration' => {}}
end
end
attr_reader :data
def initialize
@data = self.class.load
end
def set_path(p)
puts "entro set path #{p}"
@@file_path = p
end
def empty?
data == self.class.default_rcfile_structure
end
def save
File.open(self.class.file_path, File::RDWR|File::CREAT|File::TRUNC, 0600) do |rcfile|
rcfile.write data.to_yaml
end
end
def [](username)
profiles[username]
end
def profiles
data['profiles']
end
def default_profile
configuration['default_profile']
end
def default_profile=(profile)
configuration['default_profile'] = [profile.username, profile.consumer_key]
end
def configuration
data['configuration']
end
def alias(name, path)
data['aliases'] ||= {}
data['aliases'][name] = path
save
end
def aliases
data['aliases']
end
def alias_from_options(options)
options.subcommands.each do |potential_alias|
if path = alias_from_name(potential_alias)
break path
end
end
end
def alias_from_name(name)
aliases[name]
end
def has_oauth_profile_for_username_with_consumer_key?(username, consumer_key)
user_profiles = self[username]
!user_profiles.nil? && !user_profiles[consumer_key].nil?
end
def <<(oauth_client)
client_from_file = self[oauth_client.username] || {}
client_from_file[oauth_client.consumer_key] = oauth_client.to_hash
(profiles[oauth_client.username] ||= {}).update(client_from_file)
self.default_profile = oauth_client unless default_profile
save
end
end
end