bps-google-api 0.3.2 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 224c2c0919028cc227a062646c33d6b4d5b857ac
4
- data.tar.gz: dd2263743ddf4b0b24683028463f1bacf5a6c38b
3
+ metadata.gz: 5e879f7388cbb93030b4c0ebb10ba8a7f0ebbce2
4
+ data.tar.gz: 2db793f99ffe1de7c5170b95e3c7d14278f30681
5
5
  SHA512:
6
- metadata.gz: 133faee9d4e46358eafcd8f085eb99cc49dc24eafc1e46de3f5f2602c4ecc5893b82a768be57feb2200d40ab1c3395e232aa7d1c0783547794a32bb2ef833954
7
- data.tar.gz: 34d2595ddf6987a230d1f05fbbff3932cdb1307b4f7e39758e9a7488b81b61144451811f582f7cfabfe167041a4e4b78b771b9b9236f02d48ea9eaf423f114b4
6
+ metadata.gz: 15b9400b2a26b43ea093798a9129115d61d3106b21f4d491075aaf191f2f86a729ff024fd39b7ba8ac341e9bf1bc85d664a9bc613c1f89279583e186501b005b
7
+ data.tar.gz: 22b09c587f8b7f831d31e586e1118660b1a7d1dca4d1ba88240209a6b38b413f4b9913d2eedb4aedd6a127f8a35973af71207ca16c5c6098cb484b8f7ac34b15
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- bps-google-api (0.3.2)
4
+ bps-google-api (0.4.0)
5
5
  exp_retry (~> 0.0.11)
6
6
  fileutils (~> 1.2)
7
7
  google-api-client (~> 0.23.4)
data/Readme.md CHANGED
@@ -1,5 +1,8 @@
1
1
  # BPS Google API
2
2
 
3
+ [![Gem Version](https://img.shields.io/gem/v/bps-google-api.svg)](https://rubygems.org/gems/bps-google-api)
4
+ [![Build Status](https://travis-ci.org/jfiander/bps-google-api.svg)](https://travis-ci.org/jfiander/bps-google-api)
5
+
3
6
  A configured Google API wrapper.
4
7
 
5
8
  ## Installation
@@ -16,7 +19,7 @@ or install directly:
16
19
  gem install bps-google-api
17
20
  ```
18
21
 
19
- Create an initializer to configure the root directory for the gem:
22
+ Configure the root directories for the gem:
20
23
 
21
24
  ```ruby
22
25
  GoogleAPI.configure do |config|
@@ -24,6 +27,8 @@ GoogleAPI.configure do |config|
24
27
  end
25
28
  ```
26
29
 
30
+ For Rails, create an initializer:
31
+
27
32
  ```ruby
28
33
  GoogleAPI.configure do |config|
29
34
  config.root = Rails.root.join('config', 'google_api')
@@ -31,7 +36,7 @@ GoogleAPI.configure do |config|
31
36
  end
32
37
  ```
33
38
 
34
- Then run the following in `config/application.rb`:
39
+ Then add the following in `config/application.rb`:
35
40
 
36
41
  ```ruby
37
42
  require 'google_api'
@@ -54,27 +59,48 @@ TZ # Timezone
54
59
 
55
60
  ## Usage
56
61
 
57
- If no configuration is available, `.new` will automatically run `.authorize!`
58
- and return a URL to generate an authorization token.
62
+ By default, if no configuration is available, `.new` will automatically run
63
+ `.authorize!` and return a URL to generate an authorization token.
59
64
 
60
65
  ```ruby
61
- calendar = GoogleAPI::Calendar.new
66
+ calendar = GoogleAPI::Configured::Calendar.new(calendar_id)
62
67
 
63
- calendar.create(cal_id, event_options)
64
- calendar.list(cal_id, max_results: 2500, page_token: nil)
65
- calendar.get(cal_id, event_id)
66
- calendar.update(cal_id, event_id, event_options)
67
- calendar.delete(cal_id, event_id)
68
+ calendar.create(event_options)
69
+ calendar.list(max_results: 2500, page_token: nil)
70
+ calendar.get(event_id)
71
+ calendar.update(event_id, event_options)
72
+ calendar.delete(event_id)
68
73
 
69
- calendar.permit(calendar, user)
70
- calendar.unpermit(calendar, user)
74
+ calendar.permit(user)
75
+ calendar.unpermit(user)
71
76
  ```
72
77
 
73
78
  ```ruby
74
- group = GoogleAPI::Group.new('group@example.com')
79
+ group = GoogleAPI::Configured::Group.new('group@example.com')
75
80
 
76
81
  group.get
77
82
  group.members
78
83
  group.add('somebody@example.com')
79
84
  group.remove('somebody@example.com')
80
85
  ```
86
+
87
+ ### Event Options
88
+
89
+ Available event options are listed in `GoogleAPI::Calendar::VALID_EVENT_KEYS`.
90
+
91
+ ```ruby
92
+ repeat_pattern = 'WEEKLY' # 'DAILY', etc.
93
+ recurrence = ["RRULE:FREQ=#{repeat_pattern};COUNT=#{sessions}"]
94
+
95
+ event_options = {
96
+ start: start_date, end: end_date, recurrence: recurrence,
97
+ summary: event_title, description: event_description,
98
+ location: location
99
+ }
100
+ ```
101
+
102
+ To add a Meet call to an event, merge the following into `event_options`:
103
+
104
+ ```ruby
105
+ { conference: { id: meeting_id, signature: meeting_signature } }
106
+ ```
@@ -2,8 +2,8 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = 'bps-google-api'
5
- s.version = '0.3.2'
6
- s.date = '2019-06-25'
5
+ s.version = '0.4.0'
6
+ s.date = '2019-06-27'
7
7
  s.summary = 'Configured Google API'
8
8
  s.description = 'A configured Google API wrapper.'
9
9
  s.homepage = 'http://rubygems.org/gems/bps-google-api'
data/lib/google_api.rb CHANGED
@@ -1,6 +1,9 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class GoogleAPI
4
+ # Core Dependencies
5
+ require 'date'
6
+
4
7
  # Google Dependencies
5
8
  require 'google/apis/calendar_v3'
6
9
  require 'google/apis/groupssettings_v1'
@@ -10,7 +10,8 @@ class GoogleAPI
10
10
  ].freeze
11
11
  TOKEN_SCOPES ||= [
12
12
  '"https://www.googleapis.com/auth/admin.directory.group"',
13
- '"https://www.googleapis.com/auth/calendar"'
13
+ '"https://www.googleapis.com/auth/calendar"',
14
+ '"https://www.googleapis.com/auth/calendar.events"'
14
15
  ].freeze
15
16
 
16
17
  def authorize!(refresh: false, reveal: false)
@@ -6,12 +6,20 @@ class GoogleAPI
6
6
  GoogleAPI.configuration.local_path('tmp', 'run', 'last_page_token')
7
7
  end
8
8
 
9
+ require 'google_api/calendar/conference'
10
+ include GoogleAPI::Calendar::Conference
11
+
9
12
  require 'google_api/calendar/clear_test_calendar'
10
13
  include GoogleAPI::Calendar::ClearTestCalendar
11
14
 
12
15
  SERVICE_CLASS = Google::Apis::CalendarV3::CalendarService
13
16
  VALID_EVENT_KEYS ||= %i[
14
- summary start end description location recurrence conference_data conference_data_version
17
+ summary start end description location recurrence conference
18
+ conference_data
19
+ ].freeze
20
+ VALID_PATCH_KEYS ||= %i[
21
+ summary start end description location recurrence conference_data created reminders creator
22
+ etag html_link i_cal_uid id kind organizer reminders sequence status updated
15
23
  ].freeze
16
24
 
17
25
  def create(calendar_id, event_options = {})
@@ -26,8 +34,12 @@ class GoogleAPI
26
34
  call(:get_event, calendar_id, event_id)
27
35
  end
28
36
 
37
+ def patch(calendar_id, event_id, patch_options = {})
38
+ call(:patch_event, calendar_id, event_id, patch_options, conference_data_version: 1)
39
+ end
40
+
29
41
  def update(calendar_id, event_id, event_options = {})
30
- call(:update_event, calendar_id, event_id, event(event_options))
42
+ call(:update_event, calendar_id, event_id, event(event_options), conference_data_version: 1)
31
43
  end
32
44
 
33
45
  def delete(calendar_id, event_id)
@@ -58,15 +70,29 @@ class GoogleAPI
58
70
 
59
71
  private
60
72
 
61
- def event(event_options)
62
- event_options.assert_valid_keys(VALID_EVENT_KEYS)
63
- event_options[:start] = date(event_options[:start])
64
- event_options[:end] = date(event_options[:end])
73
+ def event(event_options, patch: false)
74
+ validate_keys(event_options, patch)
75
+
76
+ event_options = format_dates(event_options)
77
+ event_options = format_conference_data(event_options)
65
78
 
66
79
  Google::Apis::CalendarV3::Event.new(event_options.reject { |_, v| v.nil? })
67
80
  end
68
81
 
69
- def date(date)
82
+ def validate_keys(event_options, patch)
83
+ event_options.assert_valid_keys(VALID_EVENT_KEYS) unless patch
84
+ event_options.assert_valid_keys(VALID_PATCH_KEYS) if patch
85
+ end
86
+
87
+ def format_dates(event_options)
88
+ event_options[:start] = format_date(event_options[:start])
89
+ event_options[:end] = format_date(event_options[:end])
90
+ event_options
91
+ end
92
+
93
+ def format_date(date)
94
+ return date if date.is_a?(Google::Apis::CalendarV3::EventDateTime)
95
+
70
96
  key = date&.is_a?(String) ? :date : :date_time
71
97
  Google::Apis::CalendarV3::EventDateTime.new(key => date, time_zone: ENV['TZ'])
72
98
  end
@@ -0,0 +1,72 @@
1
+ # frozen_string_literal: true
2
+
3
+ class GoogleAPI
4
+ class Calendar < GoogleAPI::Base
5
+ module Conference
6
+ MEET_ICON ||= 'https://lh5.googleusercontent.com/proxy/bWvYBOb7O03a7HK5iKNEAPoUNPEXH1CHZjuOkiqxHx8OtyVn9sZ6Ktl8hfqBNQUUbCDg6T2unnsHx7RSkCyhrKgHcdoosAW8POQJm_ZEvZU9ZfAE7mZIBGr_tDlF8Z_rSzXcjTffVXg3M46v'
7
+
8
+ def add_conference(calendar_id, event_id)
9
+ patch_options = {
10
+ conference_data: {
11
+ create_request: { request_id: "#{Time.now.to_i}-#{SecureRandom.hex(16)}" }
12
+ }
13
+ }
14
+
15
+ call(:patch_event, calendar_id, event_id, patch_options, conference_data_version: 1)
16
+ end
17
+
18
+ private
19
+
20
+ def format_conference_data(event_options)
21
+ return event_options unless event_options.key?(:conference)
22
+
23
+ conference_options = event_options.delete(:conference)
24
+ event_options[:conference_data] = conference_hash(conference_options)
25
+ event_options
26
+ end
27
+
28
+ def conference_hash(conference_options)
29
+ if conference_options[:id] == :new
30
+ new_conference
31
+ else
32
+ conference(conference_options[:id], conference_options[:signature])
33
+ end
34
+ end
35
+
36
+ def conference(conf_id, signature)
37
+ {
38
+ conference: {
39
+ conference_id: conf_id, conference_solution: meet_solution,
40
+ entry_points: meet_entry(conf_id), signature: signature
41
+ }
42
+ }
43
+ end
44
+
45
+ def new_conference
46
+ {
47
+ create_request: {
48
+ request_id: "#{Time.now.to_i}-#{SecureRandom.hex(16)}"
49
+ }
50
+ }
51
+ end
52
+
53
+ def meet_solution
54
+ Google::Apis::CalendarV3::ConferenceSolution.new(
55
+ key: Google::Apis::CalendarV3::ConferenceSolutionKey.new(type: 'hangoutsMeet'),
56
+ name: 'Hangouts Meet',
57
+ icon_uri: MEET_ICON
58
+ )
59
+ end
60
+
61
+ def meet_entry(conf_id)
62
+ [
63
+ Google::Apis::CalendarV3::EntryPoint.new(
64
+ label: "meet.google.com/#{conf_id}",
65
+ uri: "https://meet.google.com/#{conf_id}",
66
+ entry_point_type: 'video'
67
+ )
68
+ ]
69
+ end
70
+ end
71
+ end
72
+ end
@@ -3,6 +3,7 @@
3
3
  class GoogleAPI
4
4
  module Configured
5
5
  require 'google_api/configured/calendar'
6
+ require 'google_api/configured/event'
6
7
  require 'google_api/configured/group'
7
8
  end
8
9
  end
@@ -25,6 +25,10 @@ class GoogleAPI
25
25
  self.class.api.get(calendar_id, event_id)
26
26
  end
27
27
 
28
+ def patch(event_id, event_options = {})
29
+ self.class.api.patch(calendar_id, event_id, event_options)
30
+ end
31
+
28
32
  def update(event_id, event_options = {})
29
33
  self.class.api.update(calendar_id, event_id, event_options)
30
34
  end
@@ -46,6 +50,10 @@ class GoogleAPI
46
50
  page_token: page_token, page_limit: page_limit, verbose: verbose, error: error
47
51
  )
48
52
  end
53
+
54
+ def add_conference(event_id)
55
+ self.class.api.add_conference(calendar_id, event_id)
56
+ end
49
57
  end
50
58
  end
51
59
  end
@@ -0,0 +1,40 @@
1
+ # frozen_string_literal: true
2
+
3
+ class GoogleAPI
4
+ module Configured
5
+ class Calendar
6
+ class Event
7
+ def self.api
8
+ @api ||= GoogleAPI::Calendar.new
9
+ end
10
+
11
+ attr_reader :calendar_id, :event_id
12
+
13
+ def initialize(calendar_id, event_id)
14
+ @calendar_id = calendar_id
15
+ @event_id = event_id
16
+ end
17
+
18
+ def get
19
+ self.class.api.get(calendar_id, event_id)
20
+ end
21
+
22
+ def patch(patch_options = {})
23
+ self.class.api.patch(calendar_id, event_id, patch_options)
24
+ end
25
+
26
+ def update(event_options = {})
27
+ self.class.api.update(calendar_id, event_id, event_options)
28
+ end
29
+
30
+ def delete
31
+ self.class.api.delete(calendar_id, event_id)
32
+ end
33
+
34
+ def add_conference
35
+ self.class.api.add_conference(calendar_id, event_id)
36
+ end
37
+ end
38
+ end
39
+ end
40
+ end
@@ -81,18 +81,42 @@ RSpec.describe GoogleAPI::Calendar do
81
81
  expect(subject.create(test_event)).to be_a(Google::Apis::CalendarV3::Event)
82
82
  end
83
83
 
84
+ it 'creates an event with a new conference' do
85
+ event = test_event.merge(conference: { id: :new })
86
+ expect(subject.create(event)).to be_a(Google::Apis::CalendarV3::Event)
87
+ end
88
+
89
+ it 'creates an event with conference data' do
90
+ event = subject.create(test_event)
91
+ event = subject.add_conference(event.id)
92
+ event_options = test_event.merge(
93
+ conference: {
94
+ id: event.conference_data.conference_id,
95
+ signature: event.conference_data.signature
96
+ }
97
+ )
98
+
99
+ expect(subject.create(event_options)).to be_a(Google::Apis::CalendarV3::Event)
100
+ end
101
+
84
102
  it 'gets an event ' do
85
103
  event = subject.create(test_event)
86
104
  expect(subject.get(event.id)).to be_a(Google::Apis::CalendarV3::Event)
87
105
  end
88
106
 
107
+ it 'patches an event' do
108
+ event = subject.create(test_event)
109
+ subject.patch(event.id, description: 'Patched.')
110
+
111
+ expect(subject.get(event.id).description).to eql('Patched.')
112
+ end
113
+
89
114
  it 'updates an event' do
90
115
  event = subject.create(test_event)
91
116
  updated_test_event = test_event.merge(description: 'Updated.')
92
117
  subject.update(event.id, updated_test_event)
93
- updated_event = subject.get(event.id)
94
118
 
95
- expect(updated_event.description).to eql('Updated.')
119
+ expect(subject.get(event.id).description).to eql('Updated.')
96
120
  end
97
121
 
98
122
  it 'deletes an event' do
@@ -128,5 +152,11 @@ RSpec.describe GoogleAPI::Calendar do
128
152
  end
129
153
  end
130
154
  end
155
+
156
+ it 'adds conference data' do
157
+ event = subject.create(test_event)
158
+
159
+ expect(subject.add_conference(event.id)).to be_a(Google::Apis::CalendarV3::Event)
160
+ end
131
161
  end
132
162
  end
@@ -0,0 +1,46 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'spec_helper'
4
+
5
+ RSpec.describe GoogleAPI::Configured::Calendar::Event do
6
+ def test_event
7
+ {
8
+ summary: 'API Test Event',
9
+ description: 'This is a test event generated by the API gem.',
10
+ start: DateTime.strptime('201906051200', '%Y%m%d%H%M'),
11
+ end: DateTime.strptime('201906051400', '%Y%m%d%H%M')
12
+ }
13
+ end
14
+
15
+ let(:test_cal_id) { ENV['GOOGLE_CALENDAR_ID_TEST'] }
16
+
17
+ subject do
18
+ event = GoogleAPI::Configured::Calendar.new(test_cal_id).create(test_event)
19
+ GoogleAPI::Configured::Calendar::Event.new(test_cal_id, event.id)
20
+ end
21
+
22
+ it 'gets the event' do
23
+ expect(subject.get).to be_a(Google::Apis::CalendarV3::Event)
24
+ end
25
+
26
+ it 'patches the event' do
27
+ subject.patch(description: 'Patched.')
28
+
29
+ expect(subject.get.description).to eql('Patched.')
30
+ end
31
+
32
+ it 'updates the event' do
33
+ updated_test_event = test_event.merge(description: 'Updated.')
34
+ subject.update(updated_test_event)
35
+
36
+ expect(subject.get.description).to eql('Updated.')
37
+ end
38
+
39
+ it 'deletes the event' do
40
+ expect(subject.delete).to eql('')
41
+ end
42
+
43
+ it 'adds conference data' do
44
+ expect(subject.add_conference).to be_a(Google::Apis::CalendarV3::Event)
45
+ end
46
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bps-google-api
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.2
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Julian Fiander
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-06-25 00:00:00.000000000 Z
11
+ date: 2019-06-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: exp_retry
@@ -147,14 +147,17 @@ files:
147
147
  - lib/google_api/base/authorization.rb
148
148
  - lib/google_api/calendar.rb
149
149
  - lib/google_api/calendar/clear_test_calendar.rb
150
+ - lib/google_api/calendar/conference.rb
150
151
  - lib/google_api/config.rb
151
152
  - lib/google_api/configured.rb
152
153
  - lib/google_api/configured/calendar.rb
154
+ - lib/google_api/configured/event.rb
153
155
  - lib/google_api/configured/group.rb
154
156
  - lib/google_api/group.rb
155
157
  - spec/.rubocop.yml
156
158
  - spec/lib/google_api/base_spec.rb
157
159
  - spec/lib/google_api/calendar_spec.rb
160
+ - spec/lib/google_api/event_spec.rb
158
161
  - spec/lib/google_api/group_spec.rb
159
162
  - spec/spec_helper.rb
160
163
  homepage: http://rubygems.org/gems/bps-google-api