gcevent 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: ed80a14b297c92273c4d98fd0453093b28933930
4
+ data.tar.gz: 2e91a47896486ffc72ed2bd2bf2c4fe24434670c
5
+ SHA512:
6
+ metadata.gz: 846f687faae6f667d2fd52265c4edda0769f470069fe4564d518d932420315234bb6bfe59b07ab99efaaa2f46b68230bfe80447bb7996bfe2d8bd0672695e8a6
7
+ data.tar.gz: ca595b052140336f8e529cee6a899d9c637003ec3bf3e411b1bf7f5ec188bc41d4fc4af6ed5f48085397f01cbe64ea47d6b69827417929b507a1616d79e99ebc
data/.coveralls.yml ADDED
@@ -0,0 +1 @@
1
+ service_name: travis-ci
data/.gitignore ADDED
@@ -0,0 +1,16 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
15
+ .ruby-*
16
+ /vendor/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.1.5
4
+ - 2.1.6
5
+ - 2.2.2
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in gcevent.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 ogawatti
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,128 @@
1
+ [![Build Status](https://travis-ci.org/ogawatti/gcevent.svg?branch=master)](https://travis-ci.org/ogawatti/gcevent)
2
+ [![Coverage Status](https://coveralls.io/repos/ogawatti/gcevent/badge.png?branch=master)](https://coveralls.io/r/ogawatti/gcevent?branch=master)
3
+ [<img src="https://gemnasium.com/ogawatti/gcevent.png" />](https://gemnasium.com/ogawatti/gcevent)
4
+
5
+ # Gcevent
6
+
7
+ A wrapper of Google Calendar Event API.
8
+
9
+ ## Installation
10
+
11
+ Add this line to your application's Gemfile:
12
+
13
+ ```ruby
14
+ gem 'gcevent'
15
+ ```
16
+
17
+ And then execute:
18
+
19
+ $ bundle
20
+
21
+ Or install it yourself as:
22
+
23
+ $ gem install gcevent
24
+
25
+ ## Usage
26
+
27
+ ### Calendar Setting
28
+
29
+ ```ruby
30
+ Google::Calendar.id = "Id of a target Google Calendar"
31
+ Google::Calendar.secret_key.path = "Path of xxx-privatekey.p12"
32
+ Google::Calendar.secret_key.password = "Password"
33
+ Google::Calendar.client_id = "ID of Client"
34
+ ```
35
+
36
+ ### Event#get
37
+
38
+ ```ruby
39
+ event_id = "Google Calendar Event ID"
40
+ event = Google::Calendar::Event.get(event_id)
41
+ ```
42
+
43
+ or
44
+
45
+ ```ruby
46
+ event = Google::Calendar::Event.new(event_id: event_id)
47
+ event.fetch
48
+ ```
49
+
50
+ ### Event#list
51
+
52
+ specified date or time
53
+
54
+ ```ruby
55
+ start_date = Date.today.beginning_of_week
56
+ end_date = Date.today.end_of_week
57
+ events = Google::Calendar::Event.list(start_time, end_time)
58
+ ```
59
+
60
+ today or this week or ...
61
+
62
+
63
+ ```ruby
64
+ Google::Calendar::Event.today
65
+ Google::Calendar::Event.tomorrow
66
+ Google::Calendar::Event.yesterday
67
+ Google::Calendar::Event.this_week
68
+ Google::Calendar::Event.this_month
69
+ Google::Calendar::Event.this_year
70
+ ```
71
+
72
+ ### Event#insert
73
+
74
+ ```ruby
75
+ event = Google::Calendar::Event.new
76
+ event.summary = "Inserted #{Time.now}"
77
+ event.start = { dateTime: Date.today.to_time.utc.iso8601 }
78
+ event.end = { dateTime: Date.tomorrow.to_time.utc.iso8601 }
79
+ event.insert
80
+ ```
81
+
82
+ or
83
+
84
+ ```ruby
85
+ options = { summary: "Inserted #{Time.now}",
86
+ start: { date: Date.today },
87
+ end: { date: Date.tomorrow } }
88
+ event = Google::Calendar::Event.insert(options)
89
+ ```
90
+
91
+ ### Event#quickAdd
92
+
93
+ quick insert
94
+
95
+ ```ruby
96
+ text = "Quick Added"
97
+ event = Google::Calendar::Event.quickAdd(text)
98
+ ```
99
+
100
+ ### Event#update
101
+
102
+ ```ruby
103
+ event = Google::Calendar::Event.this_week.last
104
+ event.summary = "Updated #{Time.now}"
105
+ event.update
106
+ ```
107
+
108
+ # Delete Phase
109
+
110
+ ```ruby
111
+ event = Google::Calendar::Event.this_week.first
112
+ event.delete
113
+ ```
114
+
115
+ or
116
+
117
+ ```ruby
118
+ event = Google::Calendar::Event.this_week.last
119
+ Google::Calendar::Event.delete(event.id)
120
+ ```
121
+
122
+ ## Contributing
123
+
124
+ 1. Fork it ( https://github.com/[my-github-username]/gcevent/fork )
125
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
126
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
127
+ 4. Push to the branch (`git push origin my-new-feature`)
128
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,7 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
7
+
data/gcevent.gemspec ADDED
@@ -0,0 +1,30 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "gcevent"
7
+ spec.version = "0.0.1"
8
+ spec.authors = ["ogawatti"]
9
+ spec.email = ["ogawattim@gmail.com"]
10
+ spec.summary = %q{A wrapper of Google Calendar Event API.}
11
+ spec.description = %q{}
12
+ spec.homepage = "https://github.com/ogawatti/gcevent"
13
+ spec.license = "MIT"
14
+
15
+ spec.files = `git ls-files -z`.split("\x0")
16
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
17
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
18
+ spec.require_paths = ["lib"]
19
+
20
+ spec.add_dependency "google-api-client"
21
+ spec.add_dependency "activesupport"
22
+ spec.add_dependency "hashie"
23
+
24
+ spec.add_development_dependency "bundler", "~> 1.7"
25
+ spec.add_development_dependency "rake", "~> 10.0"
26
+ spec.add_development_dependency "rspec"
27
+ spec.add_development_dependency "pry"
28
+ spec.add_development_dependency "simplecov"
29
+ spec.add_development_dependency "coveralls"
30
+ end
data/lib/gcevent.rb ADDED
@@ -0,0 +1,4 @@
1
+ require "google/calendar"
2
+
3
+ module Gcevent
4
+ end
@@ -0,0 +1,67 @@
1
+ require 'google/service_account'
2
+ require 'google/calendar/event'
3
+ require 'google/api_client'
4
+ require 'hashie'
5
+
6
+ module Google
7
+ class SecretKey < Hashie::Mash; end
8
+
9
+ module Calendar
10
+ class << self
11
+ attr_accessor :id, :client_id
12
+ end
13
+
14
+ extend self
15
+
16
+ def api
17
+ authorize unless authorized?
18
+ client.discovered_api('calendar', 'v3')
19
+ end
20
+
21
+ def client
22
+ @client ||= Google::APIClient.new(:application_name => '')
23
+ end
24
+
25
+ def authorize
26
+ raise Errno::ENOENT.new(secret_key.path) unless File.exist?(secret_key.path)
27
+ client.authorization = Signet::OAuth2::Client.new(
28
+ token_credential_uri: service_account.token_uri,
29
+ audience: service_account.token_uri,
30
+ scope: service_account.scope,
31
+ issuer: service_account.client_email,
32
+ signing_key: signing_key
33
+ )
34
+ client.authorization.fetch_access_token!
35
+ @authorized = true
36
+ end
37
+
38
+ def secret_key
39
+ @secret_key ||= Google::SecretKey.new({ path: nil, password: nil })
40
+ end
41
+
42
+ def service_account
43
+ Google::ServiceAccount.new(client_id)
44
+ end
45
+
46
+ def signing_key
47
+ Google::APIClient::KeyUtils.load_from_pkcs12(secret_key.path, secret_key.password)
48
+ end
49
+
50
+ def authorized?
51
+ @authorized
52
+ end
53
+
54
+ # MEMO : body_objectでなくbodyを使う
55
+ # * 公式のReference的にはbody_object
56
+ # * body_objectを空文字指定でEvent#getすると400 Bad Request
57
+ # * 必要ないときはパラメータに含めない方がよさそう
58
+ # * bodyパラメータなら空文字指定でもOK
59
+ def execute(api_method, parameters={}, body="")
60
+ options = { api_method: api_method,
61
+ parameters: parameters,
62
+ body: body,
63
+ headers: { 'Content-Type' => 'application/json' } }
64
+ client.execute!(options)
65
+ end
66
+ end
67
+ end
@@ -0,0 +1,7 @@
1
+ module Google
2
+ module Calendar
3
+ class Errors
4
+ class Error < StandardError; end
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,192 @@
1
+ require 'hashie'
2
+ require 'active_support'
3
+ require 'active_support/core_ext'
4
+
5
+ module Google
6
+ module Calendar
7
+ class Event
8
+ # API Reference
9
+ # * https://developers.google.com/google-apps/calendar/v3/reference/#Events
10
+ #
11
+ # Supported Google Calendar API (Events)
12
+ # Method VERB PATH
13
+ # -------- ------ --------------------------------------
14
+ # get GET /calendars/:calendarId/events/:eventId
15
+ # list GET /calendars/:calendarId/events
16
+ # insert POST /calendars/:calendarId/events
17
+ # delete DELETE /calendars/:calendarId/events/:eventId
18
+ # quickAdd POST /calendars/:calendarId/events/quickAdd
19
+ # update PUT /calendars/:calendarId/events/:eventId
20
+
21
+ attr_reader :id, :kind, :etag, :status, :htmlLink, :created, :updated
22
+ attr_reader :creator, :organizer, :transparency, :iCalUID, :sequence, :reminders
23
+ attr_accessor :summary, :start, :end
24
+ def initialize(options={})
25
+ update_attributes(options)
26
+ end
27
+
28
+ # [API] get
29
+ # GET /calendars/:calendarId/events/eventId
30
+ def self.get(id)
31
+ self.new(id: id).fetch
32
+ end
33
+
34
+ def fetch
35
+ params = { calendarId: Calendar.id, eventId: id }
36
+ request(Calendar.api.events.get, params)
37
+ end
38
+
39
+ # [API] list
40
+ # GET /calendars/:calendarId/events
41
+ def self.list(st, et)
42
+ params = { calendarId: Calendar.id,
43
+ orderBy: 'startTime',
44
+ timeMax: et.to_time.utc.iso8601,
45
+ timeMin: st.to_time.utc.iso8601,
46
+ singleEvents: 'True' }
47
+ request(Calendar.api.events.list, params)
48
+ end
49
+
50
+ # [API] insert
51
+ # POST /calendars/:calendarId/events
52
+ def insert
53
+ params = { calendarId: Calendar.id }
54
+ body = self.to_json
55
+ request(Calendar.api.events.insert, params, body)
56
+ end
57
+
58
+ def self.insert(options={})
59
+ self.new(options).insert
60
+ end
61
+
62
+ # [API] delete
63
+ # DELETE /calendars/:calendarId/events/:eventId
64
+ def delete
65
+ params = { calendarId: Calendar.id, eventId: id }
66
+ request(Calendar.api.events.delete, params)
67
+ self
68
+ end
69
+
70
+ def self.delete(id)
71
+ self.new(id: id).fetch.delete
72
+ end
73
+
74
+ # [API] quickAdd
75
+ # POST /calendars/:calendarId/events/quickAdd
76
+ def self.quickAdd(text)
77
+ params = { calendarId: Calendar.id, text: text }
78
+ request(Calendar.api.events.quick_add, params)
79
+ end
80
+
81
+ # [API] update
82
+ # PUT /calendars/:calendarId/events/:eventId
83
+ def update
84
+ params = { calendarId: Calendar.id, eventId: id }
85
+ body = self.to_json
86
+ request(Calendar.api.events.update, params, body)
87
+ self
88
+ end
89
+
90
+ def to_hash
91
+ self.instance_variables.inject(Hashie::Mash.new) do |hash, name|
92
+ key = name.to_s.delete("@").to_sym
93
+ hash[key] = self.instance_variable_get(name)
94
+ hash
95
+ end
96
+ end
97
+
98
+ def to_json
99
+ self.to_hash.to_json
100
+ end
101
+
102
+ def num_of_days
103
+ start_date = (self.start.date || self.start.dateTime).to_date
104
+ end_date = (self.end.date || self.end.dateTime ).to_date
105
+ (end_date - start_date).to_i + 1
106
+ end
107
+
108
+ def self.today
109
+ start_time = Date.today
110
+ end_time = Date.tomorrow
111
+ list(start_time, end_time)
112
+ end
113
+
114
+ def self.tomorrow
115
+ start_time = Date.tomorrow
116
+ end_time = Date.tomorrow.tomorrow
117
+ list(start_time, end_time)
118
+ end
119
+
120
+ def self.yesterday
121
+ start_time = Date.yesterday
122
+ end_time = Date.today
123
+ list(start_time, end_time)
124
+ end
125
+
126
+ def self.this_week
127
+ start_time = Date.today.beginning_of_week
128
+ end_time = Date.today.end_of_week
129
+ list(start_time, end_time)
130
+ end
131
+
132
+ def self.this_month
133
+ start_time = Date.today.beginning_of_month
134
+ end_time = Date.today.end_of_month
135
+ list(start_time, end_time)
136
+ end
137
+
138
+ def self.this_year
139
+ start_time = Date.today.beginning_of_year
140
+ end_time = Date.today.end_of_year
141
+ list(start_time, end_time)
142
+ end
143
+
144
+ private
145
+
146
+ def update_attributes(options={})
147
+ opts = Hashie::Mash.new(options)
148
+ opts.each do |key, value|
149
+ instance_variable_set("@#{key}".to_sym, value) if respond_to?(key)
150
+ end
151
+ end
152
+
153
+ def request(api_method, params={}, body="")
154
+ data = self.class.execute(api_method, params, body)
155
+ if data.respond_to?(:to_hash)
156
+ update_attributes(data.to_hash)
157
+ elsif data.nil?
158
+ {}
159
+ else
160
+ raise data.to_s
161
+ end
162
+ self
163
+ end
164
+
165
+ def self.request(api_method, params={}, body="")
166
+ data = execute(api_method, params, body)
167
+ if data.respond_to?(:items)
168
+ data_to_events(data.items)
169
+ elsif data.respond_to?(:to_hash)
170
+ data_to_event(data)
171
+ else
172
+ raise data.to_s
173
+ end
174
+ end
175
+
176
+ def self.execute(api_method, params={}, body="")
177
+ response = Calendar.execute(api_method, params, body)
178
+ response.data
179
+ end
180
+
181
+ def self.data_to_event(event_data)
182
+ self.new(event_data.to_hash)
183
+ end
184
+
185
+ def self.data_to_events(events_data)
186
+ events_data.inject([]) do |events, event_data|
187
+ events << data_to_event(event_data)
188
+ end
189
+ end
190
+ end
191
+ end
192
+ end
@@ -0,0 +1,31 @@
1
+ module Google
2
+ class ServiceAccount
3
+ CLIENT_ID_SUFFIX = ".apps.googleusercontent.com"
4
+ CLIENT_EMAIL_DOMAIN = "developer.gserviceaccount.com"
5
+ ACCOUNTS_GOOGLE_COM_BASE_URI = "https://accounts.google.com"
6
+ WWW_GOOGLEAPIS_COM_BASE_URI = "https://www.googleapis.com"
7
+
8
+ attr_reader :id
9
+ attr_reader :auth_uri, :token_uri
10
+ attr_reader :client_id, :client_email
11
+ attr_reader :auth_provider_x509_cert_url, :client_x509_cert_url
12
+ attr_reader :scope
13
+
14
+ def initialize(client_id)
15
+ @id = client_id.include?(CLIENT_ID_SUFFIX) ? scan_id(client_id) : client_id
16
+ @auth_uri = ACCOUNTS_GOOGLE_COM_BASE_URI + "/o/oauth2/auth"
17
+ @token_uri = ACCOUNTS_GOOGLE_COM_BASE_URI + "/o/oauth2/token"
18
+ @client_id = @id + CLIENT_ID_SUFFIX
19
+ @client_email = @id + "@" + CLIENT_EMAIL_DOMAIN
20
+ @auth_provider_x509_cert_url = WWW_GOOGLEAPIS_COM_BASE_URI + "/oauth2/v1/certs"
21
+ @client_x509_cert_url = WWW_GOOGLEAPIS_COM_BASE_URI + "/robot/v1/metadata/x509" + @client_email
22
+ @scope = WWW_GOOGLEAPIS_COM_BASE_URI + "/auth/calendar"
23
+ end
24
+
25
+ private
26
+
27
+ def scan_id(client_id)
28
+ client_id.scan(/^(.*)#{CLIENT_ID_SUFFIX}$/).first.first
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,340 @@
1
+ require 'spec_helper'
2
+ require 'pry'
3
+
4
+ describe Google::Calendar::Event do
5
+
6
+ let(:event_instance_variables) { [ :id, :kind, :etag, :status, :htmlLink, :created, :updated,
7
+ :creator, :organizer, :transparency, :iCalUID, :sequence, :reminders,
8
+ :summary, :start, :end ] }
9
+ let(:calendar_id) { "aaaaaaaaaaaaaaaaaaaaaaaaaa@group.calendar.google.com" }
10
+ let(:client_id) { "1000000000000-aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.apps.googleusercontent.com" }
11
+ let(:client_execute_options) { { api_method: api_method,
12
+ parameters: parameters,
13
+ body: body,
14
+ headers: { 'Content-Type' => 'application/json' } } }
15
+ let(:test_data) { { kind: "calendar#event",
16
+ etag: "\"1000000000000000\"",
17
+ id: id,
18
+ status: "confirmed",
19
+ htmlLink: "https://www.google.com/calendar/event?eid=aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
20
+ created: "2015-07-07T00:00:00.000Z",
21
+ updated: "2015-07-07T01:00:00.000Z",
22
+ summary: summary,
23
+ creator: { email: "testuser@gmail.com", displayName: "testuser" },
24
+ organizer: { email: calendar_id, displayName: "testcalendar", self: true },
25
+ start: start_date,
26
+ end: end_date,
27
+ transparency: "transparent",
28
+ iCalUID: id + "@google.com",
29
+ sequence: 0,
30
+ reminders: { useDefault: true } } }
31
+ let(:id) { "aaaaaaaaaaaaaaaaaaaaaaaaaa" }
32
+ let(:summary) { "Test Event" }
33
+ let(:start_date) { { date: Date.today } }
34
+ let(:end_date) { { date: Date.tomorrow } }
35
+
36
+ before do
37
+ allow(Google::APIClient::KeyUtils).to receive(:load_key) { OpenSSL::PKey::RSA.new }
38
+ allow(File).to receive(:exist?) { true }
39
+ allow_any_instance_of(Signet::OAuth2::Client).to receive(:fetch_access_token!) { true }
40
+ Google::Calendar.client_id = client_id
41
+ Google::Calendar.id = calendar_id
42
+ allow(Google::Calendar).to receive(:authorize?) { true }
43
+ end
44
+
45
+ describe '#initialize' do
46
+ context 'without otpion' do
47
+ it 'should be instance of Google::Calendar::Event' do
48
+ event = Google::Calendar::Event.new
49
+ expect(event).to be_instance_of Google::Calendar::Event
50
+ event_instance_variables.each{|name| expect(event.send(name)).to be_nil }
51
+ end
52
+ end
53
+
54
+ context 'with all otpion' do
55
+ let(:options) { { summary: summary, start: { date: Date.today }, end: { date: Date.tomorrow } } }
56
+
57
+ it 'should be instance of Google::Calendar::Event' do
58
+ event = Google::Calendar::Event.new(options)
59
+ expect(event).to be_instance_of Google::Calendar::Event
60
+ event_instance_variables.each do |name|
61
+ case name
62
+ when :summary
63
+ expect(event.send(name)).to eq options[name]
64
+ when :start, :end
65
+ expect(event.send(name).date).to eq options[name][:date]
66
+ else
67
+ expect(event.send(name)).to be_nil
68
+ end
69
+ end
70
+ end
71
+ end
72
+ end
73
+
74
+ # [API] get
75
+ # GET /calendars/:calendarId/events/eventId
76
+ describe 'GET /calendars/:calendarId/events/eventId' do
77
+ let(:event) { Google::Calendar::Event.new(options) }
78
+ let(:options) { { id: id, summary: summary, start: start_date, end: end_date } }
79
+
80
+ let(:api_method) { Google::Calendar.api.events.get }
81
+ let(:parameters) { { calendarId: calendar_id, eventId: id } }
82
+ let(:body) { "" }
83
+
84
+ let(:response) { Hashie::Mash.new( { status: status, data: data } ) }
85
+ let(:status) { 200 }
86
+ let(:data) { test_data }
87
+
88
+ before do
89
+ expect_any_instance_of(Google::APIClient).to receive(:execute!).with(client_execute_options) { response }
90
+ end
91
+
92
+ describe '.get' do
93
+ it 'should be instance of Google::Calendar::Event' do
94
+ event = Google::Calendar::Event.get(id)
95
+ expect(event).to be_instance_of Google::Calendar::Event
96
+ expect_event_to_eq_test_data(event, test_data)
97
+ end
98
+ end
99
+
100
+ describe '#fetch' do
101
+ it 'should be instance of Google::Calendar::Event' do
102
+ event.fetch
103
+ expect(event).to be_instance_of Google::Calendar::Event
104
+ expect_event_to_eq_test_data(event, test_data)
105
+ end
106
+ end
107
+ end
108
+
109
+ # [API] list
110
+ # GET /calendars/:calendarId/events
111
+ describe 'GET /calendars/:calendarId/events' do
112
+ let(:api_method) { Google::Calendar.api.events.list }
113
+ let(:parameters) { { calendarId: calendar_id,
114
+ orderBy: 'startTime',
115
+ timeMax: end_time.to_time.utc.iso8601,
116
+ timeMin: start_time.to_time.utc.iso8601,
117
+ singleEvents: 'True' } }
118
+ let(:body) { "" }
119
+
120
+ let(:response) { Hashie::Mash.new( { status: status, data: data } ) }
121
+ let(:status) { 200 }
122
+ let(:data) { { items: [ test_data ] } }
123
+
124
+ let(:start_time) { Date.today.beginning_of_week }
125
+ let(:end_time) { Date.today.end_of_week }
126
+
127
+ describe '.list' do
128
+ before do
129
+ expect_any_instance_of(Google::APIClient).to receive(:execute!).with(client_execute_options) { response }
130
+ end
131
+
132
+ it 'should be instance of Google::Calendar::Event' do
133
+ events = Google::Calendar::Event.list(start_time, end_time)
134
+ expect(events.size).to eq 1
135
+ event = events.first
136
+ expect(event).to be_instance_of Google::Calendar::Event
137
+ expect_event_to_eq_test_data(event, test_data)
138
+ end
139
+ end
140
+
141
+ shared_examples 'Called Event.list and Recieve Start and End' do
142
+ before { expect(Google::Calendar::Event).to receive(:list).with(start_time, end_time) }
143
+ it { is_expected.not_to raise_error }
144
+ end
145
+
146
+ describe '.today' do
147
+ let(:start_time) { Date.today }
148
+ let(:end_time) { Date.tomorrow }
149
+ subject { lambda { Google::Calendar::Event.today } }
150
+ it_behaves_like 'Called Event.list and Recieve Start and End'
151
+ end
152
+
153
+ describe '.tomorrow' do
154
+ let(:start_time) { Date.tomorrow }
155
+ let(:end_time) { Date.tomorrow.tomorrow }
156
+ subject { lambda { Google::Calendar::Event.tomorrow } }
157
+ it_behaves_like 'Called Event.list and Recieve Start and End'
158
+ end
159
+
160
+ describe '.yesterday' do
161
+ let(:start_time) { Date.yesterday }
162
+ let(:end_time) { Date.today }
163
+ subject { lambda { Google::Calendar::Event.yesterday } }
164
+ it_behaves_like 'Called Event.list and Recieve Start and End'
165
+ end
166
+
167
+ describe '.this_week' do
168
+ let(:start_time) { Date.today.beginning_of_week }
169
+ let(:end_time) { Date.today.end_of_week }
170
+ subject { lambda { Google::Calendar::Event.this_week } }
171
+ it_behaves_like 'Called Event.list and Recieve Start and End'
172
+ end
173
+
174
+ describe '.this_month' do
175
+ let(:start_time) { Date.today.beginning_of_month }
176
+ let(:end_time) { Date.today.end_of_month }
177
+ subject { lambda { Google::Calendar::Event.this_month } }
178
+ it_behaves_like 'Called Event.list and Recieve Start and End'
179
+ end
180
+
181
+ describe '.this_year' do
182
+ let(:start_time) { Date.today.beginning_of_year }
183
+ let(:end_time) { Date.today.end_of_year }
184
+ subject { lambda { Google::Calendar::Event.this_year } }
185
+ it_behaves_like 'Called Event.list and Recieve Start and End'
186
+ end
187
+ end
188
+
189
+ # [API] insert
190
+ # POST /calendars/:calendarId/events
191
+ describe 'POST /calendars/:calendarId/events' do
192
+ let(:event) { Google::Calendar::Event.new(options) }
193
+ let(:options) { { summary: summary, start: start_date, end: end_date } }
194
+
195
+ let(:api_method) { Google::Calendar.api.events.insert }
196
+ let(:parameters) { { calendarId: calendar_id } }
197
+ let(:body) { event.to_json }
198
+
199
+ let(:response) { Hashie::Mash.new( { status: status, data: test_data } ) }
200
+ let(:status) { 200 }
201
+ let(:data) { test_data }
202
+
203
+ before do
204
+ expect_any_instance_of(Google::APIClient).to receive(:execute!).with(client_execute_options) { response }
205
+ end
206
+
207
+ describe '#insert' do
208
+ it 'should be instance of Google::Calendar::Event' do
209
+ event.insert
210
+ expect(event).to be_instance_of Google::Calendar::Event
211
+ expect_event_to_eq_test_data(event, test_data)
212
+ end
213
+ end
214
+
215
+ describe '.insert' do
216
+ it 'should be instance of Google::Calendar::Event' do
217
+ event = Google::Calendar::Event.insert(options)
218
+ expect(event).to be_instance_of Google::Calendar::Event
219
+ expect_event_to_eq_test_data(event, test_data)
220
+ end
221
+ end
222
+ end
223
+
224
+ # [API] delete
225
+ # DELETE /calendars/:calendarId/events/:eventId
226
+ describe 'DELETE /calendars/:calendarId/events/:eventId' do
227
+ let(:event) { Google::Calendar::Event.new(options) }
228
+ let(:options) { { id: id, summary: summary, start: start_date, end: end_date } }
229
+
230
+ let(:api_method) { Google::Calendar.api.events.delete }
231
+ let(:parameters) { { calendarId: calendar_id, eventId: id } }
232
+ let(:body) { "" }
233
+
234
+ let(:response) { Hashie::Mash.new( { status: status, data: test_data } ) }
235
+ let(:status) { 204 }
236
+ let(:data) { nil }
237
+
238
+ before do
239
+ allow_any_instance_of(Google::Calendar::Event).to receive(:fetch) { event }
240
+ expect_any_instance_of(Google::APIClient).to receive(:execute!).with(client_execute_options) { response }
241
+ end
242
+
243
+ describe '#delete' do
244
+ it 'should be instance of Google::Calendar::Event' do
245
+ event.delete
246
+ expect(event).to be_instance_of Google::Calendar::Event
247
+ expect_event_to_eq_test_data(event, test_data)
248
+ end
249
+ end
250
+
251
+ describe '.delete' do
252
+ it 'should be instance of Google::Calendar::Event' do
253
+ event = Google::Calendar::Event.delete(id)
254
+ expect(event).to be_instance_of Google::Calendar::Event
255
+ expect_event_to_eq_test_data(event, test_data)
256
+ end
257
+ end
258
+ end
259
+
260
+ # [API] quickAdd
261
+ # POST /calendars/:calendarId/events/quickAdd
262
+ describe 'POST /calendars/:calendarId/events/quickAdd' do
263
+ let(:event) { Google::Calendar::Event.new(options) }
264
+ let(:options) { { summary: summary, start: start_date, end: end_date } }
265
+
266
+ let(:api_method) { Google::Calendar.api.events.quick_add }
267
+ let(:parameters) { { calendarId: calendar_id, text: summary } }
268
+ let(:body) { "" }
269
+
270
+ let(:response) { Hashie::Mash.new( { status: status, data: test_data } ) }
271
+ let(:status) { 200 }
272
+ let(:data) { test_data }
273
+
274
+ before do
275
+ expect_any_instance_of(Google::APIClient).to receive(:execute!).with(client_execute_options) { response }
276
+ end
277
+
278
+ describe '#quickAdd' do
279
+ it 'should be instance of Google::Calendar::Event' do
280
+ event = Google::Calendar::Event.quickAdd(summary)
281
+ expect(event).to be_instance_of Google::Calendar::Event
282
+ expect_event_to_eq_test_data(event, test_data)
283
+ end
284
+ end
285
+ end
286
+
287
+ # [API] update
288
+ # PUT /calendars/:calendarId/events/:eventId
289
+ describe 'PUT /calendars/:calendarId/events/:eventId' do
290
+ let(:event) { Google::Calendar::Event.new(options) }
291
+ let(:options) { { id: id, summary: summary, start: start_date, end: end_date } }
292
+
293
+ let(:api_method) { Google::Calendar.api.events.update }
294
+ let(:parameters) { { calendarId: calendar_id, eventId: id } }
295
+ let(:body) { event.to_json }
296
+
297
+ let(:response) { Hashie::Mash.new( { status: status, data: test_data } ) }
298
+ let(:status) { 200 }
299
+ let(:data) { test_data }
300
+
301
+ before do
302
+ expect_any_instance_of(Google::APIClient).to receive(:execute!).with(client_execute_options) { response }
303
+ end
304
+
305
+ describe '#update' do
306
+ it 'should be instance of Google::Calendar::Event' do
307
+ event.update
308
+ expect(event.summary).to eq summary
309
+ expect(event).to be_instance_of Google::Calendar::Event
310
+ expect_event_to_eq_test_data(event, test_data)
311
+ end
312
+ end
313
+ end
314
+
315
+ describe '#num_of_days' do
316
+ let(:event) { Google::Calendar::Event.new(options) }
317
+ let(:options) { { summary: summary, start: start_date, end: end_date } }
318
+
319
+ # num_of_days : today ~ tomorrow
320
+ subject { event.num_of_days }
321
+ it { is_expected.to eq 2 }
322
+ end
323
+
324
+ def expect_event_to_eq_test_data(event, test_data)
325
+ test_data.each_key do |key|
326
+ case key
327
+ when :creator, :organizer
328
+ expect(event.send(key).email).to eq test_data[key][:email]
329
+ expect(event.send(key).displayName).to eq test_data[key][:displayName]
330
+ expect(event.send(key).self).to eq test_data[key][:self] if key == :organizer
331
+ when :start, :end
332
+ expect(event.send(key).date).to eq test_data[key][:date]
333
+ when :reminders
334
+ expect(event.send(key).useDefault).to eq test_data[key][:useDefault]
335
+ else
336
+ expect(event.send(key)).to eq test_data[key]
337
+ end
338
+ end
339
+ end
340
+ end
@@ -0,0 +1,77 @@
1
+ require 'spec_helper'
2
+
3
+ describe Google::Calendar do
4
+ let(:id) { "aaaaaaaaaaaaaaaaaaaaaaaaaa@group.calendar.google.com" }
5
+ let(:client_id) { "1000000000000-aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.apps.googleusercontent.com" }
6
+ let(:event_id) { "aaaaaaaaaaaaaaaaaaaaaaaaaa" }
7
+
8
+ before do
9
+ allow(Google::APIClient::KeyUtils).to receive(:load_key) { OpenSSL::PKey::RSA.new }
10
+ allow(File).to receive(:exist?) { true }
11
+ allow_any_instance_of(Signet::OAuth2::Client).to receive(:fetch_access_token!) { true }
12
+ Google::Calendar.client_id = client_id
13
+ end
14
+
15
+ describe '.api' do
16
+ before { allow(Google::Calendar).to receive(:authorized?) { true } }
17
+ subject { Google::Calendar.api }
18
+ it { is_expected.to be_instance_of Google::APIClient::API }
19
+ end
20
+
21
+ describe '.client' do
22
+ subject { Google::Calendar.client }
23
+ it { is_expected.to be_instance_of Google::APIClient }
24
+ end
25
+
26
+ describe '.authorize' do
27
+ subject { Google::Calendar.authorize }
28
+ it { is_expected.to eq true }
29
+ end
30
+
31
+ describe '.secret_key' do
32
+ subject { Google::Calendar.secret_key }
33
+ it { is_expected.to be_instance_of Google::SecretKey }
34
+ end
35
+
36
+ describe '.service_account' do
37
+ subject { Google::Calendar.service_account }
38
+ it { is_expected.to be_instance_of Google::ServiceAccount }
39
+ end
40
+
41
+ describe '.singing_key' do
42
+ subject { Google::Calendar.signing_key }
43
+ it { is_expected.to be_instance_of OpenSSL::PKey::RSA }
44
+ end
45
+
46
+ describe 'authorized?' do
47
+ subject { Google::Calendar.authorized? }
48
+
49
+ context 'authorized instance is false or nil' do
50
+ before { Google::Calendar.instance_variable_set(:@authorized, false) }
51
+ it { is_expected.to eq false }
52
+ end
53
+
54
+ context 'authorized instance is true' do
55
+ before { Google::Calendar.instance_variable_set(:@authorized, true) }
56
+ it { is_expected.to eq true }
57
+ end
58
+ end
59
+
60
+ describe 'execute' do
61
+ let(:method) { Google::Calendar.api.events.get.class }
62
+ let(:parameters) { { calendarId: id, eventId: event_id } }
63
+ let(:body) { "" }
64
+ let(:options) { { api_method: method,
65
+ parameters: parameters,
66
+ body: body,
67
+ headers: { 'Content-Type' => 'application/json' } } }
68
+
69
+ before do
70
+ expect_any_instance_of(Google::APIClient).to receive(:execute!).with(options) { true }
71
+ allow(Google::Calendar).to receive(:authorize?) { true }
72
+ end
73
+
74
+ subject { Google::Calendar.execute(method, parameters, body) }
75
+ it { is_expected.to eq true }
76
+ end
77
+ end
@@ -0,0 +1,52 @@
1
+ require 'spec_helper'
2
+
3
+ describe Google::ServiceAccount do
4
+ let(:client_id_suffix) { ".apps.googleusercontent.com" }
5
+ let(:client_email_domain) { "developer.gserviceaccount.com" }
6
+ let(:accounts_google_com_base_uri) { "https://accounts.google.com" }
7
+ let(:www_googleapis_com_base_uri) { "https://www.googleapis.com" }
8
+
9
+ let(:id) { "1000000000000-aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" }
10
+ let(:client_id) { id + ".apps.googleusercontent.com" }
11
+ let(:auth_uri) { accounts_google_com_base_uri + "/o/oauth2/auth" }
12
+ let(:token_uri) { accounts_google_com_base_uri + "/o/oauth2/token" }
13
+ let(:client_email) { id + "@" + client_email_domain }
14
+ let(:auth_provider_x509_cert_url) { www_googleapis_com_base_uri + "/oauth2/v1/certs" }
15
+ let(:client_x509_cert_url) { www_googleapis_com_base_uri + "/robot/v1/metadata/x509" + client_email }
16
+ let(:scope) { www_googleapis_com_base_uri + "/auth/calendar" }
17
+
18
+
19
+ describe '::CLIENT_ID_SUFFIX' do
20
+ subject { Google::ServiceAccount::CLIENT_ID_SUFFIX }
21
+ it { is_expected.to eq client_id_suffix }
22
+ end
23
+
24
+ describe '::CLIENT_EMAIL_DOMAIN' do
25
+ subject { Google::ServiceAccount::CLIENT_EMAIL_DOMAIN }
26
+ it { is_expected.to eq client_email_domain }
27
+ end
28
+
29
+ describe '::ACCOUNTS_GOOGLE_COM_BASE_URI' do
30
+ subject { Google::ServiceAccount::ACCOUNTS_GOOGLE_COM_BASE_URI }
31
+ it { is_expected.to eq accounts_google_com_base_uri }
32
+ end
33
+
34
+ describe '::WWW_GOOGLEAPIS_COM_BASE_URI' do
35
+ subject { Google::ServiceAccount::WWW_GOOGLEAPIS_COM_BASE_URI }
36
+ it { is_expected.to eq www_googleapis_com_base_uri }
37
+ end
38
+
39
+ describe '#initialize' do
40
+ it 'should be instance of Google::ServiceAccount' do
41
+ service_account = Google::ServiceAccount.new(client_id)
42
+ expect(service_account.id).to eq id
43
+ expect(service_account.auth_uri).to eq auth_uri
44
+ expect(service_account.token_uri).to eq token_uri
45
+ expect(service_account.client_id).to eq client_id
46
+ expect(service_account.client_email).to eq client_email
47
+ expect(service_account.auth_provider_x509_cert_url).to eq auth_provider_x509_cert_url
48
+ expect(service_account.client_x509_cert_url).to eq client_x509_cert_url
49
+ expect(service_account.scope).to eq scope
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,15 @@
1
+ $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
2
+
3
+ require 'simplecov'
4
+ require 'coveralls'
5
+
6
+ SimpleCov.formatter = SimpleCov::Formatter::MultiFormatter[
7
+ SimpleCov::Formatter::HTMLFormatter,
8
+ Coveralls::SimpleCov::Formatter
9
+ ]
10
+ SimpleCov.start do
11
+ add_filter '.bundle/'
12
+ add_filter '/spec/'
13
+ end
14
+
15
+ require 'gcevent'
metadata ADDED
@@ -0,0 +1,192 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: gcevent
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - ogawatti
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-08-06 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: google-api-client
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: activesupport
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: hashie
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: bundler
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '1.7'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '1.7'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rake
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '10.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '10.0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rspec
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: pry
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: simplecov
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ - !ruby/object:Gem::Dependency
126
+ name: coveralls
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - ">="
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - ">="
137
+ - !ruby/object:Gem::Version
138
+ version: '0'
139
+ description: ''
140
+ email:
141
+ - ogawattim@gmail.com
142
+ executables: []
143
+ extensions: []
144
+ extra_rdoc_files: []
145
+ files:
146
+ - ".coveralls.yml"
147
+ - ".gitignore"
148
+ - ".rspec"
149
+ - ".travis.yml"
150
+ - Gemfile
151
+ - LICENSE.txt
152
+ - README.md
153
+ - Rakefile
154
+ - gcevent.gemspec
155
+ - lib/gcevent.rb
156
+ - lib/google/calendar.rb
157
+ - lib/google/calendar/errors.rb
158
+ - lib/google/calendar/event.rb
159
+ - lib/google/service_account.rb
160
+ - spec/google/calendar/event_spec.rb
161
+ - spec/google/calendar_spec.rb
162
+ - spec/google/service_account_spec.rb
163
+ - spec/spec_helper.rb
164
+ homepage: https://github.com/ogawatti/gcevent
165
+ licenses:
166
+ - MIT
167
+ metadata: {}
168
+ post_install_message:
169
+ rdoc_options: []
170
+ require_paths:
171
+ - lib
172
+ required_ruby_version: !ruby/object:Gem::Requirement
173
+ requirements:
174
+ - - ">="
175
+ - !ruby/object:Gem::Version
176
+ version: '0'
177
+ required_rubygems_version: !ruby/object:Gem::Requirement
178
+ requirements:
179
+ - - ">="
180
+ - !ruby/object:Gem::Version
181
+ version: '0'
182
+ requirements: []
183
+ rubyforge_project:
184
+ rubygems_version: 2.4.8
185
+ signing_key:
186
+ specification_version: 4
187
+ summary: A wrapper of Google Calendar Event API.
188
+ test_files:
189
+ - spec/google/calendar/event_spec.rb
190
+ - spec/google/calendar_spec.rb
191
+ - spec/google/service_account_spec.rb
192
+ - spec/spec_helper.rb