Module: Gmail

Defined in:
lib/gmail.rb,
lib/gmail/util.rb,
lib/gmail/draft.rb,
lib/gmail/label.rb,
lib/gmail/thread.rb,
lib/gmail/message.rb,
lib/gmail/version.rb,
lib/gmail/base/get.rb,
lib/gmail/base/list.rb,
lib/gmail/base/trash.rb,
lib/gmail/base/create.rb,
lib/gmail/base/delete.rb,
lib/gmail/base/modify.rb,
lib/gmail/base/update.rb,
lib/gmail/api_resource.rb,
lib/gmail/gmail_object.rb

Defined Under Namespace

Modules: Base, Util Classes: APIResource, Draft, GmailObject, Label, Message, Thread

Constant Summary collapse

VERSION =
"0.0.14"

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.application_nameObject

Returns the value of attribute application_name.



26
27
28
# File 'lib/gmail.rb', line 26

def application_name
  @application_name
end

.application_versionObject

Returns the value of attribute application_version.



26
27
28
# File 'lib/gmail.rb', line 26

def application_version
  @application_version
end

.auth_methodObject

Returns the value of attribute auth_method.



26
27
28
# File 'lib/gmail.rb', line 26

def auth_method
  @auth_method
end

.auth_scopesObject

Returns the value of attribute auth_scopes.



26
27
28
# File 'lib/gmail.rb', line 26

def auth_scopes
  @auth_scopes
end

.clientObject (readonly)

Returns the value of attribute client.



28
29
30
# File 'lib/gmail.rb', line 28

def client
  @client
end

.client_idObject

Returns the value of attribute client_id.



26
27
28
# File 'lib/gmail.rb', line 26

def client_id
  @client_id
end

.client_secretObject

Returns the value of attribute client_secret.



26
27
28
# File 'lib/gmail.rb', line 26

def client_secret
  @client_secret
end

.email_accountObject

Returns the value of attribute email_account.



26
27
28
# File 'lib/gmail.rb', line 26

def 
  @email_account
end

.mailbox_emailObject (readonly)

Returns the value of attribute mailbox_email.



28
29
30
# File 'lib/gmail.rb', line 28

def mailbox_email
  @mailbox_email
end

.refresh_tokenObject

Returns the value of attribute refresh_token.



26
27
28
# File 'lib/gmail.rb', line 26

def refresh_token
  @refresh_token
end

.serviceObject (readonly)

Returns the value of attribute service.



28
29
30
# File 'lib/gmail.rb', line 28

def service
  @service
end

Class Method Details

.connect(client_id = @client_id, client_secret = @client_secret, refresh_token = @refresh_token) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/gmail.rb', line 86

def self.connect(client_id=@client_id, client_secret=@client_secret, refresh_token=@refresh_token)
  unless client_id
    raise 'No client_id specified'
  end

  unless client_secret
    raise 'No client_secret specified'
  end

  unless refresh_token
    raise 'No refresh_token specified'
  end

  @client = Google::APIClient.new(
      application_name: @application_name,
      application_version: @application_version
  )
  @client.authorization.client_id = client_id
  @client.authorization.client_secret = client_secret
  @client.authorization.refresh_token = refresh_token
  @client.authorization.grant_type = 'refresh_token'
  @client.authorization.fetch_access_token!
  @client.auto_refresh_token = true

  #@service = @client.discovered_api('gmail', 'v1')

end

.new(hash) ⇒ Object



29
30
31
32
33
# File 'lib/gmail.rb', line 29

def new hash
  [:auth_method, :client_id, :client_secret, :refresh_token, :auth_scopes, :email_account, :application_name, :application_version].each do |accessor|
    Gmail.send("#{accessor}=", hash[accessor.to_s])
  end
end

.parse(response) ⇒ Object



138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/gmail.rb', line 138

def self.parse(response)
  begin

    if response.body.empty?
      return response.body
    else
      response = JSON.parse(response.body)
    end

  rescue JSON::ParserError
    raise "error code: #{response.error},body: #{response.body})"
  end

  r = Gmail::Util.symbolize_names(response)
  if r[:error]
    raise "#{r[:error]}"
  end
  r
end

.request(method, params = {}, body = {}, auth_method = @auth_method) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/gmail.rb', line 46

def self.request(method, params={}, body={}, auth_method=@auth_method)
  
  params[:userId] ||= "me"
  case auth_method
    when "web_application" 
      if @client.nil?
        self.connect
      end
    when "service_account"
      if @client.nil?
        self.
      elsif self.client.authorization.principal != @email_account
        self.
      end
  end

  if body.empty?
    response = @client.execute(
        :api_method => method,
        :parameters => params,

        :headers => {'Content-Type' => 'application/json'})
  else

   response =  @client.execute(
        :api_method => method,
        :parameters => params,
        :body_object => body,
        :headers => {'Content-Type' => 'application/json'})
  end
  parse(response)

end

.service_account_connect(client_id = @client_id, client_secret = @client_secret, email_account = @email_account, auth_scopes = @auth_scopes, application_name = @application_name, application_version = @application_version) ⇒ Object



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/gmail.rb', line 114

def self.(
  client_id=@client_id, client_secret=@client_secret,
  =@email_account, auth_scopes=@auth_scopes, 
  application_name=@application_name, application_version=@application_version
  )
  puts "Authenticating service account - #{}"
  

  @client = Google::APIClient.new(application_name: application_name, application_version: application_version)
    
    
  
  key = Google::APIClient::KeyUtils.load_from_pem(
      client_secret,
      'notasecret')
  asserter = Google::APIClient::JWTAsserter.new(
      client_id,
      auth_scopes, 
      key
  )
  @client.authorization = asserter.authorize()
  
end