Module: Google::Calendar

Extended by:
Calendar
Included in:
Calendar
Defined in:
lib/google/calendar.rb,
lib/google/calendar/event.rb,
lib/google/calendar/errors.rb

Defined Under Namespace

Classes: Errors, Event

Class Attribute Summary collapse

Instance Method Summary collapse

Class Attribute Details

.client_idObject

Returns the value of attribute client_id.



11
12
13
# File 'lib/google/calendar.rb', line 11

def client_id
  @client_id
end

.idObject

Returns the value of attribute id.



11
12
13
# File 'lib/google/calendar.rb', line 11

def id
  @id
end

Instance Method Details

#apiObject



16
17
18
19
# File 'lib/google/calendar.rb', line 16

def api
  authorize unless authorized?
  client.discovered_api('calendar', 'v3')
end

#authorizeObject

Raises:

  • (Errno::ENOENT)


25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/google/calendar.rb', line 25

def authorize
  raise Errno::ENOENT.new(secret_key.path) unless File.exist?(secret_key.path)
  client.authorization = Signet::OAuth2::Client.new(
    token_credential_uri: .token_uri,
    audience:             .token_uri,
    scope:                .scope,
    issuer:               .client_email,
    signing_key:          signing_key
  )
  client.authorization.fetch_access_token!
  @authorized = true
end

#authorized?Boolean

Returns:

  • (Boolean)


50
51
52
# File 'lib/google/calendar.rb', line 50

def authorized?
  @authorized
end

#clientObject



21
22
23
# File 'lib/google/calendar.rb', line 21

def client
  @client ||= Google::APIClient.new(:application_name => '')
end

#execute(api_method, parameters = {}, body = "") ⇒ Object

MEMO : body_objectでなくbodyを使う

* 公式のReference的にはbody_object
 * body_objectを空文字指定でEvent#getすると400 Bad Request
 * 必要ないときはパラメータに含めない方がよさそう
* bodyパラメータなら空文字指定でもOK


59
60
61
62
63
64
65
# File 'lib/google/calendar.rb', line 59

def execute(api_method, parameters={}, body="")
  options = { api_method:  api_method,
              parameters:  parameters,
              body:        body,
              headers: { 'Content-Type' => 'application/json' } }
  client.execute!(options)
end

#secret_keyObject



38
39
40
# File 'lib/google/calendar.rb', line 38

def secret_key
  @secret_key ||= Google::SecretKey.new({ path: nil, password: nil })
end

#service_accountObject



42
43
44
# File 'lib/google/calendar.rb', line 42

def 
  Google::ServiceAccount.new(client_id)
end

#signing_keyObject



46
47
48
# File 'lib/google/calendar.rb', line 46

def signing_key
  Google::APIClient::KeyUtils.load_from_pkcs12(secret_key.path, secret_key.password)
end