From 3fe9357240b3ceff81e830c65b6a732d5ad46070 Mon Sep 17 00:00:00 2001 From: Max Hawkins Date: Sat, 7 Jul 2012 16:45:19 -0700 Subject: [PATCH 1/2] Make 'mailbox' 'inbox' It's a more accurate name and makes outbox support more natural. --- lib/lonely_coder/mailbox.rb | 4 ++-- spec/mailbox_spec.rb | 16 ++++++++-------- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/lib/lonely_coder/mailbox.rb b/lib/lonely_coder/mailbox.rb index 5935c50..0c00886 100644 --- a/lib/lonely_coder/mailbox.rb +++ b/lib/lonely_coder/mailbox.rb @@ -1,8 +1,8 @@ require 'date' class OKCupid - def mailbox - @mailbox ||= Mailbox.new(@browser) + def inbox + @inbox ||= Mailbox.new(@browser) end def conversation_for(id) diff --git a/spec/mailbox_spec.rb b/spec/mailbox_spec.rb index 06b3081..e469878 100644 --- a/spec/mailbox_spec.rb +++ b/spec/mailbox_spec.rb @@ -4,8 +4,8 @@ it "tells you how full your mailbox is" do VCR.use_cassette('loading_mailbox', :erb => {username: ENV['OKC_USERNAME'], password: ENV['OKC_PASSWORD']}) do okc = OKCupid.new(ENV['OKC_USERNAME'], ENV['OKC_PASSWORD']) - @mailbox = okc.mailbox - @mailbox.useage.should == { + @inbox = okc.inbox + @inbox.useage.should == { current: 233, max: 300 } @@ -15,16 +15,16 @@ it "can access the first message, up to 30" do VCR.use_cassette('loading_mailbox', :erb => {username: ENV['OKC_USERNAME'], password: ENV['OKC_PASSWORD']}) do okc = OKCupid.new(ENV['OKC_USERNAME'], ENV['OKC_PASSWORD']) - @mailbox = okc.mailbox - @mailbox.messages.count.should == 30 + @inbox = okc.inbox + @inbox.messages.count.should == 30 end end it "each message header is a header" do VCR.use_cassette('loading_mailbox', :erb => {username: ENV['OKC_USERNAME'], password: ENV['OKC_PASSWORD']}) do okc = OKCupid.new(ENV['OKC_USERNAME'], ENV['OKC_PASSWORD']) - @mailbox = okc.mailbox - @mailbox.messages.all? {|m| m.is_a?(OKCupid::Mailbox::MessageSnippet)}.should == true + @inbox = okc.inbox + @inbox.messages.all? {|m| m.is_a?(OKCupid::Mailbox::MessageSnippet)}.should == true end end end @@ -64,8 +64,8 @@ before(:each) do VCR.use_cassette('loading_mailbox', :erb => {username: ENV['OKC_USERNAME'], password: ENV['OKC_PASSWORD']}) do okc = OKCupid.new(ENV['OKC_USERNAME'], ENV['OKC_PASSWORD']) - mailbox = okc.mailbox - @header = mailbox.messages.first + inbox = okc.inbox + @header = inbox.messages.first end end From cd453eb4618977d5c7c3021a35c311d355e83dfb Mon Sep 17 00:00:00 2001 From: Max Hawkins Date: Sat, 7 Jul 2012 17:20:30 -0700 Subject: [PATCH 2/2] Add 'outbox' object Allows the user to browse their outbox as well as their inbox. Added some tests. Some are marked pending until the VCR session is re-recorded. --- lib/lonely_coder/mailbox.rb | 19 ++++++++++--- spec/mailbox_spec.rb | 55 +++++++++++++++++++++++++++++++++++++ 2 files changed, 70 insertions(+), 4 deletions(-) diff --git a/lib/lonely_coder/mailbox.rb b/lib/lonely_coder/mailbox.rb index 0c00886..a568189 100644 --- a/lib/lonely_coder/mailbox.rb +++ b/lib/lonely_coder/mailbox.rb @@ -1,8 +1,16 @@ require 'date' class OKCupid + + INBOX_URL = "/messages" + OUTBOX_URL = "/messages?folder=2" + def inbox - @inbox ||= Mailbox.new(@browser) + @inbox ||= Mailbox.new(INBOX_URL, @browser) + end + + def outbox + @outbox ||= Mailbox.new(OUTBOX_URL, @browser) end def conversation_for(id) @@ -99,12 +107,15 @@ def initialize(attrs) end end - def initialize(browser) + attr_reader :url + + def initialize(url, browser) @browser = browser + @url = url end def useage - html = @browser.get('/messages') + html = @browser.get(@url) current, max = html.search('p.fullness').text.match(/([\d]+) of ([\d]+)/).captures return { current: current.to_i, max: max.to_i } @@ -113,7 +124,7 @@ def useage def messages @messages = [] - html = @browser.get('/messages') + html = @browser.get(@url) messages_html = html.search('#messages li') @messages += messages_html.collect do |message| MessageSnippet.from_html(message) diff --git a/spec/mailbox_spec.rb b/spec/mailbox_spec.rb index e469878..94ab985 100644 --- a/spec/mailbox_spec.rb +++ b/spec/mailbox_spec.rb @@ -27,6 +27,61 @@ @inbox.messages.all? {|m| m.is_a?(OKCupid::Mailbox::MessageSnippet)}.should == true end end + + describe "inbox object" do + + before(:each) do + VCR.use_cassette('loading_mailbox', :erb => {username: ENV['OKC_USERNAME'], password: ENV['OKC_PASSWORD']}) do + @okc = OKCupid.new(ENV['OKC_USERNAME'], ENV['OKC_PASSWORD']) + @inbox = @okc.inbox + end + end + + it "uses the inbox url" do + @inbox.url.should == OKCupid::INBOX_URL + end + + it "is cached" do + new_inbox = @okc.inbox + @inbox.object_id.should == new_inbox.object_id + end + + it "includes unsolicited messages" do + pending "cassettes are re-recorded" + VCR.use_cassette('loading_mailbox', :erb => {username: ENV['OKC_USERNAME'], password: ENV['OKC_PASSWORD']}) do + @outbox.messages.map(&:profile_username).should include("incoming_username") + end + end + + end + + describe "outbox object" do + + before(:each) do + VCR.use_cassette('loading_mailbox', :erb => {username: ENV['OKC_USERNAME'], password: ENV['OKC_PASSWORD']}) do + @okc = OKCupid.new(ENV['OKC_USERNAME'], ENV['OKC_PASSWORD']) + @outbox = @okc.outbox + end + end + + it "uses the outbox url" do + @outbox.url.should == OKCupid::OUTBOX_URL + end + + it "is cached" do + new_outbox = @okc.outbox + @outbox.object_id.should == new_outbox.object_id + end + + it "includes messages with no response" do + pending "cassettes are re-recorded" + VCR.use_cassette('loading_mailbox', :erb => {username: ENV['OKC_USERNAME'], password: ENV['OKC_PASSWORD']}) do + @outbox.messages.map(&:profile_username).should include("outgoing_username") + end + end + + end + end describe "Conversation" do