Class: GvoiceRuby::Client

Inherits:
Object
  • Object
show all
Includes:
Curl
Defined in:
lib/gvoice-ruby/client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config = GvoiceRuby::Configurator.load_config) ⇒ Client

Returns a new instance of Client.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/gvoice-ruby/client.rb', line 12

def initialize(config = GvoiceRuby::Configurator.load_config)
  if config[:google_account_email].nil? || config[:google_account_password].nil?
    raise ArgumentError, "Invalid Google Account username or password provided."
  else          
    @logger        = Logger.new(config.has_key?(:logfile) ? config[:logfile] :
                                  File.join(File.dirname(__FILE__), '..', '..', 'log', 'gvoice-ruby.log'))
    @user          = User.new(config[:google_account_email], config[:google_account_password])
    @any_unread    = []
    @unread_counts = {}
    @all_messages  = []
    initialize_curb
  end
  
  (config)
  set_rnr_se_token
  # in case _rnr_se scraping fails due to google changing its page content
  @_rnr_se ||= config[:rnr_se]
end

Instance Attribute Details

#all_messagesObject

Returns the value of attribute all_messages.



9
10
11
# File 'lib/gvoice-ruby/client.rb', line 9

def all_messages
  @all_messages
end

#callsObject

Returns the value of attribute calls.



9
10
11
# File 'lib/gvoice-ruby/client.rb', line 9

def calls
  @calls
end

#loggerObject (readonly)

Returns the value of attribute logger.



10
11
12
# File 'lib/gvoice-ruby/client.rb', line 10

def logger
  @logger
end

#smssObject

Returns the value of attribute smss.



9
10
11
# File 'lib/gvoice-ruby/client.rb', line 9

def smss
  @smss
end

#unread_countsObject

Returns the value of attribute unread_counts.



9
10
11
# File 'lib/gvoice-ruby/client.rb', line 9

def unread_counts
  @unread_counts
end

#userObject

Returns the value of attribute user.



9
10
11
# File 'lib/gvoice-ruby/client.rb', line 9

def user
  @user
end

#voicemailsObject

Returns the value of attribute voicemails.



9
10
11
# File 'lib/gvoice-ruby/client.rb', line 9

def voicemails
  @voicemails
end

Instance Method Details

#add_note(options) ⇒ Object



155
156
157
158
159
160
161
162
163
# File 'lib/gvoice-ruby/client.rb', line 155

def add_note(options)
  fields = [ PostField.content('id', options[:id]),
             PostField.content('note', options[:note]),
             PostField.content('_rnr_se', @_rnr_se) ]
             
  options.merge!({ :post_url => 'https://www.google.com/voice/inbox/savenote'})
  
  post(options, fields)
end

#any_unread?Boolean

Returns:

  • (Boolean)


31
32
33
# File 'lib/gvoice-ruby/client.rb', line 31

def any_unread?
  @any_unread
end

#archive(options) ⇒ Object



105
106
107
108
109
110
111
112
113
# File 'lib/gvoice-ruby/client.rb', line 105

def archive(options)
  fields = [ PostField.content('messages', options[:id]),
             PostField.content('archive', 1),
             PostField.content('_rnr_se', @_rnr_se) ]
             
  options.merge!({ :post_url => 'https://www.google.com/voice/inbox/mark'})
             
  post(options, fields)
end

#call(options) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/gvoice-ruby/client.rb', line 81

def call(options)
  fields = [ PostField.content('outgoingNumber', options[:outgoing_number]),
             PostField.content('forwardingNumber', options[:forwarding_number]),
             PostField.content('phoneType', options[:phone_type] || 2),
             PostField.content('subscriberNumber', 'undefined'),
             PostField.content('remember', 0),
             PostField.content('_rnr_se', @_rnr_se) ]
  
  options.merge!({ :post_url => "https://www.google.com/voice/call/connect" })
             
  post(options, fields)
end

#cancel_call(options = {}) ⇒ Object



94
95
96
97
98
99
100
101
102
103
# File 'lib/gvoice-ruby/client.rb', line 94

def cancel_call(options = {})
  fields = [ PostField.content('outgoingNumber', options[:outgoing_number] || 'undefined'),
             PostField.content('forwardingNumber', options[:forwarding_number] || 'undefined'),
             PostField.content('cancelType', 'C2C'),
             PostField.content('_rnr_se', @_rnr_se) ]
             
  options.merge!({ :post_url => "https://www.google.com/voice/call/cancel" })
  
  post(options, fields)
end

#check(parser = GvoiceRuby::InboxParser.new) ⇒ Object



39
40
41
42
43
44
45
46
47
# File 'lib/gvoice-ruby/client.rb', line 39

