Class: Twurl::RCFile

Inherits:
Object
  • Object
show all
Defined in:
lib/twurl/rcfile.rb

Constant Summary collapse

FILE =
'.twurlrc'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeRCFile

Returns a new instance of RCFile.



29
30
31
# File 'lib/twurl/rcfile.rb', line 29

def initialize
  @data = self.class.load
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



28
29
30
# File 'lib/twurl/rcfile.rb', line 28

def data
  @data
end

Class Method Details

.default_rcfile_structureObject



23
24
25
# File 'lib/twurl/rcfile.rb', line 23

def default_rcfile_structure
  {'profiles' => {}, 'configuration' => {}}
end

.directoryObject



5
6
7
# File 'lib/twurl/rcfile.rb', line 5

def directory
  @@directory ||= File.expand_path('~')
end

.directory=(dir) ⇒ Object



9
10
11
# File 'lib/twurl/rcfile.rb', line 9

def directory=(dir)
  @@directory = dir
end

.file_pathObject



13
14
15
# File 'lib/twurl/rcfile.rb', line 13

def file_path
  File.join(directory, FILE)
end

.loadObject



17
18
19
20
21
# File 'lib/twurl/rcfile.rb', line 17

def load
  YAML.load_file(file_path)
rescue Errno::ENOENT
  default_rcfile_structure
end

Instance Method Details

#<<(oauth_client) ⇒ Object



90
91
92
93
94
95
96
# File 'lib/twurl/rcfile.rb', line 90

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

#[](username) ⇒ Object



43
44
45
# File 'lib/twurl/rcfile.rb', line 43

def [](username)
  profiles[username]
end

#alias(name, path) ⇒ Object



63
64
65
66
67
# File 'lib/twurl/rcfile.rb', line 63

def alias(name, path)
  data['aliases'] ||= {}
  data['aliases'][name] = path
  save
end

#alias_from_name(name) ⇒ Object



81
82
83
# File 'lib/twurl/rcfile.rb', line 81

def alias_from_name(name)
  aliases[name]
end

#alias_from_options(options) ⇒ Object



73
74
75
76
77
78
79
# File 'lib/twurl/rcfile.rb', line 73

def alias_from_options(options)
  options.subcommands.each do |potential_alias|
    if path = alias_from_name(potential_alias)
      break path
    end
  end
end

#aliasesObject



69
70
71
# File 'lib/twurl/rcfile.rb', line 69

def aliases
  data['aliases']
end

#configurationObject



59
60
61
# File 'lib/twurl/rcfile.rb', line 59

def configuration
  data['configuration']
end

#default_profileObject



51
52
53
# File 'lib/twurl/rcfile.rb', line 51

def default_profile
  configuration['default_profile']
end

#default_profile=(profile) ⇒ Object



55
56
57
# File 'lib/twurl/rcfile.rb', line 55

def default_profile=(profile)
  configuration['default_profile'] = [profile.username, profile.consumer_key]
end

#empty?Boolean

Returns:

  • (Boolean)


33
34
35
# File 'lib/twurl/rcfile.rb', line 33

def empty?
  data == self.class.default_rcfile_structure
end

#has_oauth_profile_for_username_with_consumer_key?(username, consumer_key) ⇒ Boolean

Returns:

  • (Boolean)


85
86
87
88
# File 'lib/twurl/rcfile.rb', line 85

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

#profilesObject



47
48
49
# File 'lib/twurl/rcfile.rb', line 47

def profiles
  data['profiles']
end

#saveObject



37
38
39
40
41
# File 'lib/twurl/rcfile.rb', line 37

def save
  File.open(self.class.file_path, File::RDWR|File::CREAT, 0600) do |rcfile|
    rcfile.write data.to_yaml
  end
end