-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcalendar_with_transactions.rb
81 lines (69 loc) · 2.45 KB
/
calendar_with_transactions.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
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
79
80
81
require 'dotenv'
Dotenv.load
require 'mondo'
require 'google/api_client'
require 'google/api_client/client_secrets'
require 'google/api_client/auth/installed_app'
require 'google/api_client/auth/storage'
require 'google/api_client/auth/storages/file_store'
require 'fileutils'
require 'pry'
# Mondo Initialization
mondo = Mondo::Client.new(
token: ENV['MONDO_ACCOUNT_TOKEN']
)
mondo.api_url = "https://staging-api.gmon.io"
# Google Calendar Initialization
APPLICATION_NAME = 'mondo-google-calendar-client'
CLIENT_SECRETS_PATH = 'client_secret.json'
CREDENTIALS_PATH = File.join(Dir.home, '.credentials',
"calendar-ruby-quickstart.json")
SCOPE = 'https://www.googleapis.com/auth/calendar'
##
# Ensure valid credentials, either by restoring from the saved credentials
# files or intitiating an OAuth2 authorization request via InstalledAppFlow.
# If authorization is required, the user's default browser will be launched
# to approve the request.
#
# @return [Signet::OAuth2::Client] OAuth2 credentials
def authorize
FileUtils.mkdir_p(File.dirname(CREDENTIALS_PATH))
file_store = Google::APIClient::FileStore.new(CREDENTIALS_PATH)
storage = Google::APIClient::Storage.new(file_store)
auth = storage.authorize
if auth.nil? || (auth.expired? && auth.refresh_token.nil?)
app_info = Google::APIClient::ClientSecrets.load(CLIENT_SECRETS_PATH)
flow = Google::APIClient::InstalledAppFlow.new({
:client_id => app_info.client_id,
:client_secret => app_info.client_secret,
:scope => SCOPE})
auth = flow.authorize(storage)
puts "Credentials saved to #{CREDENTIALS_PATH}" unless auth.nil?
end
auth
end
# Initialize the API
client = Google::APIClient.new(:application_name => APPLICATION_NAME)
client.authorization = authorize
calendar_api = client.discovered_api('calendar', 'v3')
# Read transactions from the bank account and add to the calendar
mondo.transactions.each do |t|
event_description = "#{t.description} for #{t.amount} #{t.currency}"
event = {
'summary' => event_description,
'start' => {
'dateTime' => t.created.to_s
},
'end' => {
'dateTime' => (t.created + 0.01).to_s
}
}
results = client.execute!(
:api_method => calendar_api.events.insert,
:parameters => {
:calendarId => 'primary'},
:body_object => event)
event = results.data
puts "Event created: #{event.inspect} #{event.htmlLink}"
end
puts 'Import of transactions to calendar events finalised'