Skip to content

Instantly share code, notes, and snippets.

@philnash
Last active June 28, 2017 04:17
Show Gist options
  • Save philnash/35bd1dfa4cebb9328888 to your computer and use it in GitHub Desktop.
Save philnash/35bd1dfa4cebb9328888 to your computer and use it in GitHub Desktop.
Potential simple Gmail API class
class Gmail
# Don't generate the token in the initializer, pass it in.
# No need for attr_accessor on the client or service either, just save them as
# instance variables.
def initialize(token)
@client = Google::APIClient.new
@client.authorization.access_token = token
@service = @client.discovered_api('gmail')
end
DEFAULT_OPTIONS = {
:parameters => {
'userId' => 'me'
},
:headers => { 'Content-Type' => 'application/json' }
}
def labels
opts = DEFAULT_OPTIONS.merge(:api_method => service.users.labels.list)
execute(opts)
end
def messages_for_label(label_id)
opts = DEFAULT_OPTIONS.merge(:api_method => service.users.messages.list)
opts[:parameters]['labelIds'] = ['INBOX', label_id]
execute(opts)
end
def message_details(message_id)
opts = DEFAULT_OPTIONS.merge(:api_method => service.users.messages.get)
opts[:parameters]['id'] = message_id
execute(opts)
end
def remove_label(message_id, label_id)
opts = DEFAULT_OPTIONS.merge(:api_method => service.users.messages.modify)
opts[:parameters]['id'] = message_id
opts[:body_object]['removeLabelIds'] = [label_id]
execute(opts)
end
private
def execute
JSON.parse(@client.execute(opts))
end
end
@benkoshy
Copy link

Hi there, thanks for your code. where would you put this class?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment