Class: Impostor::Topic

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config, auth) ⇒ Topic

Topic is initialized with the auth of the impostor



9
10
11
12
13
# File 'lib/impostor/topic.rb', line 9

def initialize(config, auth)
  @config = config
  @auth = auth
  self.extend eval("Impostor::#{config.type.to_s.capitalize}::Topic")
end

Instance Attribute Details

#authObject (readonly)

Returns the value of attribute auth.



4
5
6
# File 'lib/impostor/topic.rb', line 4

def auth
  @auth
end

#configObject (readonly)

Returns the value of attribute config.



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

def config
  @config
end

Instance Method Details

#get_new_topic_form(page) ⇒ Object

Get the the new topic form on the page



79
80
81
# File 'lib/impostor/topic.rb', line 79

def get_new_topic_form(page)
  raise Impostor::MissingTemplateMethodError.new("get_new_topic_form must be implemented")
end

#get_new_topic_page(uri) ⇒ Object

Get the page that has the form for new topics referenced by the uri



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

def get_new_topic_page(uri)
  begin
    self.config.agent.get(uri)
  rescue StandardError => err
    raise Impostor::TopicError.new(err)
  end
end

#get_new_topic_uri(forum, subject, message) ⇒ Object

return a uri used to fetch the new topic page based on the forum, subject, and message



61
62
63
# File 'lib/impostor/topic.rb', line 61

def get_new_topic_uri(forum, subject, message)
  raise Impostor::MissingTemplateMethodError.new("get_new_topic_uri must be implemented")
end

#get_topic_from_result(page) ⇒ Object

Get the new topic identifier from the result page



112
113
114
# File 'lib/impostor/topic.rb', line 112

def get_topic_from_result(page)
  raise Impostor::MissingTemplateMethodError.new("get_topic_from_result must be implemented")
end

#new_topic(forum, subject, message) ⇒ Object

create a new topic in the forum with the subject title and initial message

  • validate_new_topic_input(forum, subject, message)

  • get_new_topic_uri(forum, subject, message)

  • get_new_topic_page(uri)

  • get_new_topic_form(page)

  • set_subject_and_message(form, subject, message)

  • post_new_topic(form)

  • validate_new_topic_result(page)

  • get_topic_from_result(page)



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/impostor/topic.rb', line 27

def new_topic(forum, subject, message)
  self.validate_topic_input(forum, subject, message)
  self.auth.
  uri = self.get_new_topic_uri(forum, subject, message)
  page = self.get_new_topic_page(uri)
  form = self.get_new_topic_form(page)
  self.set_subject_and_message(form, subject, message)
  page = self.post_new_topic(form)
  page = self.validate_new_topic_result(page)
  topic = self.get_topic_from_result(page)

  self.config.add_subject(forum, topic, subject)

  { :forum => forum,
    :topic => topic,
    :subject => subject,
    :message => message,
    :result => true }
end

#post_new_topic(form) ⇒ Object

Post the new topic that is contained on the form



93
94
95
96
97
98
99
100
# File 'lib/impostor/topic.rb', line 93

def post_new_topic(form)
  begin
    config.sleep_before_post
    form.submit
  rescue StandardError => err
    raise Impostor::TopicError.new(err)
  end
end

#set_subject_and_message(form, subject, message) ⇒ Object

Set the subject and message on the new topic form



86
87
88
# File 'lib/impostor/topic.rb', line 86

def set_subject_and_message(form, subject, message)
  raise Impostor::MissingTemplateMethodError.new("set_subject_and_message must be implemented")
end

#validate_new_topic_result(page) ⇒ Object

Validate the result of posting the new topic



105
106
107
# File 'lib/impostor/topic.rb', line 105

def validate_new_topic_result(page)
  raise Impostor::MissingTemplateMethodError.new("validate_new_topic_result must be implemented")
end

#validate_topic_input(forum, subject, message) ⇒ Object

validate the inputs forum, topic, and message



50
51
52
53
54
55
# File 'lib/impostor/topic.rb', line 50

def validate_topic_input(forum, subject, message)
  raise Impostor::TopicError.new("forum not set") unless forum
  raise Impostor::TopicError.new("subject not set") unless subject
  raise Impostor::TopicError.new("message not set") unless message
  true
end