Skip to content

Commit

Permalink
Update Code Style (#43)
Browse files Browse the repository at this point in the history
  • Loading branch information
dlitvakb committed Apr 12, 2017
1 parent 2298258 commit 1ee4175
Show file tree
Hide file tree
Showing 12 changed files with 137 additions and 40 deletions.
1 change: 1 addition & 0 deletions .rubocop.yml
@@ -0,0 +1 @@
inherit_from: .rubocop_todo.yml
38 changes: 38 additions & 0 deletions .rubocop_todo.yml
@@ -0,0 +1,38 @@
# This configuration was generated by
# `rubocop --auto-gen-config`
# on 2017-04-11 11:57:26 -0300 using RuboCop version 0.47.1.
# The point is for the user to remove these configuration records
# one by one as the offenses are removed from the code base.
# Note that changes in the inspected code, or installation of new
# versions of RuboCop, may require this file to be generated again.

# Offense count: 2
Metrics/AbcSize:
Max: 20

# Offense count: 1
# Configuration parameters: CountComments.
Metrics/ClassLength:
Max: 120

# Offense count: 1
Metrics/CyclomaticComplexity:
Max: 8

# Offense count: 3
# Configuration parameters: AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, IgnoredPatterns.
# URISchemes: http, https
Metrics/LineLength:
Max: 100

# Offense count: 3
# Configuration parameters: CountComments.
Metrics/MethodLength:
Max: 25

# 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:
Exclude:
- 'lib/jekyll-contentful-data-import.rb'
2 changes: 2 additions & 0 deletions CHANGELOG.md
Expand Up @@ -9,6 +9,8 @@
* Added more metadata to `sys` attributes in entries
* Added `sys` attributes for serialized assets

### Fixed
* Updated code to match style guide

### v1.4.3
* Added use of `map_field` without localizing for custom mappers that map `sys` properties when `locale='*'` is being sent.
Expand Down
13 changes: 9 additions & 4 deletions Guardfile
@@ -1,5 +1,10 @@
guard :rspec, cmd: 'rspec --format documentation --color' do
watch(%r{^spec/.+_spec\.rb$})
watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" }
watch('spec/spec_helper.rb') { "spec" }
group :green_red_refactor, halt_on_fail: true do
guard :rspec, cmd: 'rspec --format documentation --color' do
watch(%r{^spec/.+_spec\.rb$})
watch(%r{^lib/.+\.rb$}) { "spec" }
watch('spec/spec_helper.rb') { "spec" }
end

guard :rubocop, cmd: "rubocop", cli: 'lib' do
end
end
2 changes: 2 additions & 0 deletions jekyll-contentful.gemspec
Expand Up @@ -27,11 +27,13 @@ Gem::Specification.new do |s|
s.add_development_dependency 'rubygems-tasks', '~> 0.2'
s.add_development_dependency "guard"
s.add_development_dependency "guard-rspec"
s.add_development_dependency 'guard-rubocop'
s.add_development_dependency "bundler", "~> 1.6"
s.add_development_dependency "rake"
s.add_development_dependency "rspec", "~> 3.0"
s.add_development_dependency "vcr"
s.add_development_dependency "webmock"
s.add_development_dependency "pry"
s.add_development_dependency "simplecov"
s.add_development_dependency "rubocop"
end
2 changes: 1 addition & 1 deletion lib/jekyll-contentful-data-import.rb
@@ -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
34 changes: 27 additions & 7 deletions lib/jekyll-contentful-data-import/data_exporter.rb
Expand Up @@ -2,10 +2,13 @@

module Jekyll
module Contentful
# Data Exporter Class
#
# Serializes Contentful data into YAML files
class DataExporter
DATA_FOLDER = '_data'
CONTENTFUL_FOLDER = 'contentful'
SPACES_FOLDER = 'spaces'
DATA_FOLDER = '_data'.freeze
CONTENTFUL_FOLDER = 'contentful'.freeze
SPACES_FOLDER = 'spaces'.freeze

attr_reader :name, :entries, :config

Expand All @@ -19,20 +22,37 @@ def run
setup_directory

File.open(destination_file, 'w') do |file|
file.write(::Jekyll::Contentful::Serializer.new(entries, config).to_yaml)
file.write(
::Jekyll::Contentful::Serializer.new(
entries,
config
).to_yaml
)
end
end

def base_directory
directory = File.expand_path(Dir.pwd)
directory = File.join(directory, config['base_path']) if config.key?('base_path')
if config.key?('base_path')
directory = File.join(
directory,
config['base_path']
)
end

directory
end

def destination_directory
destination_dir = File.join(base_directory, DATA_FOLDER, CONTENTFUL_FOLDER, SPACES_FOLDER)
destination_dir = File.join(base_directory, DATA_FOLDER, config['destination']) if config.key?('destination')
destination_dir = File.join(
base_directory, DATA_FOLDER,
CONTENTFUL_FOLDER, SPACES_FOLDER
)
if config.key?('destination')
destination_dir = File.join(
base_directory, DATA_FOLDER, config['destination']
)
end

destination_dir
end
Expand Down
25 changes: 18 additions & 7 deletions lib/jekyll-contentful-data-import/importer.rb
Expand Up @@ -3,6 +3,9 @@

module Jekyll
module Contentful
# Importer class
#
# Entry fetching logic
class Importer
attr_reader :config

Expand All @@ -18,22 +21,26 @@ def run
client_options(options.fetch('client_options', {}))
)

