lib/wcc/contentful.rb in wcc-contentful-1.0.8 vs lib/wcc/contentful.rb in wcc-contentful-1.1.0

- old
+ new

@@ -32,11 +32,14 @@ attr_reader :initialized # Gets the current configuration, after calling WCC::Contentful.configure attr_reader :configuration - attr_reader :types + def types + ActiveSupport::Deprecation.warn('Use WCC::Contentful::Model.schema instead') + WCC::Contentful::Model.schema + end # Gets all queryable locales. # Reserved for future use. def locales @locales ||= { 'en-US' => {} }.freeze @@ -89,45 +92,48 @@ WCC::Contentful.logger.warn("Unable to download schema from management API - #{e.message}") end end - @content_types = + content_types = begin if File.exist?(configuration.schema_file) JSON.parse(File.read(configuration.schema_file))['contentTypes'] end rescue JSON::ParserError WCC::Contentful.logger.warn("Schema file invalid, ignoring it: #{configuration.schema_file}") nil end - if !@content_types && %i[if_possible never].include?(configuration.update_schema_file) + if !content_types && %i[if_possible never].include?(configuration.update_schema_file) # Final fallback - try to grab content types from CDN. We can't update the file # because the CDN doesn't have all the field validation info, but we can at least # build the WCC::Contentful::Model instances. client = Services.instance.management_client || Services.instance.client begin - @content_types = client.content_types(limit: 1000).items if client + content_types = client.content_types(limit: 1000).items if client rescue WCC::Contentful::SimpleClient::ApiError => e # indicates bad credentials WCC::Contentful.logger.warn("Unable to load content types from API - #{e.message}") end end - unless @content_types + unless content_types raise InitializationError, 'Unable to load content types from schema file or API!' \ ' Check your access token and space ID.' end - indexer = ContentTypeIndexer.from_json_schema(@content_types) - @types = indexer.types + # Set the schema on the default WCC::Contentful::Model + WCC::Contentful::Model.configure( + configuration, + schema: WCC::Contentful::ContentTypeIndexer.from_json_schema(content_types).types, + services: WCC::Contentful::Services.instance + ) # Drop an initial sync WCC::Contentful::SyncEngine::Job.perform_later if defined?(WCC::Contentful::SyncEngine::Job) - WCC::Contentful::ModelBuilder.new(@types).build_models @configuration = @configuration.freeze @initialized = true end end