Commit 8f656e4c by Andrew Dahl

Added question pagination; filters not yet working

parent 1b62cc16
......@@ -35,3 +35,4 @@ require 'lonely_coder/profile'
require 'lonely_coder/search'
require 'lonely_coder/authentication'
require 'lonely_coder/mailbox'
require 'lonely_coder/question'
require 'uri'
require 'set'
require 'lonely_coder/question/question_pagination_parser'
require 'lonely_coder/question/options/filter'
require 'lonely_coder/question/options/paginator'
class OKCupid
def questions_for(username, options={})
Question.new(username, options, @browser)
end
class Question
attr_accessor :username, :filters
def initialize(username, options, browser = Mechanize.new)
@browser = browser
@username = username
options = defaults.merge(options)
parse(options)
end
def parse(options)
@parameters = []
@filters = []
options.each do |name, value|
self.send("add_#{name}_option", value)
end
end
def defaults
{
:pagination => {
:page => 1
}
}
end
def results
return @results if @results
page = @browser.get(url)
@results = Set.new
@results += page.search("//div[@class='questions']//div[starts-with(@id,'question_')]").collect do |node|
parse_question(node)
end
@results
end
def load_next_page
@pagination.next
previous_length = @results.size
page = @browser.get(url)
@results += page.search("//div[@class='questions']//div[starts-with(@id,'question_')]").collect do |node|
parse_question(node)
end
@browser.pluggable_parser.html = Mechanize::Page
previous_length != @results.size
end
def parse_question(html)
question = html.search('p.qtext').text
answer = html.css("p[7]").text
{question: question, answer: answer}
end
def url
"/profile/#{username}/questions?#{filters_as_query}&#{parameters_as_query}"
end
def parameters_as_query
@parameters.collect {|param| param.to_param }.join('&')
end
def filters_as_query
filters.compact.to_enum(:each_with_index).map {|filter,index| filter.to_param(index+1)}.join('&')
end
end
end
class OKCupid
class QuestionFilter
class NoSuchFilter < StandardError; end
class BadValue < StandardError; end
attr_reader :name, :value
def initialize(name,value)
check = MagicNumbers::QuestionFilters[name.to_s]
raise(NoSuchFilter, name) unless check
@name = name.to_s
@value = value
if @value != 0 and @value != 1
raise(VadValue, "#{@value.inspect} is not a possible value for #{name}. Accepted values include 0 and 1.")
end
end
def to_param
"#{name}=#{value}"
end
end
class Question
def add_they_care_about_option(value)
@filters << Filter.new('she_care', value)
end
def add_you_care_about_option(value)
@filters << Filter.new('i_care', value)
end
def add_you_have_not_answered_option(value)
@filters << Filter.new('unanswered', value)
end
def add_unacceptable_answers_option(value)
@filters << Filter.new('disagree', value)
end
def add_about_ethics_option(value)
@filters << Filter.new('Ethics', value)
end
def add_about_sex_option(value)
@filters << Filter.new('Sex', value)
end
def add_about_religion_option(value)
@filters << Filter.new('Religion', value)
end
def add_about_lifestyles_option(value)
@filters << Filter.new('Lifestsyle', value)
end
def add_about_dating_option(value)
@filters << Filter.new('Dating', value)
end
def add_about_other_topics_option(value)
@filters << Filter.new('Other', value)
end
def add_with_explanations_option(value)
@filters << Filter.new('notes', value)
end
end
end
class OKCupid
# used to create the pagination part of a profile questions url
# &low=1 where low is the start value
# each page has 10 questions, so low should always end in 1
class QuestionPaginator
attr_reader :page
def initialize(options)
@page = options[:page]
end
def low
@low = ((@page - 1) * 10) + 1
end
def next
@page += 1
self
end
def to_param
"low=#{low}"
end
end
class Question
def add_pagination_option(value)
@parameters << @pagination = QuestionPaginator.new(value)
end
end
end
......@@ -5,7 +5,7 @@ Gem::Specification.new do |s|
s.platform = Gem::Platform::RUBY
s.authors = ["Trek Glowacki"]
s.email = ["trek.glowacki@gmail.com"]
s.homepage = "http://github.com/trek/lonely_coder"
s.homepage = "http://projects.droo-labs.com/DrewDahl/lonely-coder.git"
s.summary = %q{A gem for interacting with OKCupid as if it had an API}
s.description = %q{A gem for interacting with OKCupid as if it had an API.}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment