Skip to content

Commit

Permalink
Initial SMS recent parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeff Rafter committed Mar 28, 2010
1 parent b0a2ecd commit 5c7877e
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 2 deletions.
12 changes: 12 additions & 0 deletions lib/google/voice/base.rb
Expand Up @@ -28,6 +28,18 @@ def delete(ids)
@curb.perform
@curb.response_code
end

def archive(ids)
ids = Array(ids)
fields = [
Curl::PostField.content('_rnr_se', @_rnr_se),
Curl::PostField.content('archive', '1')]
ids.each{|id| fields << Curl::PostField.content('messages', id)}
@curb.http_post(fields)
@curb.url = "https://www.google.com/voice/inbox/archiveMessages/"
@curb.perform
@curb.response_code
end

def mark(ids, read = true)
ids = Array(ids)
Expand Down
44 changes: 42 additions & 2 deletions lib/google/voice/sms.rb
@@ -1,9 +1,11 @@
# coding: UTF-8
require File.join(File.expand_path(File.dirname(__FILE__)), 'base')

GOOGLE_VOICE_SMS_TYPE = 11

module Google
module Voice
class Sms < Base
module Voice
class Sms < Base
def sms(number, text)
@curb.http_post([
Curl::PostField.content('phoneNumber', number),
Expand All @@ -14,6 +16,44 @@ def sms(number, text)
@curb.perform
@curb.response_code
end

def recent
@curb.url = "https://www.google.com/voice/inbox/recent/"
@curb.http_get
sms = []
doc = Nokogiri::XML::Document.parse(@curb.body_str)
data = doc.xpath('/response/json').first.text
html = Nokogiri::HTML::DocumentFragment.parse(doc.to_html)
json = JSON.parse(data)
# Format for messages is [id, {attributes}]
json['messages'].each do |message|
if message[1]['type'].to_i != GOOGLE_VOICE_SMS_TYPE
next
else
messages = []
html.css('div.gc-message-sms-row').each do |row|
if row.css('span.gc-message-sms-from').inner_html.strip! =~ /Me:/
next
elsif row.css('span.gc-message-sms-time').inner_html =~ Regexp.new(txt_obj.display_start_time)
messages << {
:to => 'Me',
:from => row.css('span.gc-message-sms-from').inner_html.strip!.gsub!(':', ''),
:text => row.css('span.gc-message-sms-text').inner_html,
:time => row.css('span.gc-message-sms-time').inner_html
}
end
end
# Google is using milliseconds since epoch for time
sms << {
:id => message[0],
:phone_number => message[1]['phoneNumber'],
:start_time => Time.at(message[1]['startTime'].to_i / 1000),
:messages => messages}
end
end
sms
end

end
end
end

0 comments on commit 5c7877e

Please sign in to comment.