Class: PatchRetention::CalendarItems

Inherits:
Object
  • Object
show all
Defined in:
lib/patch_retention/calendar_items.rb

Class Method Summary collapse

Class Method Details

.all(contact_id: nil, min_id: nil, max_id: nil, id: nil, date_range: nil, limit: nil, offset: nil, config: nil) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/patch_retention/calendar_items.rb', line 68

def all(contact_id: nil, min_id: nil, max_id: nil, id: nil, date_range: nil, limit: nil, offset: nil, config: nil)
  params = {}
  params[:contact_id] = contact_id if contact_id
  params[:min_id] = min_id if min_id
  params[:max_id] = max_id if max_id
  params[:id] = id if id
  params[:date_range] = date_range if date_range
  params[:limit] = limit if limit
  params[:offset] = offset if offset

  response = PatchRetention.connection(config).get("/v2/calendar_items", params)

  JSON.parse(response.body)
rescue Faraday::Error => e
  raise Error, "Failed to retrieve calendar items: #{e.message}"
end

.create(contact_id:, title:, start_at:, end_at:, run_start_at: nil, run_end_at: nil, data: nil, time_occurred: nil, skip_triggers: false, tags: nil, config: nil) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/patch_retention/calendar_items.rb', line 6

def create(contact_id:, title:, start_at:, end_at:, run_start_at: nil, run_end_at: nil,
  data: nil, time_occurred: nil, skip_triggers: false, tags: nil, config: nil)
  payload = {
    contact_id: contact_id,
    title: title,
    start_at: start_at,
    end_at: end_at,
  }
  payload[:run_start_at] = run_start_at if run_start_at
  payload[:run_end_at] = run_end_at if run_end_at
  payload[:data] = data if data
  payload[:time_occurred] = time_occurred if time_occurred
  payload[:skip_triggers] = skip_triggers unless skip_triggers.nil?
  payload[:tags] = tags if tags

  response = PatchRetention.connection(config).post("/v2/calendar_items") do |req|
    req.body = payload.to_json
    req.headers["Content-Type"] = "application/json"
  end

  JSON.parse(response.body)
rescue Faraday::Error => e
  raise Error, "Failed to create calendar item: #{e.message}"
end

.delete(calendar_item_id:, config: nil) ⇒ Object



60
61
62
63
64
65
66
# File 'lib/patch_retention/calendar_items.rb', line 60

def delete(calendar_item_id:, config: nil)
  response = PatchRetention.connection(config).delete("/v2/calendar_items/#{calendar_item_id}")

  { success: response.status == 204 }
rescue Faraday::Error => e
  raise Error, "Failed to delete calendar item: #{e.message}"
end

.find(calendar_item_id:, config: nil) ⇒ Object



52
53
54
55
56
57
58
# File 'lib/patch_retention/calendar_items.rb', line 52

def find(calendar_item_id:, config: nil)
  response = PatchRetention.connection(config).get("/v2/calendar_items/#{calendar_item_id}")

  JSON.parse(response.body)
rescue Faraday::Error => e
  raise Error, "Failed to find calendar item: #{e.message}"
end

.update(calendar_item_id:, title: nil, start_at: nil, end_at: nil, run_start_at: nil, run_end_at: nil, data: nil, tags: nil, config: nil) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/patch_retention/calendar_items.rb', line 31

def update(calendar_item_id:, title: nil, start_at: nil, end_at: nil, run_start_at: nil,
  run_end_at: nil, data: nil, tags: nil, config: nil)
  payload = {}
  payload[:title] = title if title
  payload[:start_at] = start_at if start_at
  payload[:end_at] = end_at if end_at
  payload[:run_start_at] = run_start_at if run_start_at
  payload[:run_end_at] = run_end_at if run_end_at
  payload[:data] = data if data
  payload[:tags] = tags if tags

  response = PatchRetention.connection(config).patch("/v2/calendar_items/#{calendar_item_id}") do |req|
    req.body = payload.to_json
    req.headers["Content-Type"] = "application/json"
  end

  JSON.parse(response.body)
rescue Faraday::Error => e
  raise Error, "Failed to update calendar item: #{e.message}"
end