Jekyll::Contentful::DataExporter.new(
name,
get_entries(space_client, options),
options
).run
export_data(name, space_client, options)
end
end

def export_data(name, space_client, options)
Jekyll::Contentful::DataExporter.new(
name,
get_entries(space_client, options),
options
).run
end

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

def spaces
config['spaces'].map { |space_data| space_data.first }
config['spaces'].map(&:first)
end

def get_entries(space_client, options)
Expand Down Expand Up @@ -70,7 +77,11 @@ def client(space, access_token, options = {})
private

def client_options(options)
options = options.each_with_object({}){|(k,v), memo| memo[k.to_sym] = v; memo}
options = options.each_with_object({}) do |(k, v), memo|
memo[k.to_sym] = v
memo
end

options.delete(:dynamic_entries)
options.delete(:raise_errors)
options
Expand Down
24 changes: 16 additions & 8 deletions lib/jekyll-contentful-data-import/mappers/base.rb
Expand Up @@ -2,7 +2,11 @@

module Jekyll
module Contentful
# Mappers module
module Mappers
# Base Mapper Class
#
# Logic for mapping entries into simplified serialized representations
class Base
def self.mapper_for(entry, config)
ct = entry.content_type
Expand All @@ -25,7 +29,7 @@ def initialize(entry, config)

def map_entry_metadata
content_type = entry.sys.fetch(:content_type, nil)
return {
{
'id' => entry.sys.fetch(:id, nil),
'created_at' => entry.sys.fetch(:created_at, nil),
'updated_at' => entry.sys.fetch(:updated_at, nil),
Expand All @@ -36,17 +40,17 @@ def map_entry_metadata
def map
result = { 'sys' => map_entry_metadata }

fields = has_multiple_locales? ? entry.fields_with_locales : entry.fields
fields = multiple_locales? ? entry.fields_with_locales : entry.fields

fields.each do |k, v|
name, value = map_field(k, v, has_multiple_locales?)
name, value = map_field(k, v, multiple_locales?)
result[name] = value
end

result
end

def has_multiple_locales?
def multiple_locales?
config.fetch('cda_query', {}).fetch('locale', nil) == '*'
end

Expand All @@ -62,7 +66,7 @@ def map_field(field_name, field_value, multiple_locales = false)
value_mapping = map_value(field_value)
end

return field_name.to_s, value_mapping
[field_name.to_s, value_mapping]
end

def map_value(value, locale = nil)
Expand Down Expand Up @@ -91,7 +95,7 @@ def map_value(value, locale = nil)
end

def map_asset_metadata(asset)
return {
{
'id' => asset.id,
'created_at' => asset.sys.fetch(:created_at, nil),
'updated_at' => asset.sys.fetch(:updated_at, nil)
Expand Down Expand Up @@ -135,11 +139,15 @@ def map_location(location)
end

def map_link(link)
{'sys' => {'id' => link.id}}
{
'sys' => {
'id' => link.id
}
}
end

def map_array(array, locale)
array.map {|element| map_value(element, locale)}
array.map { |element| map_value(element, locale) }
end
end
end
Expand Down
3 changes: 3 additions & 0 deletions lib/jekyll-contentful-data-import/serializer.rb
Expand Up @@ -3,6 +3,9 @@

module Jekyll
module Contentful
# Serializer class
#
# Transforms the serialized entries to YAML
class Serializer
attr_reader :entries, :config

Expand Down
2 changes: 1 addition & 1 deletion lib/jekyll-contentful-data-import/version.rb
@@ -1,5 +1,5 @@
module Jekyll
module Contentful
VERSION = "1.4.3"
VERSION = '1.4.3'.freeze
end
end
31 changes: 19 additions & 12 deletions lib/jekyll/commands/contentful.rb
@@ -1,44 +1,51 @@
require 'jekyll-contentful-data-import/importer'

module Jekyll
# Module for Jekyll Commands
module Commands
# jekyll contentful Command
class Contentful < Command
def self.init_with_program(prog)
prog.command(:contentful) do |c|
c.syntax 'contentful [OPTIONS]'
c.description 'Imports data from Contentful'

options.each {|opt| c.option(*opt) }
options.each { |opt| c.option(*opt) }

add_build_options(c)

c.action do |args, options|
jekyll_options = configuration_from_options(options)
contentful_config = jekyll_options['contentful']
process args, options, contentful_config
end
command_action(c)
end
end

def self.options
[
['rebuild', '-r', '--rebuild', 'Rebuild Jekyll Site after fetching data'],
[
'rebuild', '-r', '--rebuild',
'Rebuild Jekyll Site after fetching data'
]
]
end

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
end
end

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

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

Jekyll.logger.info 'Contentful import finished'

if options['rebuild']
Jekyll.logger.info 'Starting Jekyll Rebuild'
return unless options['rebuild']

Jekyll::Commands::Build.process(options)
end
Jekyll.logger.info 'Starting Jekyll Rebuild'
Jekyll::Commands::Build.process(options)
end
end
end
Expand Down

0 comments on commit 1ee4175

Please sign in to comment.