Class: Jekyll::Contentful::Importer
- Inherits:
-
Object
- Object
- Jekyll::Contentful::Importer
- Defined in:
- lib/jekyll-contentful-data-import/importer.rb
Overview
Importer class
Entry fetching logic
Instance Attribute Summary collapse
-
#config ⇒ Object
readonly
Returns the value of attribute config.
Instance Method Summary collapse
- #client(space, access_token, options = {}) ⇒ Object
- #export_data(name, space_client, options) ⇒ Object
- #export_data_multiple_files(name, entries, options) ⇒ Object
- #export_data_single_file(name, entries, options) ⇒ Object
- #get_entries(space_client, options) ⇒ Object
-
#initialize(config) ⇒ Importer
constructor
A new instance of Importer.
- #run ⇒ Object
- #spaces ⇒ Object
- #value_for(options, key) ⇒ Object
Constructor Details
#initialize(config) ⇒ Importer
Returns a new instance of Importer.
13 14 15 |
# File 'lib/jekyll-contentful-data-import/importer.rb', line 13 def initialize(config) @config = config end |
Instance Attribute Details
#config ⇒ Object (readonly)
Returns the value of attribute config.
11 12 13 |
# File 'lib/jekyll-contentful-data-import/importer.rb', line 11 def config @config end |
Instance Method Details
#client(space, access_token, options = {}) ⇒ Object
93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/jekyll-contentful-data-import/importer.rb', line 93 def client(space, access_token, = {}) = { space: space, access_token: access_token, dynamic_entries: :auto, raise_errors: true, integration_name: 'jekyll', integration_version: Jekyll::Contentful::VERSION }.merge() ::Contentful::Client.new() end |
#export_data(name, space_client, options) ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/jekyll-contentful-data-import/importer.rb', line 29 def export_data(name, space_client, ) entries = get_entries(space_client, ) if .fetch('individual_entry_files', false) export_data_multiple_files( name, entries, ) else export_data_single_file( name, entries, ) end end |
#export_data_multiple_files(name, entries, options) ⇒ Object
55 56 57 58 59 60 61 |
# File 'lib/jekyll-contentful-data-import/importer.rb', line 55 def export_data_multiple_files(name, entries, ) Jekyll::Contentful::MultiFileDataExporter.new( name, entries, ).run end |
#export_data_single_file(name, entries, options) ⇒ Object
47 48 49 50 51 52 53 |
# File 'lib/jekyll-contentful-data-import/importer.rb', line 47 def export_data_single_file(name, entries, ) Jekyll::Contentful::SingleFileDataExporter.new( name, entries, ).run end |
#get_entries(space_client, options) ⇒ Object
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/jekyll-contentful-data-import/importer.rb', line 73 def get_entries(space_client, ) cda_query = .fetch('cda_query', {}) return space_client.entries(cda_query) unless .fetch('all_entries', false) all = [] query = cda_query.clone query[:order] = 'sys.createdAt' unless query.key?(:order) num_entries = space_client.entries(limit: 1).total page_size = .fetch('all_entries_page_size', 1000) ((num_entries / page_size) + 1).times do |i| query[:limit] = page_size query[:skip] = i * page_size page = space_client.entries(query) page.each { |entry| all << entry } end all end |
#run ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/jekyll-contentful-data-import/importer.rb', line 17 def run spaces.each do |name, | space_client = client( value_for(, 'space'), value_for(, 'access_token'), (.fetch('client_options', {})) ) export_data(name, space_client, ) end end |
#spaces ⇒ Object
69 70 71 |
# File 'lib/jekyll-contentful-data-import/importer.rb', line 69 def spaces config['spaces'].map(&:first) end |
#value_for(options, key) ⇒ Object
63 64 65 66 67 |
# File 'lib/jekyll-contentful-data-import/importer.rb', line 63 def value_for(, key) potential_value = [key] return ENV[potential_value.gsub('ENV_', '')] if potential_value.start_with?('ENV_') potential_value end |