Class: Twurl::RCFile
- Inherits:
-
Object
- Object
- Twurl::RCFile
- Defined in:
- lib/twurl/rcfile.rb
Constant Summary collapse
- FILE =
'.twurlrc'
Instance Attribute Summary collapse
-
#data ⇒ Object
readonly
Returns the value of attribute data.
Class Method Summary collapse
- .default_rcfile_structure ⇒ Object
- .directory ⇒ Object
- .directory=(dir) ⇒ Object
- .file_path ⇒ Object
- .load ⇒ Object
Instance Method Summary collapse
- #<<(oauth_client) ⇒ Object
- #[](username) ⇒ Object
- #alias(name, path) ⇒ Object
- #alias_from_name(name) ⇒ Object
- #alias_from_options(options) ⇒ Object
- #aliases ⇒ Object
- #bearer_token(consumer_key, bearer_token) ⇒ Object
- #bearer_tokens ⇒ Object
- #configuration ⇒ Object
- #default_profile ⇒ Object
- #default_profile=(profile) ⇒ Object
- #default_profile_consumer_key ⇒ Object
- #empty? ⇒ Boolean
- #has_bearer_token_for_consumer_key?(consumer_key) ⇒ Boolean
- #has_oauth_profile_for_username_with_consumer_key?(username, consumer_key) ⇒ Boolean
-
#initialize ⇒ RCFile
constructor
A new instance of RCFile.
- #profiles ⇒ Object
- #save ⇒ Object
Constructor Details
#initialize ⇒ RCFile
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
#data ⇒ Object (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_structure ⇒ Object
23 24 25 |
# File 'lib/twurl/rcfile.rb', line 23 def default_rcfile_structure {'profiles' => {}, 'configuration' => {}} end |
.directory ⇒ Object
5 6 7 |
# File 'lib/twurl/rcfile.rb', line 5 def directory @@directory ||= File.('~') end |
.directory=(dir) ⇒ Object
9 10 11 |
# File 'lib/twurl/rcfile.rb', line 9 def directory=(dir) @@directory = dir end |
.file_path ⇒ Object
13 14 15 |
# File 'lib/twurl/rcfile.rb', line 13 def file_path File.join(directory, FILE) end |
.load ⇒ Object
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 () .subcommands.each do |potential_alias| if path = alias_from_name(potential_alias) break path end end end |
#aliases ⇒ Object
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_tokens ⇒ Object
84 85 86 |
# File 'lib/twurl/rcfile.rb', line 84 def bearer_tokens data['bearer_tokens'] end |
#configuration ⇒ Object
64 65 66 |
# File 'lib/twurl/rcfile.rb', line 64 def configuration data['configuration'] end |
#default_profile ⇒ Object
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_key ⇒ Object
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
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
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
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 |
#profiles ⇒ Object
47 48 49 |
# File 'lib/twurl/rcfile.rb', line 47 def profiles data['profiles'] end |
#save ⇒ Object
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 |