lonely_coder 0.1.2 → 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,124 @@
1
+ require 'date'
2
+
3
+ class OKCupid
4
+ def mailbox
5
+ @mailbox ||= Mailbox.new(@browser)
6
+ end
7
+
8
+ def conversation_for(id)
9
+ Mailbox::Conversation.by_id(id, @browser)
10
+ end
11
+
12
+ class Mailbox
13
+ class MessageSnippet
14
+
15
+ attr_accessor :profile_username, :profile_small_avatar_url, :preview, :last_date, :conversation_url
16
+
17
+ def self.from_html(html)
18
+ profile_username = html.search('a.subject').text
19
+ preview = html.search('.previewline').text
20
+ last_date = html.search('.timestamp').text
21
+ conversation_url = html.search('p:first').attribute('onclick').text.gsub('window.location=\'', '').gsub('\';','')
22
+ profile_small_avatar_url = html.search('a.photo img').attribute('src').text
23
+
24
+ self.new({
25
+ profile_username: profile_username,
26
+ preview: preview,
27
+ last_date: Date.parse(last_date),
28
+ conversation_url: conversation_url,
29
+ profile_small_avatar_url: profile_small_avatar_url
30
+ })
31
+ end
32
+
33
+ def initialize(attrs)
34
+ attrs.each do |attr, value|
35
+ self.send("#{attr}=", value)
36
+ end
37
+ end
38
+ end
39
+
40
+ class Conversation
41
+ attr_accessor :from_profile_username, :messages
42
+
43
+ def self.by_id(id, browser)
44
+ html = browser.get("/messages?readmsg=true&threadid=#{id}&folder=1")
45
+ from_profile_username = html.search('li.to_me:first a').attribute('title').text
46
+
47
+ messages = []
48
+
49
+ html.search('#thread > li').each do |message_html|
50
+ css_class = message_html.attribute('class')
51
+ css_id = message_html.attribute('id')
52
+
53
+ if (css_class && css_class.text.match(/_me/))
54
+ if(css_id && css_id.text == 'compose')
55
+ next
56
+ else
57
+ messages << Message.from_html(message_html)
58
+ end
59
+ else
60
+ next
61
+ end
62
+ end
63
+
64
+ self.new({
65
+ from_profile_username: from_profile_username,
66
+ messages: messages
67
+ })
68
+ end
69
+
70
+ def initialize(attrs)
71
+ attrs.each do |attr, value|
72
+ self.send("#{attr}=", value)
73
+ end
74
+ end
75
+ end
76
+
77
+ class Message
78
+ attr_accessor :to_me, :from_me, :body
79
+
80
+ def self.from_html(html)
81
+ to_me = !!html.attribute('class').text.match(/to_me/)
82
+ from_me = !to_me
83
+ # time = html.search('.timestamp').text
84
+ body = html.search('.message_body').text.gsub('<br>', "\n")
85
+
86
+ self.new({
87
+ to_me: to_me,
88
+ from_me: from_me,
89
+ # time: time,
90
+ body: body
91
+ })
92
+ end
93
+
94
+ def initialize(attrs)
95
+ attrs.each do |attr, value|
96
+ self.send("#{attr}=", value)
97
+ end
98
+ end
99
+ end
100
+
101
+ def initialize(browser)
102
+ @browser = browser
103
+ end
104
+
105
+ def useage
106
+ html = @browser.get('/messages')
107
+ current, max = html.search('p.fullness').text.match(/([\d]+) of ([\d]+)/).captures
108
+
109
+ return { current: current.to_i, max: max.to_i }
110
+ end
111
+
112
+ def messages
113
+ @messages = []
114
+
115
+ html = @browser.get('/messages')
116
+ messages_html = html.search('#messages li')
117
+ @messages += messages_html.collect do |message|
118
+ MessageSnippet.from_html(message)
119
+ end
120
+
121
+ @messages
122
+ end
123
+ end
124
+ end
data/lib/lonely_coder.rb CHANGED
@@ -11,7 +11,7 @@ require 'mechanize'
11
11
 
12
12
  class OKCupid
13
13
  BaseUrl = 'http://www.okcupid.com'
14
- VERSION = '0.1.2'
14
+ VERSION = '0.1.3'
15
15
 
16
16
  def initialize(username=nil, password=nil)
17
17
  @browser = Mechanize.new
@@ -35,4 +35,5 @@ require 'lonely_coder/magic_constants'
35
35
  require 'lonely_coder/profile'
36
36
  require 'lonely_coder/search'
37
37
  require 'lonely_coder/search_pagination_parser'
38
- require 'lonely_coder/authentication'
38
+ require 'lonely_coder/authentication'
39
+ require 'lonely_coder/mailbox'
data/lonely_coder.gemspec CHANGED
@@ -1,7 +1,7 @@
1
1
  # -*- encoding: utf-8 -*-
2
2
  Gem::Specification.new do |s|
3
3
  s.name = "lonely_coder"
4
- s.version = '0.1.2'
4
+ s.version = '0.1.3'
5
5
  s.platform = Gem::Platform::RUBY
6
6
  s.authors = ["Trek Glowacki"]
7
7
  s.email = ["trek.glowacki@gmail.com"]