google-apis 0.1.0

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: 73ecd62e8a070f3007d0a70e89fba19c10fc5add
4
+ data.tar.gz: 07cdd1042f1446a7f7420269ac6b285d3188b7e3
5
+ SHA512:
6
+ metadata.gz: 2fdd5bc568744355390ba7fa05aa1045aae105a8d05d6c2283234a6f73d471338654e7e3da8bd95eb55555d1fff085ac56e29e78fdf67bbd4d873c692fb68d63
7
+ data.tar.gz: ddb5c0ac76e8232a72cfbc10113e8355995115516ba613c1c6c711778bac20229ef5b987f0a18edbd135713b6659d42eac8dc8f4c3bc8deb78e453b78d624e95
data/.gitignore ADDED
@@ -0,0 +1,11 @@
1
+ .DS_Store
2
+ .bundle
3
+ .yardoc
4
+ .rvmrc
5
+ Gemfile.lock
6
+ doc
7
+ pkg
8
+ *.log
9
+ *.p12
10
+ *.yml
11
+ test/coverage
data/CHANGELOG.rdoc ADDED
@@ -0,0 +1,5 @@
1
+ = Google APIs CHANGELOG
2
+
3
+ == Version 0.1.0 (February 10, 2015)
4
+
5
+ * Initial release
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source "https://rubygems.org"
2
+
3
+ gemspec
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2015 Paul Engel
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,155 @@
1
+ ## Google APIs
2
+
3
+ A thin layer on top of [Google::APIClient](https://github.com/google/google-api-ruby-client) for a more intuitive way of working (e.g. with BigQuery)
4
+
5
+ ### Installation
6
+
7
+ Add `GoogleApis` in your `Gemfile`:
8
+
9
+ gem "google-apis"
10
+
11
+ Run the following in your console to install with Bundler:
12
+
13
+ $ bundle install
14
+
15
+ ### Usage
16
+
17
+ The easiest setup is to define an application-wide Google API connection followed by instantiating an API instance.
18
+
19
+ Make sure you have created a Client ID (application type "Service account") at your projects API credentials page (https://console.developers.google.com/project/your_project_id/apiui/credential).
20
+
21
+ Do not forget to download the private key by generating a P12 key file.
22
+
23
+ ```ruby
24
+ [1] pry(main)> GoogleApis.connect :email_address => "lorem@developer.gserviceaccount.com", :private_key => "/path/to/private/key.p12"
25
+ => true
26
+ [2] pry(main)> bq = Google::BigQuery.new :projectId => "your_project_id", :datasetId => "your_dataset_id"
27
+ => #<Google::BigQuery:0x007fc5c68647a8 v2:[datasets,jobs,projects,tabledata,tables] {projectId:"your_project_id",datasetId:"your_dataset_id"}>
28
+ [3] pry(main)> bq.tables.list
29
+ => {"kind"=>"bigquery#tableList",
30
+ "etag"=>"\"Fo0B4r/LoR3M-1pSuM-D0loR-s1T-AM3t\"",
31
+ "tables"=>
32
+ [{"kind"=>"bigquery#table",
33
+ "id"=>"your_project_id:your_dataset_id.awesome_table_19820801",
34
+ "tableReference"=>{"projectId"=>"your_project_id", "datasetId"=>"your_dataset_id", "tableId"=>"awesome_table_19820801"},
35
+ "type"=>"TABLE"}],
36
+ "totalItems"=>1}
37
+ [4] pry(main)> bq.jobs.query :query => "SELECT * FROM [your_dataset_id.awesome_table_19820801] LIMIT 5"
38
+ => {"kind"=>"bigquery#queryResponse",
39
+ "schema"=>
40
+ {"fields"=>
41
+ [{"name"=>"awesome_column1", "type"=>"STRING", "mode"=>"NULLABLE"},
42
+ {"name"=>"awesome_column2", "type"=>"STRING", "mode"=>"NULLABLE"},
43
+ {"name"=>"awesome_column3", "type"=>"STRING", "mode"=>"NULLABLE"},
44
+ ...
45
+ ```
46
+
47
+ #### Easy API discovery info
48
+
49
+ GoogleApis quickly displays the resources and methods a certain API provides.
50
+
51
+ ```ruby
52
+ [5] pry(main)> bq
53
+ => #<Google::BigQuery:0x007fc5c68647a8 v2:[datasets,jobs,projects,tabledata,tables] {projectId:"your_project_id",datasetId:"your_dataset_id"}>
54
+ [6] pry(main)> bq.jobs
55
+ => #<Google::BigQuery::Resource:0x007f8c09c60c68 v2:jobs:[get,getQueryResults,insert,list,query]>
56
+ [7] pry(main)> bq.jobs[:query]
57
+ => {"id"=>"bigquery.jobs.query",
58
+ "path"=>"projects/{projectId}/queries",
59
+ "httpMethod"=>"POST",
60
+ "description"=>"Runs a BigQuery SQL query synchronously and returns query results if the query completes within a specified timeout.",
61
+ "parameters"=>{"projectId"=>{"type"=>"string", "description"=>"Project ID of the project billed for the query", "required"=>true, "location"=>"path"}},
62
+ "parameterOrder"=>["projectId"],
63
+ "request"=>{"$ref"=>"QueryRequest"},
64
+ "response"=>{"$ref"=>"QueryResponse"},
65
+ "scopes"=>["https://www.googleapis.com/auth/bigquery", "https://www.googleapis.com/auth/cloud-platform"]}
66
+ ```
67
+
68
+ #### Application-wide API connections (e.g. Google::BigQuery.connection)
69
+
70
+ You can also configure an application-wide API connection. Let's say you also stored the connection configuration in `config/bigquery.yml`:
71
+
72
+ ```yaml
73
+ ---
74
+ email_address: lorem@developer.gserviceaccount.com
75
+ private_key: "/path/to/private/key.p12"
76
+ projectId: your_project_id
77
+ datasetId: your_dataset_id
78
+ ```
79
+
80
+ ```ruby
81
+ [1] pry(main)> require "yaml"
82
+ => true
83
+ [2] pry(main)> Google::BigQuery.connect YAML.load_file("config/bigquery.yml")
84
+ => #<Google::BigQuery:0x007fdc1c6823b0 v2:[datasets,jobs,projects,tabledata,tables] {projectId:"your_project_id",datasetId:"your_dataset_id"}>
85
+ [3] pry(main)> Google::BigQuery.connection.jobs
86
+ => #<Google::BigQuery::Resource:0x007fdc1e94d5c0 v2:jobs:[get,getQueryResults,insert,list,query]>
87
+ [4] pry(main)> Google::BigQuery.connection.select_rows "SELECT * FROM [your_dataset_id.awesome_table_19820801] LIMIT 2"
88
+ => [["1982-08-01", "Paul is awesome", "Paul", "Engel", 19],
89
+ ["1982-08-01", "GoogleApis is cool", "Google", "Apis", 82],
90
+ ["1982-08-01", "Hello world!", "Foo", "Bar", 8],
91
+ ["1982-08-01", "Try out this gem :)", "It's", "very easy", 1]]
92
+ ```
93
+
94
+ ### Using the console
95
+
96
+ The GoogleApis repo is provided with `script/console` which you can use for development / testing purposes.
97
+
98
+ Run the following command in your console:
99
+
100
+ ```ruby
101
+ $ script/console
102
+ Loading Google APIs development environment (0.1.0)
103
+ [1] pry(main)> GoogleApis.connect :email_address => "lorem@developer.gserviceaccount.com", :private_key => "/path/to/private/key.p12"
104
+ => true
105
+ [2] pry(main)> bq = Google::BigQuery.new :projectId => "your_project_id", :datasetId => "your_dataset_id"
106
+ => #<Google::BigQuery:0x007f8c09a05338 v2:[datasets,jobs,projects,tabledata,tables] {projectId:"your_project_id",datasetId:"your_dataset_id"}>
107
+ ```
108
+
109
+ You can also define `script/config.yml` containing the connection config:
110
+
111
+ ```yaml
112
+ ---
113
+ email_address: lorem@developer.gserviceaccount.com
114
+ private_key: "/path/to/private/key.p12"
115
+ ```
116
+
117
+ And immediately start instantiating a Google API:
118
+
119
+ ```ruby
120
+ $ script/console
121
+ Loading Google APIs development environment (0.1.0)
122
+ [1] pry(main)> bq = Google::BigQuery.new :projectId => "your_project_id", :datasetId => "your_dataset_id"
123
+ => #<Google::BigQuery:0x007fa6c9cc3450 v2:[datasets,jobs,projects,tabledata,tables] {projectId:"your_project_id",datasetId:"your_dataset_id"}>
124
+ ```
125
+
126
+ ### Testing
127
+
128
+ Run the following command for testing:
129
+
130
+ $ rake
131
+
132
+ You can also run a single test file:
133
+
134
+ $ ruby test/unit/test_google_apis.rb
135
+
136
+ ### Contributing
137
+
138
+ Please feel free to fork this repository and send in pull requests to help improve GoogleApis ;)
139
+
140
+ ### TODO
141
+
142
+ * Add more Google API definitions (see [google-apis/lib/google_apis/api](https://github.com/archan937/google-apis/tree/master/lib/google_apis/api))
143
+ * Add more tests
144
+
145
+ ### License
146
+
147
+ Copyright (c) 2015 Paul Engel, released under the MIT License
148
+
149
+ http://github.com/archan937 – http://twitter.com/archan937 – http://gettopup.com – pm_engel@icloud.com
150
+
151
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
152
+
153
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
154
+
155
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ #!/usr/bin/env rake
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rake/testtask"
5
+
6
+ task :default => :test
7
+
8
+ Rake::TestTask.new do |test|
9
+ test.pattern = "test/**/test_*.rb"
10
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
@@ -0,0 +1,26 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path("../lib/google_apis/version", __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.author = "Paul Engel"
6
+ gem.email = "pm_engel@icloud.com"
7
+ gem.summary = %q{A thin layer on top of Google::APIClient (google-api-client) for a more intuitive way of working (e.g. with BigQuery)}
8
+ gem.description = %q{A thin layer on top of Google::APIClient (google-api-client) for a more intuitive way of working (e.g. with BigQuery)}
9
+ gem.homepage = "https://github.com/archan937/google-apis"
10
+
11
+ gem.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
12
+ gem.files = `git ls-files`.split("\n")
13
+ gem.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
14
+ gem.name = "google-apis"
15
+ gem.require_paths = ["lib"]
16
+ gem.version = GoogleApis::VERSION
17
+
18
+ gem.add_dependency "google-api-client", "~> 0.8.2"
19
+
20
+ gem.add_development_dependency "rake"
21
+ gem.add_development_dependency "yard"
22
+ gem.add_development_dependency "pry"
23
+ gem.add_development_dependency "simplecov"
24
+ gem.add_development_dependency "minitest"
25
+ gem.add_development_dependency "mocha"
26
+ end
@@ -0,0 +1 @@
1
+ require "google_apis"
@@ -0,0 +1,20 @@
1
+ require "google/api_client"
2
+
3
+ require "google_apis/core_ext"
4
+ require "google_apis/connection"
5
+ require "google_apis/api"
6
+ require "google_apis/version"
7
+
8
+ module GoogleApis
9
+
10
+ class Error < StandardError; end
11
+
12
+ def self.connect(options)
13
+ @connection = Connection.new options
14
+ end
15
+
16
+ def self.connection
17
+ @connection
18
+ end
19
+
20
+ end
@@ -0,0 +1,4 @@
1
+ require "google_apis/api/base"
2
+ require "google_apis/api/big_query"
3
+ require "google_apis/api/drive"
4
+ require "google_apis/api/plus"
@@ -0,0 +1,23 @@
1
+ require "google_apis/api/base/class_methods"
2
+ require "google_apis/api/base/instance_methods"
3
+ require "google_apis/api/base/resource"
4
+
5
+ module Google
6
+ end
7
+
8
+ module GoogleApis
9
+ class Api
10
+ module Base
11
+
12
+ def self.extended(base)
13
+ name = base.to_s.demodulize
14
+ Google.const_set name, base
15
+
16
+ base.instance_variable_set :@name, "Google::#{name}"
17
+ base.extend ClassMethods
18
+ base.send :include, InstanceMethods
19
+ end
20
+
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,53 @@
1
+ module GoogleApis
2
+ class Api
3
+ module Base
4
+ module ClassMethods
5
+
6
+ def api(value = nil)
7
+ if value
8
+ @api = value
9
+ else
10
+ @api
11
+ end
12
+ end
13
+
14
+ def version(value = nil)
15
+ if value
16
+ @version = value
17
+ else
18
+ @version || 1
19
+ end
20
+ end
21
+
22
+ def auth_scope(value = nil)
23
+ if value
24
+ @auth_scope = value
25
+ else
26
+ @auth_scope
27
+ end
28
+ end
29
+
30
+ def connect(options)
31
+ @connection = new(options)
32
+ end
33
+
34
+ def connection
35
+ @connection
36
+ end
37
+
38
+ def name
39
+ @name
40
+ end
41
+
42
+ def inspect
43
+ name
44
+ end
45
+
46
+ def to_s
47
+ name
48
+ end
49
+
50
+ end
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,46 @@
1
+ module GoogleApis
2
+ class Api
3
+ module Base
4
+ module InstanceMethods
5
+
6
+ attr_accessor :connection, :discovered_api
7
+
8
+ def initialize(options = {})
9
+ options = options.symbolize_keys
10
+
11
+ config, default_params = options.partition{|(k, v)| [:email_address, :private_key].include?(k)}.collect{|x| Hash[x] unless x.empty?}
12
+ @connection = config ? GoogleApis::Connection.new(config) : GoogleApis.connection
13
+ raise Error, "Please ensure a Google API connection" unless @connection
14
+
15
+ @discovered_api = connection.discover_api self.class.api, self.class.version
16
+ @default_params = default_params || {}
17
+ end
18
+
19
+ def execute(api_method, *params)
20
+ params[0] = (params[0] || {}).symbolize_keys
21
+ params[0].reverse_merge!(@default_params)
22
+ connection.execute self.class, api_method, *params
23
+ end
24
+
25
+ def inspect
26
+ "#<#{self.class}:#{object_hexid} #{discovered_api.version}:[#{discovered_api.discovered_resources.collect(&:name).sort.join(",")}] {#{@default_params.collect{|k, v| "#{k}:#{v.inspect}"}.join(",")}}>"
27
+ end
28
+
29
+ def method_missing(name, *args)
30
+ if resource = find(name)
31
+ Resource.new self, resource
32
+ else
33
+ super
34
+ end
35
+ end
36
+
37
+ private
38
+
39
+ def find(name)
40
+ discovered_api.discovered_resources.detect{|x| x.name == name.to_s}
41
+ end
42
+
43
+ end
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,40 @@
1
+ module GoogleApis
2
+ class Api
3
+ module Base
4
+ class Resource
5
+
6
+ attr_accessor :api, :discovered_resource
7
+
8
+ def initialize(api, discovered_resource)
9
+ @api = api
10
+ @discovered_resource = discovered_resource
11
+ end
12
+
13
+ def [](name)
14
+ if method = find(name)
15
+ method.discovery_document
16
+ end
17
+ end
18
+
19
+ def inspect
20
+ "#<#{api.class}::Resource:#{object_hexid} #{api.discovered_api.version}:#{discovered_resource.name}:[#{discovered_resource.discovered_methods.collect(&:name).sort.join(",")}]>"
21
+ end
22
+
23
+ def method_missing(name, *args)
24
+ if method = find(name)
25
+ api.execute method, *args
26
+ else
27
+ super
28
+ end
29
+ end
30
+
31
+ private
32
+
33
+ def find(name)
34
+ discovered_resource.discovered_methods.detect{|x| x.name == name.to_s}
35
+ end
36
+
37
+ end
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,73 @@
1
+ module GoogleApis
2
+ class Api
3
+ class BigQuery
4
+ extend Base
5
+
6
+ api "bigquery"
7
+ version 2
8
+ auth_scope "https://www.googleapis.com/auth/bigquery"
9
+
10
+ def select_rows(statement)
11
+ result = jobs.query :query => statement
12
+ types = result["schema"]["fields"].collect{|x| x["type"].downcase.to_sym}
13
+
14
+ (result["rows"] || []).collect do |row|
15
+ row["f"].inject([]) do |values, x|
16
+ values << parse_value(types[values.size], x["v"])
17
+ end
18
+ end
19
+ end
20
+
21
+ def select_values(query)
22
+ select_rows(query).collect{|x| x[0]}
23
+ end
24
+
25
+ def select_value(query)
26
+ row = select_rows(query)[0]
27
+ row[0] if row
28
+ end
29
+
30
+ private
31
+
32
+ def parse_value(type, value)
33
+ unless value == "NULL"
34
+ case type
35
+ when :string
36
+ parse_string_value value
37
+ when :integer
38
+ parse_integer_value value
39
+ when :float
40
+ parse_float_value value
41
+ when :boolean
42
+ parse_boolean_value value
43
+ when :timestamp
44
+ parse_timestamp_value value
45
+ else
46
+ raise NotImplementedError, "Cannot parse value of type #{type.inspect}"
47
+ end
48
+ end
49
+ end
50
+
51
+ def parse_string_value(value)
52
+ value
53
+ end
54
+
55
+ def parse_integer_value(value)
56
+ value.to_i
57
+ end
58
+
59
+ def parse_float_value(value)
60
+ value.to_f
61
+ end
62
+
63
+ def parse_boolean_value(value)
64
+ (value == "true") || (value == "1")
65
+ end
66
+
67
+ def parse_timestamp_value(value)
68
+ Time.at value.to_f.to_i
69
+ end
70
+
71
+ end
72
+ end
73
+ end
@@ -0,0 +1,12 @@
1
+ module GoogleApis
2
+ class Api
3
+ class Drive
4
+ extend Base
5
+
6
+ api "drive"
7
+ version 2
8
+ auth_scope "https://www.googleapis.com/auth/drive"
9
+
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,11 @@
1
+ module GoogleApis
2
+ class Api
3
+ class Plus
4
+ extend Base
5
+
6
+ api "plus"
7
+ auth_scope "https://www.googleapis.com/auth/plus.me"
8
+
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,66 @@
1
+ module GoogleApis
2
+ class Connection
3
+
4
+ def initialize(options)
5
+ options = options.symbolize_keys
6
+
7
+ @client = Google::APIClient.new(
8
+ :application_name => "rubygem:google-apis",
9
+ :application_version => GoogleApis::VERSION
10
+ )
11
+
12
+ key = Google::APIClient::PKCS12.load_key(
13
+ File.open(options[:private_key], "rb"),
14
+ options[:passphrase] || "notasecret"
15
+ )
16
+
17
+ @asserter = Google::APIClient::JWTAsserter.new(
18
+ options[:email_address],
19
+ nil,
20
+ key
21
+ )
22
+ end
23
+
24
+ def discover_api(name, version = 1)
25
+ @client.discovered_api name, "v#{version}"
26
+ end
27
+
28
+ def execute(api, api_method, *params)
29
+ authenticate!(api)
30
+
31
+ nested, top_level = params
32
+
33
+ if nested
34
+ parameter_keys = (api_method.discovery_document["parameters"] || {}).keys.collect(&:to_sym)
35
+ parameters, body_object = nested.partition{|k, v| parameter_keys.include?(k)}.collect{|x| Hash[x] unless x.empty?}
36
+ end
37
+
38
+ options = {:api_method => api_method}
39
+ options[:parameters] = parameters if parameters
40
+ options[:body_object] = body_object if body_object
41
+ options.merge!(top_level) if top_level
42
+
43
+ parse! @client.execute(options)
44
+ end
45
+
46
+ private
47
+
48
+ def authenticate!(api)
49
+ if !@client.authorization.access_token || @client.authorization.expired?
50
+ @asserter.scope = api.auth_scope
51
+ @client.authorization = @asserter.authorize
52
+ end
53
+ end
54
+
55
+ def parse!(response)
56
+ unless response.body.to_s.empty?
57
+ JSON.parse(response.body).tap do |data|
58
+ if error = data["error"]
59
+ raise Error, "#{error["code"]} #{error["message"]}"
60
+ end
61
+ end
62
+ end
63
+ end
64
+
65
+ end
66
+ end
@@ -0,0 +1,2 @@
1
+ require "google_apis/core_ext/kernel"
2
+ require "google_apis/core_ext/hash"
@@ -0,0 +1,15 @@
1
+ unless Hash.method_defined?(:symbolize_keys)
2
+ class Hash
3
+ def symbolize_keys
4
+ inject({}){|h, (k, v)| h[k.to_sym] = v; h}
5
+ end
6
+ end
7
+ end
8
+
9
+ unless Hash.method_defined?(:reverse_merge!)
10
+ class Hash
11
+ def reverse_merge!(other)
12
+ replace other.merge(self)
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,7 @@
1
+ module Kernel
2
+
3
+ def object_hexid
4
+ "0x" + (object_id << 1).to_s(16).rjust(14, "0")
5
+ end
6
+
7
+ end
@@ -0,0 +1,7 @@
1
+ module GoogleApis
2
+ MAJOR = 0
3
+ MINOR = 1
4
+ TINY = 0
5
+
6
+ VERSION = [MAJOR, MINOR, TINY].join(".")
7
+ end
data/script/console ADDED
@@ -0,0 +1,12 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler"
4
+ Bundler.require :default, :development
5
+
6
+ if File.exists?(config = "script/config.yml")
7
+ require "yaml"
8
+ GoogleApis.connect YAML.load_file(config)
9
+ end
10
+
11
+ puts "Loading Google APIs development environment (#{GoogleApis::VERSION})"
12
+ Pry.start
@@ -0,0 +1,11 @@
1
+ require_relative "test_helper/coverage"
2
+
3
+ require "minitest/autorun"
4
+ require "mocha/setup"
5
+
6
+ require "bundler"
7
+ Bundler.require :default, :development
8
+
9
+ def project_file(path)
10
+ File.expand_path "../../#{path}", __FILE__
11
+ end
@@ -0,0 +1,11 @@
1
+ if Dir.pwd == File.expand_path("../../..", __FILE__)
2
+
3
+ require "simplecov"
4
+
5
+ SimpleCov.coverage_dir "test/coverage"
6
+ SimpleCov.start do
7
+ add_group "Library", "lib"
8
+ add_group "Tests", "test"
9
+ end
10
+
11
+ end
@@ -0,0 +1,73 @@
1
+ require_relative "../../../test_helper"
2
+
3
+ module Unit
4
+ module Api
5
+ module Base
6
+ class TestClassMethods < Minitest::Test
7
+
8
+ class ClassApi
9
+ extend GoogleApis::Api::Base::ClassMethods
10
+ end
11
+
12
+ describe GoogleApis::Api::Base::ClassMethods do
13
+ describe ".api" do
14
+ describe "as reader" do
15
+ it "returns class instance variable @api" do
16
+ ClassApi.instance_variable_set :@api, "< api >"
17
+ assert_equal "< api >", ClassApi.api
18
+ end
19
+ end
20
+ describe "as writer" do
21
+ it "assigns class instance variable @api" do
22
+ ClassApi.instance_variable_set :@api, "< old api >"
23
+ ClassApi.api "< new api >"
24
+ assert_equal "< new api >", ClassApi.instance_variable_get(:@api)
25
+ end
26
+ end
27
+ end
28
+
29
+ describe ".version" do
30
+ describe "as reader" do
31
+ describe "when not set" do
32
+ it "returns 1" do
33
+ ClassApi.instance_variable_set :@version, nil
34
+ assert_equal 1, ClassApi.version
35
+ end
36
+ end
37
+ describe "when set" do
38
+ it "returns class instance variable @version" do
39
+ ClassApi.instance_variable_set :@version, "< version >"
40
+ assert_equal "< version >", ClassApi.version
41
+ end
42
+ end
43
+ end
44
+ describe "as writer" do
45
+ it "assigns class instance variable @version" do
46
+ ClassApi.instance_variable_set :@version, "< old version >"
47
+ ClassApi.version "< new version >"
48
+ assert_equal "< new version >", ClassApi.instance_variable_get(:@version)
49
+ end
50
+ end
51
+ end
52
+
53
+ describe ".auth_scope" do
54
+ describe "as reader" do
55
+ it "returns class instance variable @auth_scope" do
56
+ ClassApi.instance_variable_set :@auth_scope, "< auth_scope >"
57
+ assert_equal "< auth_scope >", ClassApi.auth_scope
58
+ end
59
+ end
60
+ describe "as writer" do
61
+ it "assigns class instance variable @auth_scope" do
62
+ ClassApi.instance_variable_set :@auth_scope, "< old auth_scope >"
63
+ ClassApi.auth_scope "< new auth_scope >"
64
+ assert_equal "< new auth_scope >", ClassApi.instance_variable_get(:@auth_scope)
65
+ end
66
+ end
67
+ end
68
+ end
69
+
70
+ end
71
+ end
72
+ end
73
+ end
@@ -0,0 +1,67 @@
1
+ require_relative "../../../test_helper"
2
+
3
+ module Unit
4
+ module Api
5
+ module Base
6
+ class TestInstanceMethods < Minitest::Test
7
+
8
+ class InstanceApi
9
+ extend GoogleApis::Api::Base
10
+ end
11
+
12
+ describe GoogleApis::Api::Base::InstanceMethods do
13
+ describe "#initialize" do
14
+ describe "when not having GoogleApis.connection defined" do
15
+ before do
16
+ GoogleApis.instance_variable_set :@connection, nil
17
+ end
18
+ describe "when passing connection options" do
19
+ it "instantiates its own connection" do
20
+ connection = "< connection >"
21
+ connection.expects(:discover_api)
22
+ options = {
23
+ :email_address => "< email_address >",
24
+ :private_key => "< private_key >"
25
+ }
26
+ GoogleApis::Connection.expects(:new).with(options).returns(connection)
27
+ assert_equal connection, InstanceApi.new(options).instance_variable_get(:@connection)
28
+ end
29
+ end
30
+ describe "when not passing connection options" do
31
+ it "raises an error" do
32
+ assert_raises GoogleApis::Error do
33
+ InstanceApi.new
34
+ end
35
+ end
36
+ end
37
+ end
38
+ describe "when not having GoogleApis.connection defined" do
39
+ before do
40
+ GoogleApis.instance_variable_set :@connection, "< google-apis connection >"
41
+ end
42
+ describe "when passing connection options" do
43
+ it "instantiates its own connection" do
44
+ connection = "< connection >"
45
+ connection.expects(:discover_api)
46
+ options = {
47
+ :email_address => "< email_address >",
48
+ :private_key => "< private_key >"
49
+ }
50
+ GoogleApis::Connection.expects(:new).with(options).returns(connection)
51
+ assert_equal connection, InstanceApi.new(options).instance_variable_get(:@connection)
52
+ end
53
+ end
54
+ describe "when not passing connection options" do
55
+ it "uses GoogleApis.connection for its connection" do
56
+ GoogleApis.connection.expects(:discover_api)
57
+ assert_equal "< google-apis connection >", InstanceApi.new.instance_variable_get(:@connection)
58
+ end
59
+ end
60
+ end
61
+ end
62
+ end
63
+
64
+ end
65
+ end
66
+ end
67
+ end
@@ -0,0 +1,48 @@
1
+ require_relative "../../test_helper"
2
+
3
+ module Unit
4
+ module Api
5
+ class TestBigQuery < Minitest::Test
6
+
7
+ class BaseApi
8
+ extend GoogleApis::Api::Base
9
+ def self.metaclass
10
+ class << self; self; end
11
+ end
12
+ end
13
+
14
+ describe GoogleApis::Api::Base do
15
+ describe ".extended" do
16
+ it "extends base with GoogleApis::Api::Base::ClassMethods" do
17
+ assert_equal true, BaseApi.metaclass.included_modules.include?(GoogleApis::Api::Base::ClassMethods)
18
+ end
19
+ it "includes base with GoogleApis::Api::Base::InstanceMethods" do
20
+ assert_equal true, BaseApi.included_modules.include?(GoogleApis::Api::Base::InstanceMethods)
21
+ end
22
+ it "registers base in the Google namespace" do
23
+ assert_equal true, Google.constants.include?(:BaseApi)
24
+ assert_equal BaseApi, Google::BaseApi
25
+ end
26
+ end
27
+ describe ".name" do
28
+ it "returns its actual name demodulized within the Google namespace" do
29
+ assert_equal "Google::BaseApi", BaseApi.name
30
+ end
31
+ end
32
+ describe ".inspect" do
33
+ it "returns its name" do
34
+ BaseApi.expects(:name).returns("< inspect >")
35
+ assert_equal "< inspect >", BaseApi.inspect
36
+ end
37
+ end
38
+ describe ".to_s" do
39
+ it "returns its name" do
40
+ BaseApi.expects(:name).returns("< to_s >")
41
+ assert_equal "< to_s >", BaseApi.to_s
42
+ end
43
+ end
44
+ end
45
+
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,29 @@
1
+ require_relative "../../test_helper"
2
+
3
+ module Unit
4
+ module Api
5
+ class TestBigQuery < Minitest::Test
6
+
7
+ describe GoogleApis::Api::BigQuery do
8
+ describe ".api" do
9
+ it "returns the default API name" do
10
+ assert_equal "bigquery", GoogleApis::Api::BigQuery.api
11
+ end
12
+ end
13
+
14
+ describe ".version" do
15
+ it "returns the default API version" do
16
+ assert_equal 2, GoogleApis::Api::BigQuery.version
17
+ end
18
+ end
19
+
20
+ describe ".auth_scope" do
21
+ it "returns the default API authentication scope" do
22
+ assert_equal "https://www.googleapis.com/auth/bigquery", GoogleApis::Api::BigQuery.auth_scope
23
+ end
24
+ end
25
+ end
26
+
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,34 @@
1
+ require_relative "../../test_helper"
2
+
3
+ module Unit
4
+ module CoreExt
5
+ class TestHash < Minitest::Test
6
+
7
+ describe Hash do
8
+ describe "#symbolize_keys" do
9
+ before do
10
+ @hash = {"a" => 1, "b" => 2, "c" => 3}
11
+ @result = @hash.symbolize_keys
12
+ end
13
+ it "returns new hash" do
14
+ assert_equal true, @result.is_a?(Hash)
15
+ assert_equal true, @hash.object_id != @result.object_id
16
+ end
17
+ it "returns a hash with symbols as keys" do
18
+ assert_equal [:a, :b, :c], @result.keys
19
+ end
20
+ end
21
+ describe "#reverse_merge!" do
22
+ before do
23
+ @hash = {"a" => 1, "b" => 2, "c" => 3}
24
+ @hash.reverse_merge!("a" => 0, "d" => 4)
25
+ end
26
+ it "reverse merges another destructively" do
27
+ assert_equal({"a" => 1, "b" => 2, "c" => 3, "d" => 4}, @hash)
28
+ end
29
+ end
30
+ end
31
+
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,18 @@
1
+ require_relative "../../test_helper"
2
+
3
+ module Unit
4
+ module CoreExt
5
+ class TestKernel < Minitest::Test
6
+
7
+ describe Kernel do
8
+ describe "#object_hexid" do
9
+ it "returns the hexadecimal representation of its object_id" do
10
+ object = Struct.new(:object_id).new(70232270636660)
11
+ assert_equal "0x007fc073160ce8", object.object_hexid
12
+ end
13
+ end
14
+ end
15
+
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,30 @@
1
+ require_relative "../test_helper"
2
+
3
+ module Unit
4
+ class TestConnection < Minitest::Test
5
+
6
+ describe GoogleApis::Connection do
7
+ describe "#initialize" do
8
+ it "instantiates a Google::APIClient and Google::APIClient::JWTAsserter" do
9
+ Google::APIClient.expects(:new).with({
10
+ :application_name => "rubygem:google-apis",
11
+ :application_version => GoogleApis::VERSION
12
+ }).returns("< client >")
13
+
14
+ File.expects(:open).with("< private_key >", "rb").returns("< file >")
15
+ Google::APIClient::PKCS12.expects(:load_key).with("< file >", "notasecret").returns("< key >")
16
+ Google::APIClient::JWTAsserter.expects(:new).with("< email_address >", nil, "< key >").returns("< asserter >")
17
+
18
+ connection = GoogleApis::Connection.new(
19
+ :email_address => "< email_address >",
20
+ :private_key => "< private_key >"
21
+ )
22
+
23
+ assert_equal "< client >", connection.instance_variable_get(:@client)
24
+ assert_equal "< asserter >", connection.instance_variable_get(:@asserter)
25
+ end
26
+ end
27
+ end
28
+
29
+ end
30
+ end
@@ -0,0 +1,43 @@
1
+ require_relative "../test_helper"
2
+
3
+ module Unit
4
+ class TestGoogleApis < MiniTest::Test
5
+
6
+ describe GoogleApis do
7
+ it "has the current version" do
8
+ version = File.read(project_file("VERSION")).strip
9
+ assert_equal version, GoogleApis::VERSION
10
+ assert File.read(project_file("CHANGELOG.rdoc")).include?("Version #{version} ")
11
+ end
12
+
13
+ describe ".connect" do
14
+ before do
15
+ @options = {
16
+ :email_address => "< email_address >",
17
+ :private_key => "< private_key >"
18
+ }
19
+ end
20
+
21
+ it "instantiates a connection" do
22
+ GoogleApis::Connection.expects(:new).with(@options).returns(true)
23
+ assert_equal true, GoogleApis.connect(@options)
24
+ end
25
+
26
+ it "memoizes the connection" do
27
+ connection = "< connection >"
28
+ GoogleApis::Connection.expects(:new).with(@options).returns(connection)
29
+ GoogleApis.connect(@options)
30
+ assert_equal connection, GoogleApis.instance_variable_get(:@connection)
31
+ end
32
+ end
33
+
34
+ describe ".connection" do
35
+ it "returns instance variable @connection" do
36
+ GoogleApis.instance_variable_set :@connection, "< connection >"
37
+ assert_equal "< connection >", GoogleApis.connection
38
+ end
39
+ end
40
+ end
41
+
42
+ end
43
+ end
metadata ADDED
@@ -0,0 +1,187 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: google-apis
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Paul Engel
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-02-10 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.8.2
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 0.8.2
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: yard
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
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: pry
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
+ - !ruby/object:Gem::Dependency
70
+ name: simplecov
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: minitest
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: mocha
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
+ description: A thin layer on top of Google::APIClient (google-api-client) for a more
112
+ intuitive way of working (e.g. with BigQuery)
113
+ email: pm_engel@icloud.com
114
+ executables: []
115
+ extensions: []
116
+ extra_rdoc_files: []
117
+ files:
118
+ - ".gitignore"
119
+ - CHANGELOG.rdoc
120
+ - Gemfile
121
+ - MIT-LICENSE
122
+ - README.md
123
+ - Rakefile
124
+ - VERSION
125
+ - google-apis.gemspec
126
+ - lib/google-apis.rb
127
+ - lib/google_apis.rb
128
+ - lib/google_apis/api.rb
129
+ - lib/google_apis/api/base.rb
130
+ - lib/google_apis/api/base/class_methods.rb
131
+ - lib/google_apis/api/base/instance_methods.rb
132
+ - lib/google_apis/api/base/resource.rb
133
+ - lib/google_apis/api/big_query.rb
134
+ - lib/google_apis/api/drive.rb
135
+ - lib/google_apis/api/plus.rb
136
+ - lib/google_apis/connection.rb
137
+ - lib/google_apis/core_ext.rb
138
+ - lib/google_apis/core_ext/hash.rb
139
+ - lib/google_apis/core_ext/kernel.rb
140
+ - lib/google_apis/version.rb
141
+ - script/console
142
+ - test/test_helper.rb
143
+ - test/test_helper/coverage.rb
144
+ - test/unit/api/base/test_class_methods.rb
145
+ - test/unit/api/base/test_instance_methods.rb
146
+ - test/unit/api/test_base.rb
147
+ - test/unit/api/test_big_query.rb
148
+ - test/unit/core_ext/test_hash.rb
149
+ - test/unit/core_ext/test_kernel.rb
150
+ - test/unit/test_connection.rb
151
+ - test/unit/test_google_apis.rb
152
+ homepage: https://github.com/archan937/google-apis
153
+ licenses: []
154
+ metadata: {}
155
+ post_install_message:
156
+ rdoc_options: []
157
+ require_paths:
158
+ - lib
159
+ required_ruby_version: !ruby/object:Gem::Requirement
160
+ requirements:
161
+ - - ">="
162
+ - !ruby/object:Gem::Version
163
+ version: '0'
164
+ required_rubygems_version: !ruby/object:Gem::Requirement
165
+ requirements:
166
+ - - ">="
167
+ - !ruby/object:Gem::Version
168
+ version: '0'
169
+ requirements: []
170
+ rubyforge_project:
171
+ rubygems_version: 2.2.2
172
+ signing_key:
173
+ specification_version: 4
174
+ summary: A thin layer on top of Google::APIClient (google-api-client) for a more intuitive
175
+ way of working (e.g. with BigQuery)
176
+ test_files:
177
+ - test/test_helper.rb
178
+ - test/test_helper/coverage.rb
179
+ - test/unit/api/base/test_class_methods.rb
180
+ - test/unit/api/base/test_instance_methods.rb
181
+ - test/unit/api/test_base.rb
182
+ - test/unit/api/test_big_query.rb
183
+ - test/unit/core_ext/test_hash.rb
184
+ - test/unit/core_ext/test_kernel.rb
185
+ - test/unit/test_connection.rb
186
+ - test/unit/test_google_apis.rb
187
+ has_rdoc: