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
-
.application_name ⇒ Object
Returns the value of attribute application_name.
-
.application_version ⇒ Object
Returns the value of attribute application_version.
-
.auth_method ⇒ Object
Returns the value of attribute auth_method.
-
.auth_scopes ⇒ Object
Returns the value of attribute auth_scopes.
-
.client ⇒ Object
readonly
Returns the value of attribute client.
-
.client_id ⇒ Object
Returns the value of attribute client_id.
-
.client_secret ⇒ Object
Returns the value of attribute client_secret.
-
.email_account ⇒ Object
Returns the value of attribute email_account.
-
.mailbox_email ⇒ Object
readonly
Returns the value of attribute mailbox_email.
-
.refresh_token ⇒ Object
Returns the value of attribute refresh_token.
-
.service ⇒ Object
readonly
Returns the value of attribute service.
Class Method Summary collapse
- .connect(client_id = @client_id, client_secret = @client_secret, refresh_token = @refresh_token) ⇒ Object
- .new(hash) ⇒ Object
- .parse(response) ⇒ Object
- .request(method, params = {}, body = {}, auth_method = @auth_method) ⇒ Object
- .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
Class Attribute Details
.application_name ⇒ Object
Returns the value of attribute application_name.
26 27 28 |
# File 'lib/gmail.rb', line 26 def application_name @application_name end |
.application_version ⇒ Object
Returns the value of attribute application_version.
26 27 28 |
# File 'lib/gmail.rb', line 26 def application_version @application_version end |
.auth_method ⇒ Object
Returns the value of attribute auth_method.
26 27 28 |
# File 'lib/gmail.rb', line 26 def auth_method @auth_method end |
.auth_scopes ⇒ Object
Returns the value of attribute auth_scopes.
26 27 28 |
# File 'lib/gmail.rb', line 26 def auth_scopes @auth_scopes end |
.client ⇒ Object (readonly)
Returns the value of attribute client.
28 29 30 |
# File 'lib/gmail.rb', line 28 def client @client end |
.client_id ⇒ Object
Returns the value of attribute client_id.
26 27 28 |
# File 'lib/gmail.rb', line 26 def client_id @client_id end |
.client_secret ⇒ Object
Returns the value of attribute client_secret.
26 27 28 |
# File 'lib/gmail.rb', line 26 def client_secret @client_secret end |
.email_account ⇒ Object
Returns the value of attribute email_account.
26 27 28 |
# File 'lib/gmail.rb', line 26 def email_account @email_account end |
.mailbox_email ⇒ Object (readonly)
Returns the value of attribute mailbox_email.
28 29 30 |
# File 'lib/gmail.rb', line 28 def mailbox_email @mailbox_email end |
.refresh_token ⇒ Object
Returns the value of attribute refresh_token.
26 27 28 |
# File 'lib/gmail.rb', line 26 def refresh_token @refresh_token end |
.service ⇒ Object (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..client_id = client_id @client..client_secret = client_secret @client..refresh_token = refresh_token @client..grant_type = 'refresh_token' @client..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.service_account_connect elsif self.client..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) ⇒ 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.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. = asserter.(email_account) end |