Class: Manhunt::Message

Inherits:
Object
  • Object
show all
Includes:
ActiveModel::Serializers::JSON, ActiveModel::Serializers::Xml
Defined in:
lib/manhunt/message.rb

Constant Summary collapse

GUID_REGEX =
/[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}/
PROFILE_NAME_REGEX =
/setWindow\('profile\/(\w+)','profile'\);/
USER_ID_REGEX =
/modalOpen\('list\/modalAddBuddy',\{buddy:(\d+)\}\);/

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type, document, format = nil) ⇒ Message

Returns a new instance of Message.



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/manhunt/message.rb', line 36

def initialize(type, document, format = nil)
  @format = format
  @row = document
  
  case type
  when :header
    columns = (document/"td")
          
    @subject = (document/".subject").inner_text.strip
    @online = (document/".buddyOnline").any?
    @from =  columns[@format.index(:from)].inner_text
    @state = :unread
    @state = :read if (document/".messageRead").any?
    @state = :replied if (document/".messageReplied").any?
    @id = GUID_REGEX.match((document/".subject a").first.attributes['href'])[0]
    @date = Time.at((columns[@format.index(:date)]/".LocalTime").first.attributes['gmt_time'].to_i)
  when :message
    @body = (document/"#msgTextDiv").inner_html
    @subject = (document/".headerColored").inner_text.strip
    @from = PROFILE_NAME_REGEX.match((document/"#readRecipProfLink a").first.attributes['onclick'])[1]
    @from_id = USER_ID_REGEX.match((document/"#buddyButton").first.attributes['onclick'])[1]
  end
end

Instance Attribute Details

#bodyObject (readonly)

Returns the value of attribute body.



9
10
11
# File 'lib/manhunt/message.rb', line 9

def body
  @body
end

#dateObject (readonly)

Returns the value of attribute date.



9
10
11
# File 'lib/manhunt/message.rb', line 9

def date
  @date
end

#formatObject (readonly)

Returns the value of attribute format.



9
10
11
# File 'lib/manhunt/message.rb', line 9

def format
  @format
end

#fromObject (readonly)

Returns the value of attribute from.



9
10
11
# File 'lib/manhunt/message.rb', line 9

def from
  @from
end

#from_idObject (readonly)

Returns the value of attribute from_id.



9
10
11
# File 'lib/manhunt/message.rb', line 9

def from_id
  @from_id
end

#idObject (readonly)

Returns the value of attribute id.



9
10
11
# File 'lib/manhunt/message.rb', line 9

def id
  @id
end

#onlineObject (readonly)

Returns the value of attribute online.



9
10
11
# File 'lib/manhunt/message.rb', line 9

def online
  @online
end

#rowObject (readonly)

Returns the value of attribute row.



9
10
11
# File 'lib/manhunt/message.rb', line 9

def row
  @row
end

#stateObject (readonly)

Returns the value of attribute state.



9
10
11
# File 'lib/manhunt/message.rb', line 9

def state
  @state
end

#subjectObject (readonly)

Returns the value of attribute subject.



9
10
11
# File 'lib/manhunt/message.rb', line 9

def subject
  @subject
end

Class Method Details

.compose(session, user_name, user_id, reply_message_id = nil) ⇒ Object



27
28
29
30
31
32
33
34
# File 'lib/manhunt/message.rb', line 27

def self.compose(session, user_name, user_id, reply_message_id = nil)
  if reply_message_id
    url = "/mail/compose/recipientId/#{user_id}/messageId/#{reply_message_id}/username/#{user_name}"
  end
  compose = session.get_document(url)
  
  { :subject => (compose/"#subject").first.attributes['value'], :body => (compose/"#body").first.inner_html, :raw => compose }
end

.parse_header(format, row) ⇒ Object



15
16
17
# File 'lib/manhunt/message.rb', line 15

def self.parse_header(format, row)
  ManhuntMessage.new(:header, row, format)
end

.parse_message(document, id) ⇒ Object



19
20
21
22
23
24
25
# File 'lib/manhunt/message.rb', line 19

def self.parse_message(document, id)
  message = ManhuntMessage.new(:message, document)
  message.instance_eval do
    @id = id
  end
  message
end

.send_message(session, params) ⇒ Object



60
61
62
63
64
65
66
67
68
# File 'lib/manhunt/message.rb', line 60

def self.send_message(session, params)
  url = "/mail/send"
  
  options = { 'body' => params[:body], 'subject' => params[:subject], 'recipientId' => params[:send_to_id], 'username' => params[:send_to] }
  options['replyMessageId'] = params[:reply_message_id] if params[:reply_message_id]
    

  session.post_nil(url, options)
end

Instance Method Details

#attributesObject



11
12
13
# File 'lib/manhunt/message.rb', line 11

def attributes
  { 'subject' => subject, 'from' => from, 'state' => state, 'id' => id, 'body' => body, 'date' => date, 'online' => online }
end

#to_paramObject



70
71
72
# File 'lib/manhunt/message.rb', line 70

def to_param
  id
end