lib/apple-news/article.rb in apple-news-0.1.1 vs lib/apple-news/article.rb in apple-news-0.2.0

- old
+ new

@@ -1,33 +1,45 @@ +require 'apple-news/article/attachments' +require 'apple-news/article/persistence' + module AppleNews class Article + extend Forwardable + + include Attachments + include Persistence include Resource include Properties + optional_properties :is_sponsored, :is_preview, :accessory_text, :revision + optional_property :links, {} + attr_reader :id + attr_accessor :document + def_delegator :@document, :title - required_properties :identifier, :title, :layout - required_property :components, [] - required_property :component_text_styles, {} + def initialize(id = nil, data = {}) + super(data) - required_property :version, "1.1" - required_property :language, "en" + @resource_path = "/articles" + @id = id - optional_properties :advertising_settings, :subtitle, :metadata, :document_style, - :text_styles, :component_layouts, :component_styles + document = (data[:document] || data['document']) + @document = document.is_a?(AppleNews::Document) ? document : Document.new(document) + @files = {} - def initialize(id = nil, opts = nil) - super(opts) + # These are read-only properties that are not submitted to the API + @share_url = data['shareUrl'] + @state = data['state'] - @id = id - @url = "/articles/#{id}" - @metadata = Metadata.new((opts || {}).fetch(:metadata, {})) - - hydrate! if !id.nil? && opts.nil? + hydrate! if !id.nil? && data.keys.size == 0 end - def persisted? - !@id.nil? + private + + def hydrate! + data = fetch_data['data'] + @document = Document.new(data.delete('document')) + load_properties(data) end - alias_method :saved?, :persisted? end end