Class: Impostor::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/impostor/config.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#agentObject (readonly)

Returns the value of attribute agent.



3
4
5
# File 'lib/impostor/config.rb', line 3

def agent
  @agent
end

#topicsObject (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



66
67
68
69
70
71
72
73
74
# File 'lib/impostor/config.rb', line 66

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_rootObject

Gets the application root of the application such as example.com/phpbb or example.com/forums



108
109
110
# File 'lib/impostor/config.rb', line 108

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

is a yaml file for Mechanize::CookieJar



151
152
153
# File 'lib/impostor/config.rb', line 151

def cookie_jar
  self.config(:cookie_jar)
end

#get_subject(forum, topic) ⇒ Object

Get the topic name (subject) based on forum and topic ids



79
80
81
82
83
# File 'lib/impostor/config.rb', line 79

def get_subject(forum, topic)
  forum = forum.to_s
  topic = topic.to_s
  self.topics[forum] ? self.topics[forum][topic] : nil
end

#load_topicsObject

Load the topics that the impostor already knows about



54
55
56
57
58
59
60
61
# File 'lib/impostor/config.rb', line 54

def load_topics
  cache = self.topics_cache || ""
  if File::exist?(cache)
    @topics = YAML::load_file(cache)
  else
    @topics = Hash.new
  end
end

#login_pageObject

Get the login page for the application



122
123
124
# File 'lib/impostor/config.rb', line 122

def 
  URI.join(app_root, self.config(:login_page))
end

#passwordObject

Get the password for the application



136
137
138
# File 'lib/impostor/config.rb', line 136

def password
  self.config(:password)
end

Save the cookie jar



100
101
102
# File 'lib/impostor/config.rb', line 100

def save_cookie_jar
  self.agent.cookie_jar.save_as(self.cookie_jar) if self.cookie_jar
end

#save_topicsObject

Save the topics



88
89
90
91
92
93
94
95
# File 'lib/impostor/config.rb', line 88

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_agentObject

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
# File 'lib/impostor/config.rb', line 34

def setup_agent
  @agent = Mechanize.new do |mechanize|
    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.cookie_jar.load(cookie_jar) if cookie_jar && File.exist?(cookie_jar)
end

#sleep_before_postObject

Some forums require a bit of delay before posting to not have the post be considered coming from a bot.



166
167
168
# File 'lib/impostor/config.rb', line 166

def sleep_before_post
  sleep self.config(:sleep_before_post).to_i
end

#topics_cacheObject

Get the topics cache



115
116
117
# File 'lib/impostor/config.rb', line 115

def topics_cache
  self.config(:topics_cache)
end

#typeObject

The impostor type, as specified in the config



158
159
160
# File 'lib/impostor/config.rb', line 158

def type
  self.config(:type)
end

#user_agentObject

A Mechanize user agent name, see the mechanize documentation ‘Linux Mozilla’, ‘Mac Safari’, ‘Windows IE 7’, etc.



144
145
146
# File 'lib/impostor/config.rb', line 144

def user_agent
  self.config(:user_agent) || 'Mechanize'
end

#usernameObject

Get the username for the application



129
130
131
# File 'lib/impostor/config.rb', line 129

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