abstract_google_client 0.0.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +22 -0
- data/.rubocop.yml +53 -0
- data/.ruby-gemset +1 -0
- data/.ruby-version +1 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +29 -0
- data/Rakefile +1 -0
- data/google_client.gemspec +26 -0
- data/lib/google_client.rb +10 -0
- data/lib/google_client/builder.rb +64 -0
- data/lib/google_client/configuration.rb +95 -0
- data/lib/google_client/version.rb +3 -0
- metadata +114 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 3d255af04fd09b960dd5936c80bc84f1117c3aaa
|
4
|
+
data.tar.gz: 39862b5564b035fcb4ecb6684d6b1712e36276dd
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: d3881177c42c356999dc19b1118d7f09a56d2084e0ca2f5ba42177cec65b5da1b4b80cbce4d8fca0887c5335185195ecb23a555fb3773dc5c1d297bd83e10ee3
|
7
|
+
data.tar.gz: d40ee1f1ed15a31b5b0be3d68a624059cdefe1fb4ba31eb97e81f9fd57cc2d9a2cb53cec42e0b0443980985a25b730643ccde602c3fd101d706b9acce591262c
|
data/.gitignore
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
*.gem
|
2
|
+
*.rbc
|
3
|
+
.bundle
|
4
|
+
.config
|
5
|
+
.yardoc
|
6
|
+
Gemfile.lock
|
7
|
+
InstalledFiles
|
8
|
+
_yardoc
|
9
|
+
coverage
|
10
|
+
doc/
|
11
|
+
lib/bundler/man
|
12
|
+
pkg
|
13
|
+
rdoc
|
14
|
+
spec/reports
|
15
|
+
test/tmp
|
16
|
+
test/version_tmp
|
17
|
+
tmp
|
18
|
+
*.bundle
|
19
|
+
*.so
|
20
|
+
*.o
|
21
|
+
*.a
|
22
|
+
mkmf.log
|
data/.rubocop.yml
ADDED
@@ -0,0 +1,53 @@
|
|
1
|
+
AllCops:
|
2
|
+
Exclude:
|
3
|
+
- 'google_client.gemspec'
|
4
|
+
|
5
|
+
Metrics/BlockNesting:
|
6
|
+
Max: 2
|
7
|
+
|
8
|
+
Metrics/LineLength:
|
9
|
+
AllowURI: true
|
10
|
+
Enabled: false
|
11
|
+
|
12
|
+
Metrics/MethodLength:
|
13
|
+
CountComments: false
|
14
|
+
Max: 15
|
15
|
+
|
16
|
+
Metrics/ParameterLists:
|
17
|
+
Max: 4
|
18
|
+
CountKeywordArgs: true
|
19
|
+
|
20
|
+
Style/AccessModifierIndentation:
|
21
|
+
EnforcedStyle: outdent
|
22
|
+
|
23
|
+
Style/CollectionMethods:
|
24
|
+
PreferredMethods:
|
25
|
+
map: 'collect'
|
26
|
+
reduce: 'inject'
|
27
|
+
find: 'detect'
|
28
|
+
find_all: 'select'
|
29
|
+
|
30
|
+
Style/Documentation:
|
31
|
+
Enabled: false
|
32
|
+
|
33
|
+
Style/DotPosition:
|
34
|
+
EnforcedStyle: trailing
|
35
|
+
|
36
|
+
Style/DoubleNegation:
|
37
|
+
Enabled: false
|
38
|
+
|
39
|
+
Style/EachWithObject:
|
40
|
+
Enabled: false
|
41
|
+
|
42
|
+
Style/Encoding:
|
43
|
+
Enabled: false
|
44
|
+
|
45
|
+
Style/Lambda:
|
46
|
+
Enabled: false
|
47
|
+
|
48
|
+
Style/TrailingComma:
|
49
|
+
EnforcedStyleForMultiline: 'comma'
|
50
|
+
|
51
|
+
# Avoid complex methods.
|
52
|
+
Metrics/CyclomaticComplexity:
|
53
|
+
Max: 7
|
data/.ruby-gemset
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
google_client
|
data/.ruby-version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
ruby-2.1.5
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2015 Alexander Bradner
|
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,29 @@
|
|
1
|
+
# GoogleClient
|
2
|
+
|
3
|
+
TODO: Write a gem description
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'google_client'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install google_client
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
TODO: Write usage instructions here
|
22
|
+
|
23
|
+
## Contributing
|
24
|
+
|
25
|
+
1. Fork it ( https://github.com/[my-github-username]/google_client/fork )
|
26
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
27
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
28
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
29
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'bundler/gem_tasks'
|
@@ -0,0 +1,26 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'google_client/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = 'abstract_google_client'
|
8
|
+
spec.version = GoogleClient::VERSION
|
9
|
+
spec.authors = ['Alexander Bradner']
|
10
|
+
spec.email = ['alex@bradner.net']
|
11
|
+
spec.summary = 'Handles a persistent Google::ApiClient in rails'
|
12
|
+
spec.description = 'This is totally a work in progress. Ask me if you want to use it so I can explain the traps'
|
13
|
+
spec.homepage = 'http://github.com/abradner/google_client'
|
14
|
+
spec.license = 'MIT'
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0")
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ['lib']
|
20
|
+
|
21
|
+
spec.add_development_dependency 'bundler', '~> 1.6'
|
22
|
+
spec.add_development_dependency 'rake'
|
23
|
+
|
24
|
+
spec.add_runtime_dependency 'google-api-client', '~> 0.8'
|
25
|
+
spec.add_development_dependency 'rubocop'
|
26
|
+
end
|
@@ -0,0 +1,64 @@
|
|
1
|
+
module GoogleClient
|
2
|
+
class Builder
|
3
|
+
require 'oauth2/access_token'
|
4
|
+
# require 'signet/errors'
|
5
|
+
|
6
|
+
attr_reader :service
|
7
|
+
|
8
|
+
def initialize(api, version, retries = 0)
|
9
|
+
@client = GoogleClient.client
|
10
|
+
@auth = GoogleClient.authorization.dup
|
11
|
+
@service = @client.discovered_api(api, version) #
|
12
|
+
@client.retries = retries
|
13
|
+
end
|
14
|
+
|
15
|
+
def execute(credential, method, parameters = {})
|
16
|
+
preflight(credential)
|
17
|
+
@client.execute(
|
18
|
+
authorization: @auth,
|
19
|
+
api_method: method,
|
20
|
+
parameters: parameters,
|
21
|
+
)
|
22
|
+
end
|
23
|
+
|
24
|
+
def execute_batch(credential, batch)
|
25
|
+
# preflight(credential)
|
26
|
+
@auth.update_token!(credential)
|
27
|
+
@client.execute(batch, authorization: @auth)
|
28
|
+
end
|
29
|
+
|
30
|
+
private
|
31
|
+
|
32
|
+
def preflight(credential)
|
33
|
+
if credential.is_a?(OAuth2::AccessToken)
|
34
|
+
hash = {
|
35
|
+
access_token: credential.token,
|
36
|
+
refresh_token: credential.refresh_token,
|
37
|
+
expires_in: credential.expires_in,
|
38
|
+
expires: credential.expires_in,
|
39
|
+
}
|
40
|
+
@auth.update_token!(hash) #
|
41
|
+
else
|
42
|
+
@auth.update_token!(credential.to_auth_hash) #
|
43
|
+
return unless credential.expires_at < 5.minutes.from_now # Update the access token if it has potentially expired
|
44
|
+
fetch_and_update_access_token!(credential)
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
def fetch_and_update_access_token!(credential)
|
49
|
+
@auth.fetch_access_token!
|
50
|
+
credential.update_access_token!(
|
51
|
+
token: @auth.access_token,
|
52
|
+
expires_at: (Time.now.utc + @auth.expires_in),
|
53
|
+
)
|
54
|
+
rescue Signet::AuthorizationError => _e
|
55
|
+
# If a refresh token is invalid for some reason we'll end up here.
|
56
|
+
# This can also happen if *our* client token/secret is bad, so that needs to be dealt with too
|
57
|
+
|
58
|
+
# Todo
|
59
|
+
# if it's because the refresh token is bad, get the user to log out and in again.
|
60
|
+
puts 'Auth attempt failed (either client ID/Secret are missing or user has a bad refresh token)'
|
61
|
+
raise
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
@@ -0,0 +1,95 @@
|
|
1
|
+
module GoogleClient
|
2
|
+
# Exception to handle a missing initializer
|
3
|
+
# class MissingConfiguration < StandardError
|
4
|
+
# def initialize
|
5
|
+
# super('Configuration for authinator missing. Do you have an Authinator initializer?')
|
6
|
+
# end
|
7
|
+
# end
|
8
|
+
|
9
|
+
# Module level methods
|
10
|
+
class << self
|
11
|
+
attr_accessor :configuration
|
12
|
+
end
|
13
|
+
|
14
|
+
def self.configure
|
15
|
+
self.configuration ||= Configuration.new
|
16
|
+
yield(configuration)
|
17
|
+
end
|
18
|
+
|
19
|
+
def self.client
|
20
|
+
configuration.client
|
21
|
+
end
|
22
|
+
|
23
|
+
def self.authorization
|
24
|
+
configuration.authorization
|
25
|
+
end
|
26
|
+
|
27
|
+
# def self.configuration
|
28
|
+
# @config || (fail MissingConfiguration.new)
|
29
|
+
# end
|
30
|
+
|
31
|
+
# Configuration class
|
32
|
+
class Configuration
|
33
|
+
attr_accessor :client_id,
|
34
|
+
:client_secret,
|
35
|
+
:redirect_uris,
|
36
|
+
:javascript_origins,
|
37
|
+
:auth_uri,
|
38
|
+
:token_uri,
|
39
|
+
:app_name,
|
40
|
+
:app_version
|
41
|
+
|
42
|
+
def initialize
|
43
|
+
@redirect_uris = []
|
44
|
+
@javascript_origins = []
|
45
|
+
@auth_uri = 'https://accounts.google.com/o/oauth2/auth'
|
46
|
+
@token_uri = 'https://accounts.google.com/o/oauth2/token'
|
47
|
+
end
|
48
|
+
|
49
|
+
def client
|
50
|
+
return @client if @client
|
51
|
+
errors = []
|
52
|
+
|
53
|
+
# symbols = %i(app_name app_version)
|
54
|
+
# symbols.each do |sym|
|
55
|
+
# errors << errors_for(sym)
|
56
|
+
# end
|
57
|
+
errors << 'app_name not set' unless @app_name.present?
|
58
|
+
errors << 'app_version not set' unless @app_version.present?
|
59
|
+
|
60
|
+
fail "Couldn't create the client:\n" << errors.to_sentence unless errors.blank?
|
61
|
+
|
62
|
+
@client = Google::APIClient.new(application_name: @app_name,
|
63
|
+
application_version: @app_version)
|
64
|
+
end
|
65
|
+
|
66
|
+
def authorization
|
67
|
+
errors = []
|
68
|
+
|
69
|
+
errors << 'app_name not set' unless @app_name.present?
|
70
|
+
errors << 'app_version not set' unless @app_version.present?
|
71
|
+
|
72
|
+
fail "Couldn't create the authorizationt:\n" << errors.to_sentence unless errors.blank?
|
73
|
+
Google::APIClient::ClientSecrets.new(to_hash).to_authorization
|
74
|
+
end
|
75
|
+
|
76
|
+
def to_hash
|
77
|
+
{ web: {
|
78
|
+
client_id: @client_id,
|
79
|
+
client_secret: @client_secret,
|
80
|
+
redirect_uris: @redirect_uris,
|
81
|
+
javascript_origins: @javascript_origins,
|
82
|
+
auth_uri: @auth_uri,
|
83
|
+
token_uri: @token_uri,
|
84
|
+
} }.with_indifferent_access
|
85
|
+
end
|
86
|
+
|
87
|
+
private
|
88
|
+
|
89
|
+
# def errors_for(sym)
|
90
|
+
# send(sym)
|
91
|
+
# rescue NoMethodError
|
92
|
+
# "#{sym} not set"
|
93
|
+
# end
|
94
|
+
end
|
95
|
+
end
|
metadata
ADDED
@@ -0,0 +1,114 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: abstract_google_client
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.5
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Alexander Bradner
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-01-14 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.6'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.6'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
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: google-api-client
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0.8'
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0.8'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rubocop
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
description: This is totally a work in progress. Ask me if you want to use it so I
|
70
|
+
can explain the traps
|
71
|
+
email:
|
72
|
+
- alex@bradner.net
|
73
|
+
executables: []
|
74
|
+
extensions: []
|
75
|
+
extra_rdoc_files: []
|
76
|
+
files:
|
77
|
+
- ".gitignore"
|
78
|
+
- ".rubocop.yml"
|
79
|
+
- ".ruby-gemset"
|
80
|
+
- ".ruby-version"
|
81
|
+
- Gemfile
|
82
|
+
- LICENSE.txt
|
83
|
+
- README.md
|
84
|
+
- Rakefile
|
85
|
+
- google_client.gemspec
|
86
|
+
- lib/google_client.rb
|
87
|
+
- lib/google_client/builder.rb
|
88
|
+
- lib/google_client/configuration.rb
|
89
|
+
- lib/google_client/version.rb
|
90
|
+
homepage: http://github.com/abradner/google_client
|
91
|
+
licenses:
|
92
|
+
- MIT
|
93
|
+
metadata: {}
|
94
|
+
post_install_message:
|
95
|
+
rdoc_options: []
|
96
|
+
require_paths:
|
97
|
+
- lib
|
98
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
99
|
+
requirements:
|
100
|
+
- - ">="
|
101
|
+
- !ruby/object:Gem::Version
|
102
|
+
version: '0'
|
103
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
104
|
+
requirements:
|
105
|
+
- - ">="
|
106
|
+
- !ruby/object:Gem::Version
|
107
|
+
version: '0'
|
108
|
+
requirements: []
|
109
|
+
rubyforge_project:
|
110
|
+
rubygems_version: 2.4.3
|
111
|
+
signing_key:
|
112
|
+
specification_version: 4
|
113
|
+
summary: Handles a persistent Google::ApiClient in rails
|
114
|
+
test_files: []
|