lonely_coder 0.1.3 → 0.1.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/CHANGELOG ADDED
@@ -0,0 +1,3 @@
1
+ 0.1.4 (March 27, 2012)
2
+ * more consistent search results when paginating by using OKCupid server timestamps
3
+ * Search#load_next_page returns boolean to deal with cases where pagination doesn't load new results
@@ -95,5 +95,17 @@ class OKCupid
95
95
  def ==(other)
96
96
  self.username == other.username
97
97
  end
98
+
99
+ def eql?(other)
100
+ self.username == other.username
101
+ end
102
+
103
+ def hash
104
+ if self.username
105
+ self.username.hash
106
+ else
107
+ super
108
+ end
109
+ end
98
110
  end
99
111
  end
@@ -32,12 +32,12 @@ class OKCupid
32
32
 
33
33
  options.each do |name,value|
34
34
  self.send("add_#{name}_option", value)
35
- # if OKCupid.const_defined?("#{name.to_s.camelize}Filter")
36
- # @filters << OKCupid.const_get("#{name.to_s.camelize}Filter").new(name, value)
37
- # else
38
- # @filters << Filter.new(name, value)
39
- # end
40
35
  end
36
+
37
+ # OKC needs an initial time key of 1 to represent "waaaay in the past"
38
+ # futures searches will use the OKC server value returned from the first
39
+ # results set
40
+ @timekey = 1
41
41
  end
42
42
 
43
43
  def add_order_by_option(value)
@@ -121,15 +121,18 @@ class OKCupid
121
121
  def results
122
122
  return @results if @results
123
123
 
124
- @browser.pluggable_parser.html = SearchPaginationParser
125
- page = @browser.get(ajax_url)
124
+ # the first results request has to receive a full HTML page.
125
+ # subseqent calls can make json requests
126
+ page = @browser.get(url)
127
+ @timekey = page.search('script')[0].text.match(/CurrentGMT = new Date\(([\d]+)\*[\d]+\)/).captures[0]
126
128
 
127
- @results = page.search('.match_row').collect do |node|
129
+ # OKCupid will return previous profiles if there aren't enough
130
+ # profiles to fill a query, so we stop that with a set.
131
+ @results = Set.new
132
+ @results += page.search('.match_row').collect do |node|
128
133
  OKCupid::Profile.from_search_result(node)
129
134
  end
130
-
131
- @browser.pluggable_parser.html = Mechanize::Page
132
-
135
+
133
136
  @results
134
137
  end
135
138
 
@@ -142,16 +145,29 @@ class OKCupid
142
145
  # mygender=m
143
146
  # no idea what the following parameters do, but without them, the search
144
147
  # behaves erratically
145
- # &timekey=1
146
148
  # &custom_search=0
149
+ #
150
+ # OKCupid timestamps searches for pagniation. The first search gets a timestamp
151
+ # of 1 (e.g. 1 second into the epoch) and future searches are stamped with
152
+ # some server cache value. If that server value isn't submitted, the results
153
+ # for pagniation don't quite match what you'd expect: you'll get duplicates,
154
+ # or lower numbers than expected.
155
+ # &timekey=1
156
+
147
157
  def magic_params_not_truly_understood
148
- "timekey=1&custom_search=0"
158
+ "timekey=#{@timekey}&custom_search=0"
149
159
  end
150
160
 
161
+ # returns true if it worked, false if not.
162
+ # OKCupid searches will return duplicate results if you attempt to
163
+ # page beyond the availble results.
151
164
  def load_next_page
152
165
  @browser.pluggable_parser.html = SearchPaginationParser
153
166
 
154
- page = @browser.get("#{ajax_url}&#{@pagination.next.to_param}")
167
+ @pagination.next
168
+ previous_length = @results.size
169
+
170
+ page = @browser.get(ajax_url)
155
171
 
156
172
  @results += page.search('.match_row').collect do |node|
157
173
  OKCupid::Profile.from_search_result(node)
@@ -159,7 +175,7 @@ class OKCupid
159
175
 
160
176
  @browser.pluggable_parser.html = Mechanize::Page
161
177
 
162
- self
178
+ previous_length != @results.size
163
179
  end
164
180
 
165
181
  def url
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.3'
14
+ VERSION = '0.1.4'
15
15
 
16
16
  def initialize(username=nil, password=nil)
17
17
  @browser = Mechanize.new
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.3'
4
+ s.version = '0.1.4'
5
5
  s.platform = Gem::Platform::RUBY
6
6
  s.authors = ["Trek Glowacki"]
7
7
  s.email = ["trek.glowacki@gmail.com"]