Class: Jekyll::Contentful::Importer
- Inherits:
-
Object
- Object
- Jekyll::Contentful::Importer
- Defined in:
- lib/jekyll-contentful-data-import/importer.rb
Instance Attribute Summary collapse
-
#config ⇒ Object
readonly
Returns the value of attribute config.
Instance Method Summary collapse
- #client(space, access_token, 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.
9 10 11 |
# File 'lib/jekyll-contentful-data-import/importer.rb', line 9 def initialize(config) @config = config end |
Instance Attribute Details
#config ⇒ Object (readonly)
Returns the value of attribute config.
7 8 9 |
# File 'lib/jekyll-contentful-data-import/importer.rb', line 7 def config @config end |
Instance Method Details
#client(space, access_token, options = {}) ⇒ Object
59 60 61 62 63 64 65 66 67 68 |
# File 'lib/jekyll-contentful-data-import/importer.rb', line 59 def client(space, access_token, = {}) = { space: space, access_token: access_token, dynamic_entries: :auto, raise_errors: true }.merge() ::Contentful::Client.new() end |
#get_entries(space_client, options) ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/jekyll-contentful-data-import/importer.rb', line 39 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
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/jekyll-contentful-data-import/importer.rb', line 13 def run spaces.each do |name, | space_client = client( value_for(, 'space'), value_for(, 'access_token'), (.fetch('client_options', {})) ) Jekyll::Contentful::DataExporter.new( name, get_entries(space_client, ), ).run end end |
#spaces ⇒ Object
35 36 37 |
# File 'lib/jekyll-contentful-data-import/importer.rb', line 35 def spaces config['spaces'].map { |space_data| space_data.first } end |
#value_for(options, key) ⇒ Object
29 30 31 32 33 |
# File 'lib/jekyll-contentful-data-import/importer.rb', line 29 def value_for(, key) potential_value = [key] return ENV[potential_value.gsub('ENV_', '')] if potential_value.start_with?('ENV_') potential_value end |