Class: RMB::MechanizeSubmitter

Inherits:
Submitter show all
Defined in:
lib/mechanize_submitter.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#agentObject

Returns the value of attribute agent.



7
8
9
# File 'lib/mechanize_submitter.rb', line 7

def agent
  @agent
end

#daemon_nameObject

Returns the value of attribute daemon_name.



7
8
9
# File 'lib/mechanize_submitter.rb', line 7

def daemon_name
  @daemon_name
end

#delivery_urlObject

Returns the value of attribute delivery_url.



7
8
9
# File 'lib/mechanize_submitter.rb', line 7

def delivery_url
  @delivery_url
end

#loggerObject

Returns the value of attribute logger.



7
8
9
# File 'lib/mechanize_submitter.rb', line 7

def logger
  @logger
end

#login_urlObject

Returns the value of attribute login_url.



7
8
9
# File 'lib/mechanize_submitter.rb', line 7

def 
  @login_url
end

#passwordObject

Returns the value of attribute password.



7
8
9
# File 'lib/mechanize_submitter.rb', line 7

def password
  @password
end

#userObject

Returns the value of attribute user.



7
8
9
# File 'lib/mechanize_submitter.rb', line 7

def user
  @user
end

Instance Method Details

#connectObject

connect Uses the login_url property, along with the user and password properties, to log in to the rails app.



31
32
33
34
# File 'lib/mechanize_submitter.rb', line 31

def connect
  super
  #this code requires completion of the User controller in the main app
end

#marshal_message_body(message) ⇒ Object

marshal_message_body Extracts the message body from the rest of the message, and marshals it into a file. The name of this file will be submitted to the rails app, along with other message attributes.



38
39
40
41
42
43
44
# File 'lib/mechanize_submitter.rb', line 38

def marshal_message_body(message)
  file = File.join("#{@hash[:working_dir]}", "tmp", "messages", "#{daemon_name}_#{message.headers["timestamp"]}.message")
  File.open(file, "w+") do |f|
    Marshal.dump(message.body, f)
  end
  file
end

#properties=(hash) ⇒ Object

properties=(hash) Accepts a hash object containing all of the configuration properties required. These properties are copied into instance variables.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/mechanize_submitter.rb', line 11

def properties=(hash)
  super
  @hash = hash
  @daemon_name = "#{RMB::Properties.daemon_prefix}#{hash[:key]}"
  submitter = hash[:submitter]
  @user = submitter[:user] || ""
  @password = submitter[:password] || ""
  @login_url = submitter[:login_url] || ""
  @delivery_url = submitter[:delivery_url] || ""
  begin
    @agent = WWW::Mechanize.new 
    @agent.user_agent_alias = 'Linux Mozilla'
  rescue Exception
    logger.fatal "#{__FILE__}:#{__LINE__} Exception #{$!}"
    raise
  end
end

#send(message) ⇒ Object

send This method uses the delivery_url property to issue a get request to retrieve the new document form from the rails app. The form is filled in with message attributes and submitted to the rails app for processing.



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/mechanize_submitter.rb', line 48

def send(message)
  super
  file = marshal_message_body(message)
  begin
    page = agent.get(delivery_url)
  rescue Exception
    logger.fatal "#{__FILE__}:#{__LINE__} Exception #{$!}"
    raise
  end
  form = page.forms.first
  
  # I can't seem to make the Mechanize code recognize fields as attributes, so
  # I am forced to treat them as an array
  form.fields[1].value = @hash[:key]
  form.fields[2].value = message.headers["destination"]
  form.fields[3].value = message.headers["message-id"]
  form.fields[4].value = message.headers["content-type"]
  form.fields[5].value = message.headers["priority"]
  form.fields[6].value = message.headers["content-length"]
  form.fields[7].value = message.headers["timestamp"]
  form.fields[8].value = message.headers["expires"]
  form.fields[9].value = file
  
  #logger.info "final form: #{form.inspect}"
  
  #submit the form
  begin
    page = agent.submit(form)
  rescue Exception
    logger.fatal "#{__FILE__}:#{__LINE__} Exception #{$!}"
    raise
  end
end