class Latergram::Publications
Constants
- ENDPOINT
Attributes
api_key[R]
api_url[R]
Public Class Methods
new(api_key, api_url:)
click to toggle source
# File lib/latergram/publications.rb, line 12 def initialize(api_key, api_url:) @api_key = api_key @api_url = api_url end
Public Instance Methods
all(page: 1, per_page: 10)
click to toggle source
# File lib/latergram/publications.rb, line 17 def all(page: 1, per_page: 10) response = requester.get(ENDPOINT, page: page, per_page: per_page) raise Latergram::Error, response.body unless response.status == 200 JSON.parse(response.body) end
create(text:, images:)
click to toggle source
# File lib/latergram/publications.rb, line 33 def create(text:, images:) check_images(images) parameters = { publication: { text: text, images: images }.compact } response = requester.post(ENDPOINT, parameters) raise Latergram::Error, response.body unless response.status == 200 JSON.parse(response.body) end
destroy(publication_id)
click to toggle source
# File lib/latergram/publications.rb, line 53 def destroy(publication_id) response = requester.delete(ENDPOINT + "/#{publication_id}") raise Latergram::Error, response.body unless response.status == 200 JSON.parse(response.body) end
find(publication_id)
click to toggle source
# File lib/latergram/publications.rb, line 25 def find(publication_id) response = requester.get(ENDPOINT + "/#{publication_id}") raise Latergram::Error, response.body unless response.status == 200 JSON.parse(response.body) end
update(publication_id, text: nil)
click to toggle source
# File lib/latergram/publications.rb, line 44 def update(publication_id, text: nil) parameters = { publication: { text: text }.compact } response = requester.put(ENDPOINT + "/#{publication_id}", parameters) raise Latergram::Error, response.body unless response.status == 200 JSON.parse(response.body) end
Private Instance Methods
check_images(images)
click to toggle source
# File lib/latergram/publications.rb, line 63 def check_images(images) raise(Latergram::Error, 'images must be array') unless images.is_a?(Array) raise(Latergram::Error, 'images cannot be empty') if images.empty? return if images.all? { |image| image.key?(:data) && image.key?(:filename) && image.key?(:content_type) } raise( Latergram::Error, 'images must contain the following attributes: data (base64 encoded image), filename and content_type' ) end
requester()
click to toggle source
# File lib/latergram/publications.rb, line 75 def requester @requester ||= Requester.new(api_key, api_url) end