google-apis 0.1.2 → 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: b8d1add3229c971662ab77620ca8b6a11021803e
4
- data.tar.gz: ba08f0e27722cc5055ab23c6142777e7edbc2b60
3
+ metadata.gz: 5bedf6667092812f0ef8e2292a028ae8d63dd0f3
4
+ data.tar.gz: 4a3d38dec4fe35449d6b0b71e397f49008d900c4
5
5
  SHA512:
6
- metadata.gz: 13de4d3734a4c18d8534c69478c2ce988648be0deb0475a3743343c6fd1e9f02bcdd13b9b96617a9f7de156dda5aed9683d1fbe6537c6b03a659e6d30187c70c
7
- data.tar.gz: 83788437def396e77c72749567e75a54f570c14db8a1c0f2b2cced2bba0b9c7d5da98d94fda08b7cb785dfab780aa028761568565887128b2098a754d5004f15
6
+ metadata.gz: 2ca597c89963a2e0464d8d30b8cea8f963f7b6163aadebc6a071eb6269951233a0c9456fb6fc9efa68f222a1e92cb676aa85b0f9425f193a9f7bfb915675b467
7
+ data.tar.gz: 616f14c243a99469ade6b4a1c809f3853ae4ac95f1d3c5cfab8cd1a62c623819d4cca35a7c2ab5e346706d4526c77f6afd798c06f93b9c815d7b04b97fd738d4
data/CHANGELOG.rdoc CHANGED
@@ -1,5 +1,14 @@
1
1
  = Google APIs CHANGELOG
2
2
 
3
+ == Version 0.1.3 (April 20, 2015)
4
+
5
+ * Camelizing underscored default param keys
6
+ * Defined private #default_params
7
+ * Defined Google::BigQuery.connection.project and Google::BigQuery.connection.dataset
8
+ * Using HTTPClient as default Faraday adapter
9
+ * Being able to (easily) upload to Google Storage
10
+ * Improved downloading files from Google Storage
11
+
3
12
  == Version 0.1.2 (April 17, 2015)
4
13
 
5
14
  * Added Google Storage API definition
data/README.md CHANGED
@@ -25,7 +25,7 @@ Do not forget to download the private key by generating a P12 key file.
25
25
  ```ruby
26
26
  [1] pry(main)> GoogleApis.connect :email_address => "lorem@developer.gserviceaccount.com", :private_key => "/path/to/private/key.p12"
27
27
  => #<GoogleApis::Connection:0x007fa5a4743668 [lorem@developer.gserviceaccount.com]>
28
- [2] pry(main)> bq = Google::BigQuery.new :projectId => "your_project_id", :datasetId => "your_dataset_id"
28
+ [2] pry(main)> bq = Google::BigQuery.new :project_id => "your_project_id", :dataset_id => "your_dataset_id"
29
29
  => #<Google::BigQuery:0x007fc5c68647a8 v2:[datasets,jobs,projects,tabledata,tables] {projectId:"your_project_id",datasetId:"your_dataset_id"}>
30
30
  [3] pry(main)> bq.tables.list
31
31
  => {"kind"=>"bigquery#tableList",
@@ -77,8 +77,8 @@ You can also configure an application-wide API connection. Let's say you also st
77
77
  ---
78
78
  email_address: lorem@developer.gserviceaccount.com
79
79
  private_key: "/path/to/private/key.p12"
80
- projectId: your_project_id
81
- datasetId: your_dataset_id
80
+ project_id: your_project_id
81
+ dataset_id: your_dataset_id
82
82
  ```
83
83
 
