Class: Impostor::Config
- Inherits:
-
Object
- Object
- Impostor::Config
- Defined in:
- lib/impostor/config.rb
Instance Attribute Summary collapse
-
#agent ⇒ Object
readonly
Returns the value of attribute agent.
-
#topics ⇒ Object
readonly
Returns the value of attribute topics.
Instance Method Summary collapse
-
#add_subject(forum, topic, name) ⇒ Object
Add subject to topics hash.
-
#app_root ⇒ Object
Gets the application root of the application such as example.com/phpbb or example.com/forums.
-
#config(key) ⇒ Object
Access the current config and key it without regard for symbols or strings.
-
#cookie_jar ⇒ Object
is a yaml file for Mechanize::CookieJar.
-
#get_subject(forum, topic) ⇒ Object
Get the topic name (subject) based on forum and topic ids.
-
#initialize(config) ⇒ Config
constructor
A new instance of Config.
-
#load_topics ⇒ Object
Load the topics that the impostor already knows about.
-
#login_page ⇒ Object
Get the login page for the application.
-
#password ⇒ Object
Get the password for the application.
-
#save_cookie_jar ⇒ Object
Save the cookie jar.
-
#save_topics ⇒ Object
Save the topics.
-
#setup_agent ⇒ Object
Sets up the mechanize agent initialized with cookie jar file specified by the :cookie_jar configuration parameter if it exists.
-
#sleep_before_post ⇒ Object
Some forums require a bit of delay before posting to not have the post be considered coming from a bot.
-
#topics_cache ⇒ Object
Get the topics cache.
-
#type ⇒ Object
The impostor type, as specified in the config.
-
#user_agent ⇒ Object
A Mechanize user agent name, see the mechanize documentation ‘Linux Mozilla’, ‘Mac Safari’, ‘Windows IE 7’, etc.
-
#username ⇒ Object
Get the username for the application.
-
#validate_keys(*keys) ⇒ Object
Validates expected keys are in the config.
Constructor Details
#initialize(config) ⇒ Config
Returns a new instance of Config.
6 7 8 9 10 11 |
# File 'lib/impostor/config.rb', line 6 def initialize(config) @config = config validate_keys(:type, :username, :password, :app_root, :login_page) setup_agent load_topics end |
Instance Attribute Details
#agent ⇒ Object (readonly)
Returns the value of attribute agent.
3 4 5 |
# File 'lib/impostor/config.rb', line 3 def agent @agent end |
#topics ⇒ Object (readonly)
Returns the value of attribute topics.
4 5 6 |
# File 'lib/impostor/config.rb', line 4 def topics @topics end |
Instance Method Details
#add_subject(forum, topic, name) ⇒ Object
Add subject to topics hash
67 68 69 70 71 72 73 74 75 |
# File 'lib/impostor/config.rb', line 67 def add_subject(forum, topic, name) forum = forum.to_s topic = topic.to_s if self.topics[forum].nil? self.topics[forum] = {topic => name} else self.topics[forum][topic] = name end end |
#app_root ⇒ Object
Gets the application root of the application such as example.com/phpbb or example.com/forums
109 110 111 |
# File 'lib/impostor/config.rb', line 109 def app_root self.config(:app_root) end |
#config(key) ⇒ Object
Access the current config and key it without regard for symbols or strings
26 27 28 |
# File 'lib/impostor/config.rb', line 26 def config(key) @config[key.to_sym] || @config[key.to_s] end |
#cookie_jar ⇒ Object
is a yaml file for Mechanize::CookieJar
152 153 154 |
# File 'lib/impostor/config.rb', line 152 def self.config(:cookie_jar) end |
#get_subject(forum, topic) ⇒ Object
Get the topic name (subject) based on forum and topic ids
80 81 82 83 84 |
# File 'lib/impostor/config.rb', line 80 def get_subject(forum, topic) forum = forum.to_s topic = topic.to_s self.topics[forum] ? self.topics[forum][topic] : nil end |
#load_topics ⇒ Object
Load the topics that the impostor already knows about
55 56 57 58 59 60 61 62 |
# File 'lib/impostor/config.rb', line 55 def load_topics cache = self.topics_cache || "" if File::exist?(cache) @topics = YAML::load_file(cache) else @topics = Hash.new end end |
#login_page ⇒ Object
Get the login page for the application
123 124 125 |
# File 'lib/impostor/config.rb', line 123 def login_page URI.join(app_root, self.config(:login_page)) end |
#password ⇒ Object
Get the password for the application
137 138 139 |
# File 'lib/impostor/config.rb', line 137 def password self.config(:password) end |
#save_cookie_jar ⇒ Object
Save the cookie jar
101 102 103 |
# File 'lib/impostor/config.rb', line 101 def self.agent..save_as(self.) if self. end |
#save_topics ⇒ Object
Save the topics
89 90 91 92 93 94 95 96 |
# File 'lib/impostor/config.rb', line 89 def save_topics cache = self.topics_cache || "" if File::exist?(cache) File.open(cache, 'w') do |out| YAML.dump(self.topics, out) end end end |
#setup_agent ⇒ Object
Sets up the mechanize agent initialized with cookie jar file specified by the :cookie_jar configuration parameter if it exists
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/impostor/config.rb', line 34 def setup_agent @agent = Mechanize.new do |mechanize| mechanize. = !!self.config(:follow_meta_refresh) if logger = self.config(:logger) if File.exist?(logger.to_s) mechanize.log = Logger.new(logger.to_s) elsif logger == "STDOUT" mechanize.log = Logger.new(STDOUT) else mechanize.log = logger end end end @agent.user_agent_alias = self.user_agent if self.user_agent # jar is a yaml file @agent..load() if && File.exist?() end |
#sleep_before_post ⇒ Object
Some forums require a bit of delay before posting to not have the post be considered coming from a bot.
167 168 169 |
# File 'lib/impostor/config.rb', line 167 def sleep_before_post sleep self.config(:sleep_before_post).to_i end |
#topics_cache ⇒ Object
Get the topics cache
116 117 118 |
# File 'lib/impostor/config.rb', line 116 def topics_cache self.config(:topics_cache) end |
#type ⇒ Object
The impostor type, as specified in the config
159 160 161 |
# File 'lib/impostor/config.rb', line 159 def type self.config(:type) end |
#user_agent ⇒ Object
A Mechanize user agent name, see the mechanize documentation ‘Linux Mozilla’, ‘Mac Safari’, ‘Windows IE 7’, etc.
145 146 147 |
# File 'lib/impostor/config.rb', line 145 def user_agent self.config(:user_agent) || 'Mechanize' end |
#username ⇒ Object
Get the username for the application
130 131 132 |
# File 'lib/impostor/config.rb', line 130 def username self.config(:username) end |
#validate_keys(*keys) ⇒ Object
Validates expected keys are in the config
16 17 18 19 20 21 |
# File 'lib/impostor/config.rb', line 16 def validate_keys(*keys) keys.each do |key| val = self.config(key) raise Impostor::ConfigError.new("Missing key '#{key}' in configuration") unless val end end |