Class: Fopost::Resources::Posts

Inherits:
Base
  • Object
show all
Defined in:
lib/fopost/resources/posts.rb

Overview

client.posts — create, schedule, publish, and inspect posts.

Instance Method Summary collapse

Methods inherited from Base

#initialize

Constructor Details

This class inherits a constructor from Fopost::Resources::Base

Instance Method Details

#cancel(post_id) ⇒ Object



124
125
126
# File 'lib/fopost/resources/posts.rb', line 124

def cancel(post_id)
  as_hash(unwrap(http.post("/posts/#{post_id}/cancel")))
end

#create(workspace_id:, content:, accounts: [], status: 'draft', schedule_at: nil, labels: nil, title: nil, summary: nil, content_type: nil, settings: nil, **extra) ⇒ Object

Create a draft or a scheduled post.

status is "draft" or "scheduled"; a scheduled post needs schedule_at. To send a post out now, create it and call #publish.



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/fopost/resources/posts.rb', line 70

def create(workspace_id:, content:, accounts: [], status: 'draft', schedule_at: nil,
           labels: nil, title: nil, summary: nil, content_type: nil, settings: nil, **extra)
  body = {
    'workspace_id' => workspace_id,
    'status' => status,
    'content' => normalize_content(content),
    'accounts' => normalize_accounts(accounts)
  }
  body.merge!(
    compact_nil(
      'schedule_at' => iso8601(schedule_at),
      'labels' => labels&.to_a,
      'title' => title,
      'summary' => summary,
      'content_type' => content_type,
      'settings' => settings
    )
  )
  body.merge!(stringify(extra))

  Post.new(unwrap(http.post('/posts', body)))
end

#delete(post_id) ⇒ Object



114
115
116
117
# File 'lib/fopost/resources/posts.rb', line 114

def delete(post_id)
  http.delete("/posts/#{post_id}")
  nil
end

#deliveries(post_id) ⇒ Object



138
139
140
# File 'lib/fopost/resources/posts.rb', line 138

def deliveries(post_id)
  parse_list(Delivery, unwrap(http.get("/posts/#{post_id}/deliveries")))
end

#each(workspace_id: nil, status: nil, search: nil, per_page: 30, sort: nil, start_page: 1, &block) ⇒ Object

Walk every matching post, fetching one page at a time.



27
28
29
30
31
32
33
34
35
36
37
# File 'lib/fopost/resources/posts.rb', line 27

def each(workspace_id: nil, status: nil, search: nil, per_page: 30, sort: nil, start_page: 1, &block)
  unless block
    return enum_for(:each, workspace_id: workspace_id, status: status, search: search,
                           per_page: per_page, sort: sort, start_page: start_page)
  end

  each_page(workspace_id: workspace_id, status: status, search: search,
            per_page: per_page, sort: sort, start_page: start_page) do |page|
    page.items.each(&block)
  end
end

#each_page(workspace_id: nil, status: nil, search: nil, per_page: 30, sort: nil, start_page: 1) ⇒ Object

The same walk as #each, but yields whole pages so the meta stays reachable.



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/fopost/resources/posts.rb', line 40

def each_page(workspace_id: nil, status: nil, search: nil, per_page: 30, sort: nil, start_page: 1)
  unless block_given?
    return enum_for(:each_page, workspace_id: workspace_id, status: status, search: search,
                                per_page: per_page, sort: sort, start_page: start_page)
  end

  page_number = start_page
  loop do
    page = list(workspace_id: workspace_id, status: status, search: search,
                page: page_number, per_page: per_page, sort: sort)
    return if page.items.empty?

    yield page

    last_page = page.meta.last_page
    return if last_page && page_number >= last_page
    return if last_page.nil? && page.items.size < per_page

    page_number += 1
  end
end

#get(post_id) ⇒ Object



62
63
64
# File 'lib/fopost/resources/posts.rb', line 62

def get(post_id)
  Post.new(unwrap(http.get("/posts/#{post_id}")))
end

#list(workspace_id: nil, status: nil, search: nil, page: 1, per_page: 30, sort: nil) ⇒ Object

One page of posts. The result is Enumerable over its items.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/fopost/resources/posts.rb', line 8

def list(workspace_id: nil, status: nil, search: nil, page: 1, per_page: 30, sort: nil)
  body = http.get(
    '/posts',
    {
      'workspace_id' => workspace_id,
      'status' => status,
      'search' => search,
      'page' => page,
      'per_page' => per_page,
      'sort' => sort
    }
  )

  items = parse_list(Post, body.is_a?(Hash) ? body['data'] : body)
  raw_meta = body.is_a?(Hash) ? body['meta'] : nil
  Page.new(items: items, meta: PageMeta.new(raw_meta.is_a?(Hash) ? raw_meta : {}))
end

#preflight(post_id) ⇒ Object

Per-account blockers and advisory content signals, without publishing.



134
135
136
# File 'lib/fopost/resources/posts.rb', line 134

def preflight(post_id)
  as_hash(unwrap(http.post("/posts/#{post_id}/preflight")))
end

#publish(post_id) ⇒ Object

Queue the post for immediate delivery to its accounts.



120
121
122
# File 'lib/fopost/resources/posts.rb', line 120

def publish(post_id)
  as_hash(unwrap(http.post("/posts/#{post_id}/publish")))
end

#retry(post_id) ⇒ Object

Retry the deliveries that failed, leaving the successful ones alone.



129
130
131
# File 'lib/fopost/resources/posts.rb', line 129

def retry(post_id)
  as_hash(unwrap(http.post("/posts/#{post_id}/retry")))
end

#update(post_id, content: UNSET, accounts: UNSET, status: UNSET, schedule_at: UNSET, labels: UNSET, title: UNSET, summary: UNSET, content_type: UNSET, settings: UNSET, **extra) ⇒ Object

Partial update — only the fields you pass are sent. Pass an explicit nil to clear a field.



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/fopost/resources/posts.rb', line 95

def update(post_id, content: UNSET, accounts: UNSET, status: UNSET, schedule_at: UNSET,
           labels: UNSET, title: UNSET, summary: UNSET, content_type: UNSET,
           settings: UNSET, **extra)
  body = compact_unset(
    'content' => UNSET.equal?(content) ? UNSET : normalize_content(content),
    'accounts' => UNSET.equal?(accounts) ? UNSET : normalize_accounts(accounts),
    'status' => status,
    'schedule_at' => UNSET.equal?(schedule_at) ? UNSET : iso8601(schedule_at),
    'labels' => UNSET.equal?(labels) ? UNSET : labels&.to_a,
    'title' => title,
    'summary' => summary,
    'content_type' => content_type,
    'settings' => settings
  )
  body.merge!(stringify(extra))

  Post.new(unwrap(http.put("/posts/#{post_id}", body)))
end