84
84
  ```ruby
@@ -123,11 +123,28 @@ The following example demonstrates how to download a file from Google Storage:
123
123
  "owner"=>{"entity"=>"user-00b4903a97b10389ce680ca45ba5999e068c4d0c8ccbbfbb7094238bc85a567", "entityId"=>"00b4903a97b20004ce680ca5f5aeebe068c4d0c8ccbbfbb7094266d1b9787457"},
124
124
  "crc32c"=>"P4UlsJQ==",
125
125
  "etag"=>"EN93lyNu/j8QCEAE="}]}
126
- [3] pry(main)> Google::Storage.download metadata["items"][0]["mediaLink"]
126
+ [3] pry(main)> Google::Storage.download metadata["items"][0]["mediaLink"], "foo/"
127
127
  => 5592792
128
- [4] pry(main)> puts `ls -l | grep awesome`
128
+ [4] pry(main)> puts `ls -l foo | grep awesome`
129
129
  -rw-rw-r-- 1 paulengel paulengel 5592792 Apr 17 16:38 awesome.tsv
130
130
  => nil
131
+ [5] pry(main)> Google::Storage.objects.insert :bucket => "your-bucket", :media => "path/to/your/local/awesome.file.gz", :directory => "awesome_directory"
132
+ => {"kind"=>"storage#object",
133
+ "id"=>"your-bucket/awesome_directory/awesome.file.gz/1429539088390000",
134
+ "selfLink"=>"https://www.googleapis.com/storage/v1/b/your-bucket/o/awesome_directory%2Fawesome.file.gz",
135
+ "name"=>"awesome_directory/awesome.file.gz",
136
+ "bucket"=>"your-bucket",
137
+ "generation"=>"1429539088390000",
138
+ "metageneration"=>"1",
139
+ "contentType"=>"text/plain",
140
+ "updated"=>"2015-04-20T14:11:28.390Z",
141
+ "storageClass"=>"STANDARD",
142
+ "size"=>"5898192",
143
+ "md5Hash"=>"82Pe0oFOTdC6nP86/K4fmA==",
144
+ "mediaLink"=>"https://www.googleapis.com/download/storage/v1/b/your-bucket/o/awesome_directory%2Fawesome.file.gz?generation=1429539088390000&alt=media",
145
+ "owner"=>{"entity"=>"user-00b49031947c40e21c1934ed03110ed7f71bfc2f8dd1982c257ad66d72e5d9a2", "entityId"=>"00b49031947c40e21c0e306d03110ed7f71bfc2f8dd1982c257ad66d72ea1934"},
146
+ "crc32c"=>"qPMe/Q==",
147
+ "etag"=>"ENGluYyIhcUCEAE="}
131
148
  ```
132
149
 
133
150
  Easy, huh? :)
@@ -161,7 +178,7 @@ If it isn't already clear, you can specify a global Google API connection and us
161
178
  [3] pry(main)> Google::Drive.files.list
