-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
configurable.rb
84 lines (71 loc) · 2.11 KB
/
configurable.rb
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
require 'forwardable'
require 'twitter/error/configuration_error'
module Twitter
module Configurable
extend Forwardable
attr_writer :consumer_key, :consumer_secret, :oauth_token, :oauth_token_secret
attr_accessor :endpoint, :connection_options, :identity_map, :middleware
def_delegator :options, :hash
class << self
def keys
@keys ||= [
:consumer_key,
:consumer_secret,
:oauth_token,
:oauth_token_secret,
:endpoint,
:connection_options,
:identity_map,
:middleware,
]
end
end
# Convenience method to allow configuration options to be set in a block
#
# @raise [Twitter::Error::ConfigurationError] Error is raised when supplied
# twitter credentials are not a String or Symbol.
def configure
yield self
validate_credential_type!
self
end
# @return [Boolean]
def credentials?
credentials.values.all?
end
def reset!
Twitter::Configurable.keys.each do |key|
instance_variable_set(:"@#{key}", Twitter::Default.options[key])
end
self
end
alias setup reset!
private
# @return [Hash]
def credentials
{
:consumer_key => @consumer_key,
:consumer_secret => @consumer_secret,
:token => @oauth_token,
:token_secret => @oauth_token_secret,
}
end
# @return [Hash]
def options
Hash[Twitter::Configurable.keys.map{|key| [key, instance_variable_get(:"@#{key}")]}]
end
# Ensures that all credentials set during configuration are of a
# valid type. Valid types are String and Symbol.
#
# @raise [Twitter::Error::ConfigurationError] Error is raised when
# supplied twitter credentials are not a String or Symbol.
def validate_credential_type!
credentials.each do |credential, value|
next if value.nil?
unless value.is_a?(String) || value.is_a?(Symbol)
raise(Error::ConfigurationError, "Invalid #{credential} specified: #{value} must be a string or symbol.")
end
end
end
end
end