forked from adamsalter/sitemap_generator
-
Notifications
You must be signed in to change notification settings - Fork 277
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #340 from kjvarga/kvarga/pull-326-google-cloud-sto…
…rage Google Cloud Storage support
- Loading branch information
Showing
8 changed files
with
127 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
6.0.2 | ||
6.1.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
if !defined?(Google::Cloud::Storage) | ||
raise "Error: `Google::Cloud::Storage` is not defined.\n\n"\ | ||
"Please `require 'google/cloud/storage'` - or another library that defines this class." | ||
end | ||
|
||
module SitemapGenerator | ||
# Class for uploading sitemaps to a Google Storage using `google-cloud-storage` gem. | ||
class GoogleStorageAdapter | ||
# Requires Google::Cloud::Storage to be defined. | ||
# | ||
# @param [Hash] opts Google::Cloud::Storage configuration options. | ||
# @option :bucket [String] Required. Name of Google Storage Bucket where the file is to be uploaded. | ||
# | ||
# All options other than the `:bucket` option are passed to the `Google::Cloud::Storage.new` | ||
# initializer. See https://googleapis.dev/ruby/google-cloud-storage/latest/file.AUTHENTICATION.html | ||
# for all the supported environment variables and https://github.com/googleapis/google-cloud-ruby/blob/master/google-cloud-storage/lib/google/cloud/storage.rb | ||
# for supported options. | ||
# | ||
# Suggested Options: | ||
# @option :credentials [String] Path to Google service account JSON file, or JSON contents. | ||
# @option :project_id [String] Google Accounts project id where the storage bucket resides. | ||
def initialize(opts = {}) | ||
opts = opts.clone | ||
@bucket = opts.delete(:bucket) | ||
@storage_options = opts | ||
end | ||
|
||
# Call with a SitemapLocation and string data | ||
def write(location, raw_data) | ||
SitemapGenerator::FileAdapter.new.write(location, raw_data) | ||
|
||
storage = Google::Cloud::Storage.new(@storage_options) | ||
bucket = storage.bucket(@bucket) | ||
bucket.create_file(location.path, location.path_in_public, acl: 'public') | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
spec/sitemap_generator/adapters/google_storage_adapter_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
# encoding: UTF-8 | ||
require 'spec_helper' | ||
require 'google/cloud/storage' | ||
|
||
describe SitemapGenerator::GoogleStorageAdapter do | ||
subject(:adapter) { SitemapGenerator::GoogleStorageAdapter.new(options) } | ||
|
||
let(:options) { { credentials: 'abc', project_id: 'project_id', bucket: 'bucket' } } | ||
|
||
describe 'write' do | ||
let(:location) { SitemapGenerator::SitemapLocation.new } | ||
|
||
it 'writes the raw data to a file and then uploads that file to Google Storage' do | ||
bucket = double(:bucket) | ||
storage = double(:storage) | ||
bucket_resource = double(:bucket_resource) | ||
expect(Google::Cloud::Storage).to receive(:new).with(credentials: 'abc', project_id: 'project_id').and_return(storage) | ||
expect(storage).to receive(:bucket).with('bucket').and_return(bucket_resource) | ||
expect(location).to receive(:path_in_public).and_return('path_in_public') | ||
expect(location).to receive(:path).and_return('path') | ||
expect(bucket_resource).to receive(:create_file).with('path', 'path_in_public', acl: 'public').and_return(nil) | ||
expect_any_instance_of(SitemapGenerator::FileAdapter).to receive(:write).with(location, 'raw_data') | ||
adapter.write(location, 'raw_data') | ||
end | ||
end | ||
|
||
describe '.new' do | ||
it "doesn't modify the original options" do | ||
adapter | ||
expect(options.size).to be(3) | ||
end | ||
end | ||
end |