Class: Impostor::Post

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config, auth) ⇒ Post

Post is initialized with the auth of the impostor



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

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

Instance Attribute Details

#authObject (readonly)

Returns the value of attribute auth.



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

def auth
  @auth
end

#configObject (readonly)

Returns the value of attribute config.



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

def config
  @config
end

Instance Method Details

#get_post_form(page) ⇒ Object

return the form used for posting a message from the reply page



80
81
82
# File 'lib/impostor/post.rb', line 80

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

#get_post_from_result(page) ⇒ Object

get post id from the result of posting the message form



107
108
109
# File 'lib/impostor/post.rb', line 107

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

#get_reply_page(uri) ⇒ Object

return the reply page that is fetched with the reply uri



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

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

#get_reply_uri(forum, topic) ⇒ Object

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



62
63
64
# File 'lib/impostor/post.rb', line 62

def get_reply_uri(forum, topic)
  raise Impostor::MissingTemplateMethodError.new("get_reply_uri must be implemented")
end

#post(forum, topic, message) ⇒ Object

post a message to the forum and topic thread post is comprised of the following template methods to allow implementation for specific forum applications

  • validate_post_input(forum, topic, message)

  • get_reply_uri(forum, topic)

  • get_reply_page(uri)

  • get_post_form(page)

  • set_message(form, message)

  • post_message(form)

  • get_post_from_result(page)

A hash of results is returned, having keys to the :forum, :topic, new :post id, :message, and :result



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/impostor/post.rb', line 31

def post(forum, topic, message)
  self.validate_post_input(forum, topic, message)
  self.auth.
  uri = self.get_reply_uri(forum, topic)
  page = get_reply_page(uri)
  form = get_post_form(page)
  set_message(form, message)
  page = post_message(form)
  post = get_post_from_result(page)

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

#post_message(form) ⇒ Object

post the message form



95
96
97
98
99
100
101
102
# File 'lib/impostor/post.rb', line 95

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

#set_message(form, message) ⇒ Object

set the message to reply with on the reply form



87
88
89
90
# File 'lib/impostor/post.rb', line 87

def set_message(form, message)
  form.message = message
  form
end

#validate_post_input(forum, topic, message) ⇒ Object

validate the inputs forum, topic, and message



51
52
53
54
55
56
# File 'lib/impostor/post.rb', line 51

def validate_post_input(forum, topic, message)
  raise Impostor::PostError.new("forum not set") unless forum
  raise Impostor::PostError.new("topic not set") unless topic
  raise Impostor::PostError.new("message not set") unless message
  true
end