Skip to content
This repository was archived by the owner on Nov 23, 2021. It is now read-only.

Add outbox support to 'messages' #3

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 16 additions & 5 deletions lib/lonely_coder/mailbox.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
require 'date'

class OKCupid
def mailbox
@mailbox ||= Mailbox.new(@browser)

INBOX_URL = "/messages"
OUTBOX_URL = "/messages?folder=2"

def inbox
@inbox ||= Mailbox.new(INBOX_URL, @browser)
end

def outbox
@outbox ||= Mailbox.new(OUTBOX_URL, @browser)
end

def conversation_for(id)
Expand Down Expand Up @@ -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 }
Expand All @@ -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)
Expand Down
71 changes: 63 additions & 8 deletions spec/mailbox_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -15,18 +15,73 @@
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

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
Expand Down Expand Up @@ -64,8 +119,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

Expand Down