Skip to content

Commit

Permalink
Adding spec for GoogleStorageAdapter
Browse files Browse the repository at this point in the history
  • Loading branch information
raviy06 committed May 10, 2019
1 parent 0360f5a commit 6286dee
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 9 deletions.
20 changes: 11 additions & 9 deletions lib/sitemap_generator/adapters/google_storage_adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,27 @@
end

module SitemapGenerator
# Class for uploading sitemaps to a Google Storage supported endpoint.
# 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 Fog configuration options
# @option :credentials [Hash] Path to the google service account keyfile.json
# @option :project_id [String] Google Accounts project_id where the storage bucket resides
# @option :bucket [String] Name of Google Storage Bucket where the file is to be uploaded
# Options:
# :credentials [String] Path to the google service account keyfile.json
# :project_id [String] Google Accounts project_id where the storage bucket resides
# :bucket [String] Name of Google Storage Bucket where the file is to be uploaded

# @param [Hash] opts Google::Cloud::Storage configuration options
def initialize(opts = {})
@credentials = opts[:keyfile] || ENV['']
@project_id = opts[:project_id] || ENV['']
@bucket = opts[:bucket] || ENV['']
@credentials = opts[:keyfile] || ENV['GOOGLE_CLOUD_PROJECT']
@project_id = opts[:project_id] || ENV['GOOGLE_APPLICATION_CREDENTIALS']
@bucket = opts[:bucket]
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(project_id: @project_id, credentials: @credentials)
storage = Google::Cloud::Storage.new(project_id: @project_id, credentials: @credentials)
bucket = storage.bucket @bucket
bucket.create_file location.path, location.path_in_public, acl: 'public'
end
Expand Down
25 changes: 25 additions & 0 deletions spec/sitemap_generator/adapters/google_storage_adapter_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# encoding: UTF-8
require 'spec_helper'
require 'google/cloud/storage'

describe SitemapGenerator::GoogleStorageAdapter do
let(:location) { SitemapGenerator::SitemapLocation.new }
let(:options) { {:credentials=>nil, :project_id=>nil} }
let(:google_bucket) { 'bucket' }
let(:adapter) { SitemapGenerator::GoogleStorageAdapter.new(options.merge(bucket: google_bucket)) }

describe 'write' do
it '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(options).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
end

0 comments on commit 6286dee

Please sign in to comment.