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

Commit

Permalink
Add mapper autoloading (#63)
Browse files Browse the repository at this point in the history
  • Loading branch information
dlitvakb authored Oct 2, 2018
1 parent 07abb68 commit ff606df
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 20 deletions.
4 changes: 2 additions & 2 deletions .rubocop_todo.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ Metrics/CyclomaticComplexity:
# Configuration parameters: AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, IgnoredPatterns.
# URISchemes: http, https
Metrics/LineLength:
Max: 100
Max: 120

# Offense count: 3
# Configuration parameters: CountComments.
Expand All @@ -33,6 +33,6 @@ Metrics/MethodLength:
# Offense count: 1
# Configuration parameters: ExpectMatchingDefinition, Regex, IgnoreExecutableScripts, AllowedAcronyms.
# AllowedAcronyms: CLI, DSL, ACL, API, ASCII, CPU, CSS, DNS, EOF, GUID, HTML, HTTP, HTTPS, ID, IP, JSON, LHS, QPS, RAM, RHS, RPC, SLA, SMTP, SQL, SSH, TCP, TLS, TTL, UDP, UI, UID, UUID, URI, URL, UTF8, VM, XML, XMPP, XSRF, XSS
Style/FileName:
Naming/FileName:
Exclude:
- 'lib/jekyll-contentful-data-import.rb'
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# Change Log

## Unreleased
### Added
* Added capability to autoload mappers [#22](https://github.com/contentful/jekyll-contentful-data-import/issues/22)

## v1.7.0
### Added
Expand Down
15 changes: 13 additions & 2 deletions lib/jekyll-contentful-data-import/importer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,11 @@ module Contentful
class Importer
attr_reader :config

def initialize(config)
@config = config
def initialize(jekyll_config)
@jekyll_config = jekyll_config
@config = jekyll_config['contentful']

autoload_mappers!
end

def run
Expand Down Expand Up @@ -117,6 +120,14 @@ def client_options(options)
options.delete(:raise_errors)
options
end

def autoload_mappers!
mapper_search_path = File.join(@jekyll_config['source'], @jekyll_config['plugins_dir'], 'mappers')
mapper_files = Jekyll::Utils.safe_glob(mapper_search_path, File.join('**', '*.rb'))
Jekyll::External.require_with_graceful_fail(mapper_files)
rescue StandardError
Jekyll.logger.debug "Couldn't find custom mappers"
end
end
end
end
7 changes: 3 additions & 4 deletions lib/jekyll/commands/contentful.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,14 @@ def self.options
def self.command_action(command)
command.action do |args, options|
jekyll_options = configuration_from_options(options)
contentful_config = jekyll_options['contentful']
process args, options, contentful_config
process args, options, jekyll_options
end
end

def self.process(_args = [], options = {}, contentful_config = {})
def self.process(_args = [], options = {}, config = {})
Jekyll.logger.info 'Starting Contentful import'

Jekyll::Contentful::Importer.new(contentful_config).run
Jekyll::Contentful::Importer.new(config).run

Jekyll.logger.info 'Contentful import finished'

Expand Down
66 changes: 54 additions & 12 deletions spec/jekyll-contentful/importer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,12 @@ def run; end
end

describe Jekyll::Contentful::Importer do
before :each do
allow(Jekyll.logger).to receive(:debug).with("Couldn't find custom mappers")
end

let(:config) do
{
{ 'contentful' => {
'spaces' => [
{
'example' => {
Expand All @@ -31,7 +35,7 @@ def run; end
}
}
]
}
}}
end
subject { described_class.new(config) }

Expand Down Expand Up @@ -94,27 +98,29 @@ def run; end
it 'runs exporter with correct arguments' do
allow(subject).to receive(:client).and_return(ClientDouble.new)

expect(Jekyll::Contentful::SingleFileDataExporter).to receive(:new).with('example', [], config['spaces'].first['example']).and_return(ExporterDouble.new)
expect(Jekyll::Contentful::SingleFileDataExporter).to receive(:new).with('example', [], config['contentful']['spaces'].first['example']).and_return(ExporterDouble.new)

subject.run
end

it 'runs multifile exporter when passed :individual_entry_files flag' do
config = {
'spaces' => [
{
'example' => {
'space' => 'cfexampleapi',
'access_token' => 'b4c0n73n7fu1',
'individual_entry_files' => true
'contentful' => {
'spaces' => [
{
'example' => {
'space' => 'cfexampleapi',
'access_token' => 'b4c0n73n7fu1',
'individual_entry_files' => true
}
}
}
]
]
}
}
subject = described_class.new(config)
allow(subject).to receive(:client).and_return(ClientDouble.new)

expect(Jekyll::Contentful::MultiFileDataExporter).to receive(:new).with('example', [], config['spaces'].first['example']).and_return(ExporterDouble.new)
expect(Jekyll::Contentful::MultiFileDataExporter).to receive(:new).with('example', [], config['contentful']['spaces'].first['example']).and_return(ExporterDouble.new)

subject.run
end
Expand Down Expand Up @@ -143,4 +149,40 @@ def run; end
end
end
end

describe 'mappers are autoloaded' do
let(:jekyll_config) do
{ 'contentful' => {
'spaces' => [
{
'example' => {
'space' => 'cfexampleapi',
'access_token' => 'b4c0n73n7fu1'
}
}
]
}}
end

it 'custom mappers are autoloaded' do
config = jekyll_config.merge('source' => '.', 'plugins_dir' => '_plugins')

allow(subject).to receive(:spaces).and_return([['foo', {'space' => 'foo', 'access_token' => 'bar'}], ['bar', {'space' => 'bar', 'access_token' => 'foo'}]])
allow(subject).to receive(:client).and_return(ClientDouble.new)

expect(Jekyll::Utils).to receive(:safe_glob).with(File.join('.', '_plugins', 'mappers'), File.join('**', '*.rb')) { ['some_mapper.rb'] }
expect(Jekyll::External).to receive(:require_with_graceful_fail).with(['some_mapper.rb'])

described_class.new(config)
end

it 'raises a warning if no mappers found' do
allow(subject).to receive(:spaces).and_return([['foo', {'space' => 'foo', 'access_token' => 'bar'}], ['bar', {'space' => 'bar', 'access_token' => 'foo'}]])
allow(subject).to receive(:client).and_return(ClientDouble.new)

expect(Jekyll.logger).to receive(:debug).with("Couldn't find custom mappers")

described_class.new(config)
end
end
end
4 changes: 4 additions & 0 deletions spec/jekyll/commands/contentful_spec.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
require 'spec_helper'

describe Jekyll::Commands::Contentful do
before :each do
allow(Jekyll.logger).to receive(:debug).with("Couldn't find custom mappers")
end

describe 'class methods' do
describe '::init_with_program' do
it 'implements jekyll command interface' do
Expand Down

0 comments on commit ff606df

Please sign in to comment.