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.5"

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.application_nameObject

Returns the value of attribute application_name.



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

def application_name
  @application_name
end

.application_versionObject

Returns the value of attribute application_version.



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

def application_version
  @application_version
end

.clientObject

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.



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

def client_id
  @client_id
end

.client_secretObject

Returns the value of attribute client_secret.



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

def client_secret
  @client_secret
end

.refresh_tokenObject

Returns the value of attribute refresh_token.



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

def refresh_token
  @refresh_token
end

.serviceObject

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



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/gmail.rb', line 69

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
  [:client_id, :client_secret, :refresh_token, :application_name, :application_version].each do |accessor|
    Gmail.send("#{accessor}=", hash[accessor.to_s])
  end
end

.parse(response) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/gmail.rb', line 97

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 = {}) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/gmail.rb', line 46

def self.request(method, params={}, body={})
  params[:userId] ||= "me"
  if @client.nil?
    self.connect
  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