Sha256: d3ed9ddd3de3ad9bd7470d3f783a003d9a24e1fbc4904fade566bad49a345355

Contents?: true

Size: 1.28 KB

Versions: 5

Compression:

Stored size: 1.28 KB

Contents

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, :share_url, :state
    attr_accessor :document
    def_delegator :@document, :title

    def initialize(id = nil, data = {})
      super(data)

      @resource_path = "/articles"
      @id = id

      document = (data[:document] || data['document'])
      @document = document.is_a?(AppleNews::Document) ? document : Document.new(document)
      @files = {}

      # These are read-only properties that are not submitted to the API
      @share_url = data['shareUrl']
      @state = data['state']

      hydrate! if !id.nil? && data.keys.size == 0
    end

    def reload
      return false if id.nil?
      hydrate!
    end

    private

    def hydrate!
      data = fetch_data['data']
      
      # Some special properties that need to be manually set.
      @document = Document.new(data.delete('document'))
      @share_url = data.delete('shareUrl')
      @state = data.delete('state')

      load_properties(data)
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
apple-news-0.4.2 lib/apple-news/article.rb
apple-news-0.4.1 lib/apple-news/article.rb
apple-news-0.4.0 lib/apple-news/article.rb
apple-news-0.3.1 lib/apple-news/article.rb
apple-news-0.3.0 lib/apple-news/article.rb