Class: Latergram::Publications

Inherits:
Object
  • Object
show all
Defined in:
lib/latergram/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/latergram/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/latergram/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/latergram/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/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:) ⇒ Object

Raises:



33
34
35
36
37
38
39
40
41
42
# 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) ⇒ Object

Raises:



53
54
55
56
57
58
59
# 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) ⇒ Object

Raises:



25
26
27
28
29
30
31
# 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) ⇒ Object

Raises:



44
45
46
47
48
49
50
51
# 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