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



109
110
111
112
113
114
115
# File 'lib/twurl/rcfile.rb', line 109

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



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

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

#alias_from_name(name) ⇒ Object



96
97
98
# File 'lib/twurl/rcfile.rb', line 96

def alias_from_name(name)
  aliases[name]
end

#alias_from_options(options) ⇒ Object



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

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

#aliasesObject



74
75
76
# File 'lib/twurl/rcfile.rb', line 74

def aliases
  data['aliases'] ||= {}
end

#bearer_token(consumer_key, bearer_token) ⇒ Object



78
79
80
81
82
# File 'lib/twurl/rcfile.rb', line 78

def bearer_token(consumer_key, bearer_token)
  data['bearer_tokens'] ||= {}
  data['bearer_tokens'][consumer_key] = bearer_token
  save
end

#bearer_tokensObject



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

def bearer_tokens
  data['bearer_tokens']
end

#configurationObject



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

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



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

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

#default_profile_consumer_keyObject



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

def default_profile_consumer_key
  username, consumer_key = configuration['default_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_bearer_token_for_consumer_key?(consumer_key) ⇒ Boolean

Returns:

  • (Boolean)


105
106
107
# File 'lib/twurl/rcfile.rb', line 105

def has_bearer_token_for_consumer_key?(consumer_key)
  bearer_tokens.nil? ? false : bearer_tokens.to_hash.has_key?(consumer_key)
end

#has_oauth_profile_for_username_with_consumer_key?(username, consumer_key) ⇒ Boolean

Returns:

  • (Boolean)


100
101
102
103
# File 'lib/twurl/rcfile.rb', line 100

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|File::TRUNC, 0600) do |rcfile|
    rcfile.write data.to_yaml
  end
end