module Gmail

Constants

VERSION

Attributes

application_name[RW]
application_version[RW]
auth_method[RW]
auth_scopes[RW]
client[R]
client_id[RW]
client_secret[RW]
email_account[RW]
mailbox_email[R]
refresh_token[RW]
service[R]

Public Class Methods

connect(client_id=@client_id, client_secret=@client_secret, refresh_token=@refresh_token) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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.service_account_connect
      elsif self.client.authorization.principal != @email_account
        self.service_account_connect
      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 ) click to toggle source
# File lib/gmail.rb, line 114
def self.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
  )
  puts "Authenticating service account - #{email_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(email_account)
  
end