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
30
# 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

    # in case _rnr_se scraping fails due to google changing its page content
    @_rnr_se ||= config[:rnr_se]
  end
  
  (config)
  set_rnr_se_token
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



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

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)


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

def any_unread?
  @any_unread
end

#archive(options) ⇒ Object



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

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



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

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



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

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



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

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



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

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)


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

def logged_in?
  !@curb_instance.nil?
end

#logoutObject



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

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



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

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



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

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



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

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



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

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



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

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



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

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



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

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



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

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