Class: Socialfred::Publications

Inherits:
Object
  • Object
show all
Defined in:
lib/socialfred/publications.rb

Constant Summary collapse

ENDPOINT =
'publications'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_key, api_url:) ⇒ Publications

Returns a new instance of Publications.



12
13
14
15
# File 'lib/socialfred/publications.rb', line 12

def initialize(api_key, api_url:)
  @api_key = api_key
  @api_url = api_url
end

Instance Attribute Details

#api_keyObject (readonly)

Returns the value of attribute api_key.



8
9
10
# File 'lib/socialfred/publications.rb', line 8

def api_key
  @api_key
end

#api_urlObject (readonly)

Returns the value of attribute api_url.



8
9
10
# File 'lib/socialfred/publications.rb', line 8

def api_url
  @api_url
end

Instance Method Details

#all(page: 1, per_page: 10) ⇒ Object

Raises:



17
18
19
20
21
22
23
# File 'lib/socialfred/publications.rb', line 17

def all(page: 1, per_page: 10)
  response = requester.get(ENDPOINT, page: page, per_page: per_page)

  raise Socialfred::Error, response.body unless response.status == 200

  JSON.parse(response.body)
end

#create(publish_at: nil, text:, images: nil, options: nil) ⇒ Object

Raises:



33
34
35
36
37
38
39
40
41
42
43
# File 'lib/socialfred/publications.rb', line 33

def create(publish_at: nil, text:, images: nil, options: nil)
  check_images(images) if images

  publish_at = Time.parse(publish_at.to_s).iso8601 if publish_at
  parameters = { publication: { published_at: publish_at, text: text, images: images, options: options }.compact }
  response = requester.post(ENDPOINT, parameters)

  raise Socialfred::Error, response.body unless response.status == 200

  JSON.parse(response.body)
end

#destroy(publication_id) ⇒ Object

Raises:



57
58
59
60
61
62
63
# File 'lib/socialfred/publications.rb', line 57

def destroy(publication_id)
  response = requester.delete(ENDPOINT + "/#{publication_id}")

  raise Socialfred::Error, response.body unless response.status == 200

  JSON.parse(response.body)
end

#find(publication_id) ⇒ Object

Raises:



25
26
27
28
29
30
31
# File 'lib/socialfred/publications.rb', line 25

def find(publication_id)
  response = requester.get(ENDPOINT + "/#{publication_id}")

  raise Socialfred::Error, response.body unless response.status == 200

  JSON.parse(response.body)
end

#update(publication_id, publish_at: nil, text: nil, images: nil, options: nil) ⇒ Object

Raises:



45
46
47
48
49
50
51
52
53
54
55
# File 'lib/socialfred/publications.rb', line 45

def update(publication_id, publish_at: nil, text: nil, images: nil, options: nil)
  check_images(images) if images

  publish_at = Time.parse(publish_at.to_s).iso8601 if publish_at
  parameters = { publication: { published_at: publish_at, text: text, images: images, options: options }.compact }
  response = requester.put(ENDPOINT + "/#{publication_id}", parameters)

  raise Socialfred::Error, response.body unless response.status == 200

  JSON.parse(response.body)
end