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



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_rootObject

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

is a yaml file for Mechanize::CookieJar



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

def cookie_jar
  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_topicsObject

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_pageObject

Get the login page for the application



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

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

#passwordObject

Get the password for the application



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

def password
  self.config(:password)
end

Save the cookie jar



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

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

#save_topicsObject

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

def setup_agent
  @agent = Mechanize.new do |mechanize|
    mechanize.follow_meta_refresh = !!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.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.



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

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

#topics_cacheObject

Get the topics cache



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

def topics_cache
  self.config(:topics_cache)
end

#typeObject

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_agentObject

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

#usernameObject

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