Class: WCC::Contentful::DownloadsSchema
- Inherits:
-
Object
- Object
- WCC::Contentful::DownloadsSchema
- Defined in:
- lib/wcc/contentful/downloads_schema.rb
Class Method Summary collapse
Instance Method Summary collapse
- #call ⇒ Object
- #content_types ⇒ Object
- #editor_interfaces ⇒ Object
-
#initialize(file = nil, management_client: nil) ⇒ DownloadsSchema
constructor
A new instance of DownloadsSchema.
- #locales ⇒ Object
- #needs_update? ⇒ Boolean
- #update! ⇒ Object
Constructor Details
#initialize(file = nil, management_client: nil) ⇒ DownloadsSchema
Returns a new instance of DownloadsSchema.
10 11 12 13 14 15 |
# File 'lib/wcc/contentful/downloads_schema.rb', line 10 def initialize(file = nil, management_client: nil) @client = management_client || WCC::Contentful::Services.instance.management_client @file = file || WCC::Contentful.configuration&.schema_file raise ArgumentError, 'Please configure your management token' unless @client raise ArgumentError, 'Please pass filename or call WCC::Contentful.configure' unless @file end |
Class Method Details
.call(file = nil, management_client: nil) ⇒ Object
6 7 8 |
# File 'lib/wcc/contentful/downloads_schema.rb', line 6 def self.call(file = nil, management_client: nil) new(file, management_client: management_client).call end |
Instance Method Details
#call ⇒ Object
17 18 19 20 21 |
# File 'lib/wcc/contentful/downloads_schema.rb', line 17 def call return unless needs_update? update! end |
#content_types ⇒ Object
56 57 58 59 60 61 62 |
# File 'lib/wcc/contentful/downloads_schema.rb', line 56 def content_types @content_types ||= @client.content_types(limit: 1000) .items .map { |ct| strip_sys(ct) } .sort_by { |ct| ct.dig('sys', 'id') } end |
#editor_interfaces ⇒ Object
64 65 66 67 68 69 70 |
# File 'lib/wcc/contentful/downloads_schema.rb', line 64 def editor_interfaces @editor_interfaces ||= content_types .map { |ct| @client.editor_interface(ct.dig('sys', 'id')).raw } .map { |i| sort_controls(strip_sys(i)) } .sort_by { |i| i.dig('sys', 'contentType', 'sys', 'id') } end |
#locales ⇒ Object
72 73 74 75 76 77 78 |
# File 'lib/wcc/contentful/downloads_schema.rb', line 72 def locales @locales ||= @client.locales(limit: 1000) .items .map { |l| strip_sys(l) } .sort_by { |l| l.dig('sys', 'code') } end |
#needs_update? ⇒ Boolean
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/wcc/contentful/downloads_schema.rb', line 33 def needs_update? return true unless File.exist?(@file) contents = begin JSON.parse(File.read(@file)) rescue JSON::ParserError return true # rubocop:disable Lint/NoReturnInBeginEndBlocks end existing_cts = contents['contentTypes'].sort_by { |ct| ct.dig('sys', 'id') } return true unless content_types.count == existing_cts.count return true unless deep_contains_all(content_types, existing_cts) existing_eis = contents['editorInterfaces'].sort_by { |i| i.dig('sys', 'contentType', 'sys', 'id') } return true unless editor_interfaces.count == existing_eis.count return true unless deep_contains_all(editor_interfaces, existing_eis) existing_locales = contents['locales'].sort_by { |i| i.dig('sys', 'contentType', 'sys', 'id') } return true unless locales.count == existing_locales.count return true unless deep_contains_all(locales, existing_locales) end |
#update! ⇒ Object
23 24 25 26 27 28 29 30 31 |
# File 'lib/wcc/contentful/downloads_schema.rb', line 23 def update! FileUtils.mkdir_p(File.dirname(@file)) File.write(@file, format_json({ 'contentTypes' => content_types, 'editorInterfaces' => editor_interfaces, 'locales' => locales })) end |