Sha256: 35b04b1a404581b5413a0ca464da17d3f30d15980a3e310a5cb3908a8e663453

Contents?: true

Size: 950 Bytes

Versions: 2

Compression:

Stored size: 950 Bytes

Contents

module AppleNews
  class Document
    module Persistence
      extend ActiveSupport::Concern

      included do
        def save!
          request = AppleNews::Request::Post.new(endpoint_url)
          request.fields = {
            'metadata' => metadata_field,
            'article.json' => article_json
          }.merge(@files)

          resp = request.call
          article.update_with_data(resp['data'].delete('document'))
        end

        private

        def endpoint_url
          if article.persisted?
            "/articles/#{id}"
          else
            "/channels/#{AppleNews.config.channel_id}/articles"
          end
        end

        def metadata_field
          JSON.dump({ data: @metadata })
        end

        def article_json
          UploadIO.new(
            StringIO.new(JSON.dump(article.as_json)),
            "application/json",
            "article.json"
          )
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
apple-news-0.1.1 lib/apple-news/document/persistence.rb
apple-news-0.1.0 lib/apple-news/document/persistence.rb