Skip to content
This repository has been archived by the owner on May 8, 2024. It is now read-only.

Commit

Permalink
Add environment as top-level option and fetching from env vars (#62)
Browse files Browse the repository at this point in the history
  • Loading branch information
dlitvakb authored Aug 1, 2018
1 parent 02a70bf commit faa2823
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 9 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
rvm:
- 2.2.3
- 2.3.1
- 2.4.0
- 2.5.1

script: "bundle exec rake test"

Expand Down
5 changes: 3 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
# Change Log

## Unreleased
### Added
* Added top-level environment assignment, with support for `ENV_` access [#61](https://github.com/contentful/jekyll-contentful-data-import/issues/61)

## 1.6.0

## v1.6.0
### Added
* Independent file per entry [#45](https://github.com/contentful/jekyll-contentful-data-import/pull/45) [#10](https://github.com/contentful/jekyll-contentful-data-import/issues/10) [#23](https://github.com/contentful/jekyll-contentful-data-import/pull/23) [#25](https://github.com/contentful/jekyll-contentful-data-import/issues/25)
* Added User Agent Integration Headers
Expand Down
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ contentful:
- example: # Jekyll _data folder identifier - Required
space: cfexampleapi # Required
access_token: b4c0n73n7fu1 # Required
environment: master # Optional
cda_query: # Optional
include: 2
limit: 100
Expand All @@ -65,6 +66,7 @@ Parameter | Description
---------- | ------------
space | Contentful Space ID
access_token | Contentful Delivery API access token
environment | Space environment, defaults to `master`
cda_query | Hash describing query configuration. See [contentful.rb](https://github.com/contentful/contentful.rb) for more info (look for filter options there). Note that by default only 100 entries will be fetched, this can be configured to up to 1000 entries using the `limit` option.
all_entries | Boolean, if true will run multiple queries to the API until it fetches all entries for the space
all_entries_page_size | Integer, the amount of maximum entries per CDA Request when fetching :all_entries
Expand Down Expand Up @@ -138,23 +140,25 @@ therefore you can do the following:
- example:
space: ENV_CONTENTFUL_SPACE_ID
access_token: ENV_CONTENTFUL_ACCESS_TOKEN
environment: ENV_CONTENTFUL_ENVIRONMENT
```

(Your Space ID will be looked upon on `ENV['CONTENTFUL_SPACE_ID']` and your Access Token on `ENV['CONTENTFUL_ACCESS_TOKEN']`.)
(Your Space ID will be looked upon on `ENV['CONTENTFUL_SPACE_ID']`, your Access Token on `ENV['CONTENTFUL_ACCESS_TOKEN']` and your environment on `ENV['CONTENTFUL_ENVIRONMENT']`.)

3. Either add the following variables to your shell's configuration file (.bashrc or .bash_profile, for example):

```bash
export CONTENTFUL_ACCESS_TOKEN=abc123
export CONTENTFUL_SPACE_ID=abc123
export CONTENTFUL_ENVIRONMENT=master
```

(And run `source ~/.bashrc` or open new terminal to enable changes.)

Or specify them on the command line:

```bash
CONTENTFUL_ACCESS_TOKEN=abc123 CONTENTFUL_SPACE_ID=abc123 jekyll contentful
CONTENTFUL_ACCESS_TOKEN=abc123 CONTENTFUL_SPACE_ID=abc123 CONTENTFUL_ENVIRONMENT=master jekyll contentful
```

4. Party.
Expand Down
2 changes: 1 addition & 1 deletion lib/jekyll-contentful-data-import.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
require 'jekyll-contentful-data-import/version'

%w(contentful).each do |file|
%w[contentful].each do |file|
require File.expand_path("jekyll/commands/#{file}.rb", File.dirname(__FILE__))
end
6 changes: 4 additions & 2 deletions lib/jekyll-contentful-data-import/importer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ def run
space_client = client(
value_for(options, 'space'),
value_for(options, 'access_token'),
value_for(options, 'environment'),
client_options(options.fetch('client_options', {}))
)

Expand Down Expand Up @@ -62,7 +63,7 @@ def export_data_multiple_files(name, entries, options)

def value_for(options, key)
potential_value = options[key]
return ENV[potential_value.gsub('ENV_', '')] if potential_value.start_with?('ENV_')
return ENV[potential_value.gsub('ENV_', '')] if !potential_value.nil? && potential_value.start_with?('ENV_')
potential_value
end

Expand Down Expand Up @@ -90,10 +91,11 @@ def get_entries(space_client, options)
all
end

def client(space, access_token, options = {})
def client(space, access_token, environment = nil, options = {})
options = {
space: space,
access_token: access_token,
environment: environment || 'master',
dynamic_entries: :auto,
raise_errors: true,
integration_name: 'jekyll',
Expand Down
4 changes: 3 additions & 1 deletion spec/jekyll-contentful/importer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ def run; end
expect(::Contentful::Client).to receive(:new).with(
space: 'foo',
access_token: 'foobar',
environment: 'master',
dynamic_entries: :auto,
raise_errors: true,
integration_name: 'jekyll',
Expand All @@ -58,13 +59,14 @@ def run; end
expect(::Contentful::Client).to receive(:new).with(
space: 'foo',
access_token: 'foobar',
environment: 'master',
dynamic_entries: :auto,
raise_errors: false,
integration_name: 'jekyll',
integration_version: Jekyll::Contentful::VERSION
)

subject.client('foo', 'foobar', raise_errors: false)
subject.client('foo', 'foobar', 'master', raise_errors: false)
end
end

Expand Down

0 comments on commit faa2823

Please sign in to comment.