def check(parser = GvoiceRuby::InboxParser.new)
  inbox = parser.parse_page(fetch_page)
  
  get_unread_counts(inbox)
  @smss = parser.parse_sms_messages(inbox['messages'])
  @voicemails = parser.parse_voicemail_messages(inbox['messages'])
  @all_messages = smss | voicemails
  @all_messages.sort_by!(&:start_time)
end

#delete_note(options) ⇒ Object



165
166
167
168
169
170
171
172
# File 'lib/gvoice-ruby/client.rb', line 165

def delete_note(options)
  fields = [ PostField.content('id', options[:id]),
             PostField.content('_rnr_se', @_rnr_se) ]
             
  options.merge!({ :post_url => 'https://www.google.com/voice/inbox/deletenote'})
  
  post(options, fields)
end

#logged_in?Boolean

Returns:

  • (Boolean)


35
36
37
# File 'lib/gvoice-ruby/client.rb', line 35

def logged_in?
  !@curb_instance.nil?
end

#logoutObject



174
175
176
177
178
179
180
181
182
# File 'lib/gvoice-ruby/client.rb', line 174

def logout
  if logged_in?
    @curb_instance.url = "https://www.google.com/voice/account/signout"
    @curb_instance.perform
    logger.info "FINISHED LOGOUT #{@curb_instance.url}: HTTP #{@curb_instance.response_code}"
    @curb_instance = nil
  end
  self
end

#mark_as_read(options) ⇒ Object



115
116
117
118
119
120
121
122
123
# File 'lib/gvoice-ruby/client.rb', line 115

def mark_as_read(options)
  fields = [ PostField.content('messages', options[:id]),
             PostField.content('read', 1),
             PostField.content('_rnr_se', @_rnr_se) ]
             
  options.merge!({ :post_url => 'https://www.google.com/voice/inbox/mark'})
             
  post(options, fields)
end

#mark_as_unread(options) ⇒ Object



125
126
127
128
129
130
131
132
133
# File 'lib/gvoice-ruby/client.rb', line 125

def mark_as_unread(options)
  fields = [ PostField.content('messages', options[:id]),
             PostField.content('read', 0),
             PostField.content('_rnr_se', @_rnr_se) ]
             
  options.merge!({ :post_url => 'https://www.google.com/voice/inbox/mark'})
             
  post(options, fields)
end

#missed(parser = GvoiceRuby::InboxParser.new) ⇒ Object



49
50
51
52
# File 'lib/gvoice-ruby/client.rb', line 49

def missed(parser = GvoiceRuby::InboxParser.new)
  inbox = parser.parse_page(fetch_page('https://www.google.com/voice/inbox/recent/missed/'))
  parser.parse_calls(inbox['messages'])
end

#placed(parser = GvoiceRuby::InboxParser.new) ⇒ Object



59
60
61
62
# File 'lib/gvoice-ruby/client.rb', line 59

def placed(parser = GvoiceRuby::InboxParser.new)
  inbox = parser.parse_page(fetch_page('https://www.google.com/voice/inbox/recent/placed/'))
  parser.parse_calls(inbox['messages'])
end

#received(parser = GvoiceRuby::InboxParser.new) ⇒ Object



54
55
56
57
# File 'lib/gvoice-ruby/client.rb', line 54

def received(parser = GvoiceRuby::InboxParser.new)
  inbox = parser.parse_page(fetch_page('https://www.google.com/voice/inbox/recent/received/'))
  parser.parse_calls(inbox['messages'])
end

#send_sms(options, show_details = true) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/gvoice-ruby/client.rb', line 64

def send_sms(options, show_details = true)
  fields = [ PostField.content('phoneNumber', options[:phone_number]),
             PostField.content('text', options[:text]),
             PostField.content('_rnr_se', @_rnr_se) ]

  options.merge!({ :post_url => "https://www.google.com/voice/sms/send" })

  if show_details
    logger.info "Sending sms..."
    logger.info "  To: #{options[:phone_number]}"
    logger.info "  Body: #{options[:text]}"
    logger.info "  At: #{Time.now}"
  end

  post(options, fields)
end

#star(options) ⇒ Object



135
136
137
138
139
140
141
142
143
# File 'lib/gvoice-ruby/client.rb', line 135

def star(options)
  fields = [ PostField.content('messages', options[:id]),
             PostField.content('star', 1),
             PostField.content('_rnr_se', @_rnr_se) ]
             
  options.merge!({ :post_url => 'https://www.google.com/voice/inbox/star'})
             
  post(options, fields)
end

#unstar(options) ⇒ Object



145
146
147
148
149
150
151
152
153
# File 'lib/gvoice-ruby/client.rb', line 145

def unstar(options)
  fields = [ PostField.content('messages', options[:id]),
             PostField.content('star', 0),
             PostField.content('_rnr_se', @_rnr_se) ]
             
  options.merge!({ :post_url => 'https://www.google.com/voice/inbox/star'})
             
  post(options, fields)
end