Skip to content

Commit

Permalink
Allow Fog public option to be Configurable (#359)
Browse files Browse the repository at this point in the history
* Allow Fog public option to be Configurable

* Add documentation; don't use Rails language features

* Support environment variable for consistency; add documentation

* Validate the call to create has correct arguments

Co-authored-by: Karl Varga <kjvarga@gmail.com>
  • Loading branch information
mstruve and kjvarga committed Jan 11, 2022
1 parent 8a71fcf commit fce9c0c
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 2 deletions.
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,25 @@ directory.

You must `require 'fog-aws'` in your sitemap config before using this adapter.

An example of using this adapter in your sitemap configuration:

```ruby
SitemapGenerator::Sitemap.adapter = SitemapGenerator::S3Adapter.new(options)
```

Where `options` is a Hash with any of the following keys:
* `aws_access_key_id` [String] Your AWS access key id
* `aws_secret_access_key` [String] Your AWS secret access key
* `fog_provider` [String]
* `fog_directory` [String]
* `fog_region` [String]
* `fog_path_style` [String]
* `fog_storage_options` [Hash] Other options to pass to `Fog::Storage`
* `fog_public` [Boolean] Whether the file is publicly accessible

Alternatively you can use an environment variable to configure each option (except `fog_storage_options`). The environment variables have the same
name but capitalized, e.g. `FOG_PATH_STYLE`.

##### `SitemapGenerator::AwsSdkAdapter`

Uses `Aws::S3::Resource` to upload to Amazon S3 storage. Includes automatic detection of your AWS
Expand Down
8 changes: 7 additions & 1 deletion lib/sitemap_generator/adapters/s3_adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ class S3Adapter
# @option :fog_region [String]
# @option :fog_path_style [String]
# @option :fog_storage_options [Hash] Other options to pass to `Fog::Storage`
# @option :fog_public [Boolean] Whether the file is publicly accessible
#
# Alternatively you can use an environment variable to configure each option (except `fog_storage_options`).
# The environment variables have the same name but capitalized, e.g. `FOG_PATH_STYLE`.
def initialize(opts = {})
@aws_access_key_id = opts[:aws_access_key_id] || ENV['AWS_ACCESS_KEY_ID']
@aws_secret_access_key = opts[:aws_secret_access_key] || ENV['AWS_SECRET_ACCESS_KEY']
Expand All @@ -24,6 +28,8 @@ def initialize(opts = {})
@fog_region = opts[:fog_region] || ENV['FOG_REGION']
@fog_path_style = opts[:fog_path_style] || ENV['FOG_PATH_STYLE']
@fog_storage_options = opts[:fog_storage_options] || {}
fog_public = opts[:fog_public].nil? ? ENV['FOG_PUBLIC'] : opts[:fog_public]
@fog_public = SitemapGenerator::Utilities.falsy?(fog_public) ? false : true
end

# Call with a SitemapLocation and string data
Expand All @@ -47,7 +53,7 @@ def write(location, raw_data)
directory.files.create(
:key => location.path_in_public,
:body => File.open(location.path),
:public => true
:public => @fog_public
)
end
end
Expand Down
54 changes: 53 additions & 1 deletion spec/sitemap_generator/adapters/s3_adapter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
require 'fog-aws'

describe SitemapGenerator::S3Adapter do
subject(:adapter) { described_class.new(options) }

let(:location) do
SitemapGenerator::SitemapLocation.new(
:namer => SitemapGenerator::SimpleNamer.new(:sitemap),
Expand All @@ -23,6 +25,18 @@
)
)
end
let(:options) do
{
aws_access_key_id: 'aws_access_key_id',
aws_secret_access_key: 'aws_secret_access_key',
fog_provider: 'fog_provider',
fog_directory: 'fog_directory',
fog_region: 'fog_region',
fog_path_style: 'fog_path_style',
fog_storage_options: {},
fog_public: false,
}
end

context 'when Fog::Storage is not defined' do
it 'raises a LoadError' do
Expand All @@ -33,10 +47,48 @@
end
end

describe 'initialize' do
it 'sets options on the instance' do
expect(adapter.instance_variable_get(:@aws_access_key_id)).to eq('aws_access_key_id')
expect(adapter.instance_variable_get(:@aws_secret_access_key)).to eq('aws_secret_access_key')
expect(adapter.instance_variable_get(:@fog_provider)).to eq('fog_provider')
expect(adapter.instance_variable_get(:@fog_directory)).to eq('fog_directory')
expect(adapter.instance_variable_get(:@fog_region)).to eq('fog_region')
expect(adapter.instance_variable_get(:@fog_path_style)).to eq('fog_path_style')
expect(adapter.instance_variable_get(:@fog_storage_options)).to eq(options[:fog_storage_options])
expect(adapter.instance_variable_get(:@fog_public)).to eq(false)
end

context 'fog_public' do
let(:options) do
{ fog_public: nil }
end

it 'defaults to true' do
expect(adapter.instance_variable_get(:@fog_public)).to eq(true)
end

context 'when a string value' do
let(:options) do
{ fog_public: 'false' }
end

it 'converts to a boolean' do
expect(adapter.instance_variable_get(:@fog_public)).to eq(false)
end
end
end
end

describe 'write' do
it 'creates the file in S3 with a single operation' do
expect(Fog::Storage).to receive(:new).and_return(directories)
subject.write(location, 'payload')
expect(directory.files).to receive(:create).with(
body: instance_of(File),
key: 'test/sitemap.xml.gz',
public: false,
)
adapter.write(location, 'payload')
end
end
end

0 comments on commit fce9c0c

Please sign in to comment.