162
179
  => {"kind"=>"drive#fileList",
163
180
  ...
164
- [4] pry(main)> Google::BigQuery.connect :projectId => "your_project_id", :datasetId => "your_dataset_id"
181
+ [4] pry(main)> Google::BigQuery.connect :project_id => "your_project_id", :dataset_id => "your_dataset_id"
165
182
  => #<Google::BigQuery:0x007ffe0b1fb240 v2:[datasets,jobs,projects,tabledata,tables] {projectId:"your_project_id",datasetId:"your_dataset_id"}>
166
183
  [5] pry(main)> Google::BigQuery.tables.list
167
184
  => {"kind"=>"bigquery#tableList",
@@ -176,10 +193,10 @@ Run the following command in your console:
176
193
 
177
194
  ```ruby
178
195
  $ script/console
179
- Loading Google APIs development environment (0.1.0)
196
+ Loading Google APIs development environment (0.1.3)
180
197
  [1] pry(main)> GoogleApis.connect :email_address => "", :private_key => "/path/to/private/key.p12"
181
198
  => #<GoogleApis::Connection:0x007ff3d356cbf0 [lorem@developer.gserviceaccount.com]>
182
- [2] pry(main)> bq = Google::BigQuery.new :projectId => "your_project_id", :datasetId => "your_dataset_id"
199
+ [2] pry(main)> bq = Google::BigQuery.new :project_id => "your_project_id", :dataset_id => "your_dataset_id"
183
200
  => #<Google::BigQuery:0x007f8c09a05338 v2:[datasets,jobs,projects,tabledata,tables] {projectId:"your_project_id",datasetId:"your_dataset_id"}>
184
201
  ```
185
202
 
@@ -195,8 +212,8 @@ And immediately start instantiating a Google API:
195
212
 
196
213
  ```ruby
197
214
  $ script/console
198
- Loading Google APIs development environment (0.1.0)
199
- [1] pry(main)> bq = Google::BigQuery.new :projectId => "your_project_id", :datasetId => "your_dataset_id"
215
+ Loading Google APIs development environment (0.1.3)
216
+ [1] pry(main)> bq = Google::BigQuery.new :project_id => "your_project_id", :dataset_id => "your_dataset_id"
200
217
  => #<Google::BigQuery:0x007fa6c9cc3450 v2:[datasets,jobs,projects,tabledata,tables] {projectId:"your_project_id",datasetId:"your_dataset_id"}>
201
218
  ```
202
219
 
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.2
1
+ 0.1.3
data/google-apis.gemspec CHANGED
@@ -17,6 +17,7 @@ Gem::Specification.new do |gem|
17
17
  gem.licenses = ["MIT"]
18
18
 
19
19
  gem.add_dependency "google-api-client", "~> 0.8.2"
20
+ gem.add_dependency "httpclient"
20
21
 
21
22
  gem.add_development_dependency "rake"
22
23
  gem.add_development_dependency "yard"
@@ -13,12 +13,12 @@ module GoogleApis
13
13
  raise Error, "Please ensure a Google API connection" unless @connection
14
14
 
15
15
  @discovered_api = connection.discover_api self.class.api, self.class.version
16
- @default_params = default_params || {}
16
+ @default_params = (default_params || {}).inject({}){|h, (k, v)| h[k.to_s.gsub(/_(.)/){$1.upcase}.to_sym] = v; h}
17
17
  end
18
18
 
19
19
  def execute(api_method, *params)
20
20
  params[0] = (params[0] || {}).symbolize_keys
21
- params[0].reverse_merge!(@default_params)
21
+ params[0].reverse_merge!(default_params)
22
22
  connection.execute self.class, api_method, *params
23
23
  end
24
24
 
@@ -27,7 +27,7 @@ module GoogleApis
27
27
  end
28
28
 
29
29
  def inspect
30
- "#<#{self.class}:#{object_hexid} #{discovered_api.version}:[#{discovered_api.discovered_resources.collect(&:name).sort.join(",")}] {#{@default_params.collect{|k, v| "#{k}:#{v.inspect}"}.join(",")}}>"
30
+ "#<#{self.class}:#{object_hexid} #{discovered_api.version}:[#{discovered_api.discovered_resources.collect(&:name).sort.join(",")}] {#{default_params.collect{|k, v| "#{k}:#{v.inspect}"}.join(",")}}>"
31
31
  end
32
32
 
33
33
  def method_missing(name, *args)
@@ -40,6 +40,10 @@ module GoogleApis
40
40
 
41
41
  private
42
42
 
43
+ def default_params
44
+ @default_params
45
+ end
46
+
43
47
  def find(name)
44
48
  discovered_api.discovered_resources.detect{|x| x.name == name.to_s}
45
49
  end
@@ -7,6 +7,14 @@ module GoogleApis
7
7
  version 2
8
8
  auth_scope "https://www.googleapis.com/auth/bigquery"
9
9
 
10
+ def project
11
+ default_params[:projectId]
12
+ end
13
+
14
+ def dataset
15
+ default_params[:datasetId]
16
+ end
17
+
10
18
  def select_rows(statement)
11
19
  result = jobs.query :query => statement
12
20
  types = result["schema"]["fields"].collect{|x| x["type"].downcase.to_sym}
@@ -1,4 +1,5 @@
1
1
  require "uri"
2
+ require "fileutils"
2
3
 
3
4
  module GoogleApis
4
5
  class Connection
@@ -28,32 +29,40 @@ module GoogleApis
28
29
  end
29
30
 
30
31
  def execute(api, api_method, *params)
31
- authenticate!(api)
32
-
33
- nested, top_level = params
32
+ options, headers = params
34
33
 
35
- if nested
34
+ if options
36
35
  parameter_keys = (api_method.discovery_document["parameters"] || {}).keys.collect(&:to_sym)
37
- parameters, body_object = nested.partition{|k, v| parameter_keys.include?(k)}.collect{|x| Hash[x] unless x.empty?}
36
+ media = options.delete(:media)
37
+ parameters, body_object = options.partition{|k, v| parameter_keys.include?(k)}.collect{|x| Hash[x]}
38
+ end
39
+
40
+ if media && media.is_a?(String)
41
+ parameters[:uploadType] = "resumable"
42
+ parameters[:name] ||= File.basename(media)
43
+ if directory = body_object.delete(:directory)
44
+ parameters[:name] = File.join(directory, parameters[:name])
45
+ end
46
+ media = Google::APIClient::UploadIO.new(media, `file --mime -b #{media}`.split(";")[0])
38
47
  end
39
48
 
40
49
  options = {:api_method => api_method}
41
- options[:parameters] = parameters if parameters
42
- options[:body_object] = body_object if body_object
43
- options.merge!(top_level) if top_level
50
+ options[:parameters] = parameters unless parameters.empty?
51
+ options[:body_object] = body_object unless body_object.empty?
52
+ options[:media] = media if media
53
+ options.merge!(headers) if headers
44
54
 
45
- parse! @client.execute(options)
55
+ parse! execute!(api, options)
46
56
  end
47
57
 
48
58
  def download(api, uri, to = nil)
49
- authenticate!(api)
59
+ options = Google::APIClient::Request.new(:uri => uri)
50
60
 
51
- response = @client.execute Google::APIClient::Request.new(:uri => uri)
52
- to ||= File.basename CGI.unescape(URI.parse(uri).path)
53
-
54
- File.open(to, "wb") do |file|
55
- file.write response.body
61
+ if to.nil? || File.directory?(to) || to.match(/\/$/)
62
+ to = File.join *[to, File.basename(CGI.unescape(URI.parse(uri).path))].compact
56
63
  end
64
+
65
+ save! execute!(api, options), to
57
66
  end
58
67
 
59
68
  def inspect
@@ -62,6 +71,11 @@ module GoogleApis
62
71
 
63
72
  private
64
73
 
74
+ def execute!(api, options)
75
+ authenticate!(api)
76
+ @client.execute(options)
77
+ end
78
+
65
79
  def authenticate!(api)
66
80
  if !@asserter.scope.include?(api.auth_scope) || @client.authorization.expired?
67
81
  @asserter.scope = (@asserter.scope.split(" ") << api.auth_scope).uniq
@@ -79,5 +93,12 @@ module GoogleApis
79
93
  end
80
94
  end
81
95
 
96
+ def save!(response, to)
97
+ FileUtils.mkdir_p File.dirname(to)
98
+ File.open(to, "wb") do |file|
99
+ file.write(response.body)
100
+ end
101
+ end
102
+
82
103
  end
83
104
  end
@@ -1,7 +1,7 @@
1
1
  module GoogleApis
2
2
  MAJOR = 0
3
3
  MINOR = 1
4
- TINY = 2
4
+ TINY = 3
5
5
 
6
6
  VERSION = [MAJOR, MINOR, TINY].join(".")
7
7
  end
data/lib/google_apis.rb CHANGED
@@ -18,3 +18,5 @@ module GoogleApis
18
18
  end
19
19
 
20
20
  end
21
+
22
+ Faraday.default_adapter = :httpclient
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: google-apis
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Paul Engel
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-04-17 00:00:00.000000000 Z
11
+ date: 2015-04-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: google-api-client
@@ -24,6 +24,20 @@ dependencies:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
26
  version: 0.8.2
27
+ - !ruby/object:Gem::Dependency
28
+ name: httpclient
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'
27
41
  - !ruby/object:Gem::Dependency
28
42
  name: rake
29
43
  requirement: !ruby/object:Gem::Requirement