Class: GoogleVoice::Api

Inherits:
Object
  • Object
show all
Defined in:
lib/googlevoice47.rb

Defined Under Namespace

Classes: InvalidLoginException, InvalidPhoneNumberException

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(email, password) ⇒ Api

Returns a new instance of Api.



21
22
23
24
25
26
27
28
# File 'lib/googlevoice47.rb', line 21

def initialize(email, password)
  @agent = Mechanize.new
  @coder = HTMLEntities.new
  @email = email
  @password = password

  init_xml_methods()
end

Instance Attribute Details

#agentObject

Returns the value of attribute agent.



16
17
18
# File 'lib/googlevoice47.rb', line 16

def agent
  @agent
end

#coderObject

Returns the value of attribute coder.



16
17
18
# File 'lib/googlevoice47.rb', line 16

def coder
  @coder
end

#emailObject

Returns the value of attribute email.



16
17
18
# File 'lib/googlevoice47.rb', line 16

def email
  @email
end

#passwordObject

Returns the value of attribute password.



16
17
18
# File 'lib/googlevoice47.rb', line 16

def password
  @password
end

#rnr_seObject

Returns the value of attribute rnr_se.



16
17
18
# File 'lib/googlevoice47.rb', line 16

def rnr_se
  @rnr_se
end

Instance Method Details

#call(remote_number, forwarding_number) ⇒ Object



52
53
54
55
56
57
# File 'lib/googlevoice47.rb', line 52

def call(remote_number, forwarding_number)
  #login unless logged_in?
  remote_number = validate_number(remote_number)
  forwarding_number = validate_number(forwarding_number)
  @agent.post('https://www.google.com/voice/call/connect/', :outgoingNumber => remote_number, :forwardingNumber => forwarding_number, :phoneType => 2, :subscriberNumber => 'undefined', :remember => '0', "_rnr_se" => @rnr_se)
end

#cancelObject



59
60
61
62
# File 'lib/googlevoice47.rb', line 59

def cancel()
  #login unless logged_in?
  @agent.post('https://www.google.com/voice/call/cancel/', :outgoingNumber => 'undefined', :forwardingNumber => 'undefined', :cancelType => 'C2C', "_rnr_se" => @rnr_se)      
end

#loginObject



30
31
32
33
34
35
36
37
38
39
# File 'lib/googlevoice47.rb', line 30

def ()
   = @agent.get("https://www.google.com/accounts/ServiceLogin?service=grandcentral")
  .forms.first.field_with(:name=>"Email").value = @email
  .forms.first.field_with(:name=>"Passwd").value = @password
  agent.submit(.forms.first)
  page = @agent.get('https://www.google.com/voice/')
  dialing_form = page.forms.find { |f| f.has_field?('_rnr_se') }
  raise InvalidLoginException, "Cannot login to Google Voice with #{@email}" unless dialing_form
  @rnr_se = dialing_form.field_with(:name => '_rnr_se').value
end

#sms(remote_number, text_message) ⇒ Object

def logged_in?

(@agent !=nil) && (@rnr_se != nil)

end



45
46
47
48
49
50
# File 'lib/googlevoice47.rb', line 45

def sms(remote_number, text_message)
  #login unless logged_in?
  remote_number = validate_number(remote_number)
  text_message = @coder.encode(text_message)
  @agent.post('https://www.google.com/voice/sms/send/', :id => '', :phoneNumber => remote_number, :text => text_message, "_rnr_se" => @rnr_se)
end

#validate_number(phone_number) ⇒ Object



64
65
66
67
68
# File 'lib/googlevoice47.rb', line 64

def validate_number(phone_number)
  phone_number = phone_number.gsub(/\D/,'')
  raise InvalidPhoneNumberException, "Invalid Phone Number #{phone_number}" if phone_number.length < 4 || phone_number.length > 15
  return phone_number
end