Class: UscreenAPI::Customers

Inherits:
Base
  • Object
show all
Defined in:
lib/uscreen_api/customers.rb

Constant Summary collapse

PATH =
"customers"

Instance Attribute Summary

Attributes inherited from Base

#client

Instance Method Summary collapse

Methods inherited from Base

#initialize

Constructor Details

This class inherits a constructor from UscreenAPI::Base

Instance Method Details

#accesses(id:, page: nil, from: nil, to: nil) ⇒ Object

Get paginated list of accesses for a customer

Parameters:

  • id (Integer)

    Customer ID or Email

  • page (Integer) (defaults to: nil)

    Page number

  • from (String) (defaults to: nil)

    Starting date and time for the filter range, formatted in RFC 3339. Example: 2023-01-01T00:00:00Z

  • to (String) (defaults to: nil)

    Ending date and time for the filter range, formatted in RFC 3339. Example: 2023-01-01T00:00:00Z



119
120
121
122
123
124
125
126
127
128
129
# File 'lib/uscreen_api/customers.rb', line 119

def accesses(
  id:,
  page: nil,
  from: nil,
  to: nil
)
  response = client.connection.get("#{PATH}/#{id}/accesses")
  handle_errors(response.status, response.body)

  response.body
end

#create(email:, name:, password: nil, payment_user_id: nil, skip_invite: nil, opted_in_for_news_and_updates: nil, custom_fields: {}) ⇒ Object

Invite a new customer

Parameters:

  • email (String)

    Customer email

  • name (String)

    Customer name

  • password (String) (defaults to: nil)

    Customer password

  • payment_user_id (String) (defaults to: nil)

    Payment user ID

  • skip_invite (Boolean) (defaults to: nil)

    Skip invite email

  • opted_in_for_news_and_updates (Boolean) (defaults to: nil)

    Opted in for news and updates

  • custom_fields (Hash) (defaults to: {})

    Custom fields



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/uscreen_api/customers.rb', line 40

def create(
  email:,
  name:,
  password: nil,
  payment_user_id: nil,
  skip_invite: nil,
  opted_in_for_news_and_updates: nil,
  custom_fields: {}
)
  response = client.connection.post(PATH) do |req|
    req.body = {
      email: email,
      name: name
    }
    req.body["password"] = password if password
    req.body["payment_user_id"] = payment_user_id if payment_user_id
    req.body["skip_invite"] = skip_invite if skip_invite
    req.body["opted_in_for_news_and_updates"] = opted_in_for_news_and_updates if opted_in_for_news_and_updates

    custom_fields.each { |key, value| req.body[key] = value } if custom_fields.any?
  end

  handle_errors(response.status, response.body)

  response.body
end

#get(id:) ⇒ Object

Get a customer information

Parameters:

  • id (Integer)

    Customer ID or Email



69
70
71
72
73
74
75
# File 'lib/uscreen_api/customers.rb', line 69

def get(id:)
  response = client.connection.get("#{PATH}/#{id}")

  handle_errors(response.status, response.body)

  response.body
end

#grant_access(id:, product_id:, product_type: nil, perform_action_at: nil, with_manual_billing: false) ⇒ Object

Grant access to a customer

Parameters:

  • id (Integer)

    Customer ID or Email

  • product_id (Integer)

    Product ID

  • product_type (String) (defaults to: nil)

    Uscreen system product type. Can be a program|recurring|rent|freebie|fixed_price

  • perform_action_at (String) (defaults to: nil)

    Seconds since the Epoch. Next perform action needed in case of rent or recurring product types. System will add next due automatically in case of a blank field. example: 2024-07-05T13:47:52Z

  • with_manual_billing (Boolean) (defaults to: false)

    Allows you to process billing outside the Uscreen platform. Default false.



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/uscreen_api/customers.rb', line 137

def grant_access(
  id:,
  product_id:,
  product_type: nil,
  perform_action_at: nil,
  with_manual_billing: false
)
  response = client.connection.post("#{PATH}/#{id}/accesses") do |req|
    req.body = {
      product_id: product_id,
      with_manual_billing: with_manual_billing
    }
    req.body["product_type"] = product_type if product_type
    req.body["perform_action_at"] = perform_action_at if perform_action_at
  end

  handle_errors(response.status, response.body)

  response.body
end

#list(page: nil, from: nil, to: nil, date_field: nil) ⇒ Object

Get paginated list of customers

Parameters:

  • page (Integer) (defaults to: nil)

    Page number

  • from (String) (defaults to: nil)

    Starting date and time for the filter range, formatted in RFC 3339. Example: 2023-01-01T00:00:00Z

  • to (String) (defaults to: nil)

    Ending date and time for the filter range, formatted in RFC 3339. Example: 2023-01-01T00:00:00Z

  • date_field (String) (defaults to: nil)

    Date field to filter by. Available fields: created_at, updated_at. Default: created_at. If not set, created_at is used.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/uscreen_api/customers.rb', line 12

def list(
  page: nil,
  from: nil,
  to: nil,
  date_field: nil
)
  response = client.connection.get(PATH) do |req|
    req.params = {}

    req.params["page"] = page if page
    req.params["from"] = from if from
    req.params["to"] = to if to
    req.params["date_field"] = date_field if date_field
  end

  handle_errors(response.status, response.body)

  response.body
end

#revoke_access(id:, access_id:) ⇒ Object

Revoke access by ID

Parameters:

  • id (Integer)

    Customer ID or Email

  • access_id (Integer)

    Access ID



161
162
163
164
165
166
167
168
169
# File 'lib/uscreen_api/customers.rb', line 161

def revoke_access(
  id:,
  access_id:
)
  response = client.connection.delete("#{PATH}/#{id}/accesses/#{access_id}")
  handle_errors(response.status, response.body)

  response.body
end

#tokenized_url(id:) ⇒ Object

Generates a Single Sign-On link for a customer

Parameters:

  • id (Integer)

    Customer ID or Email



107
108
109
110
111
112
# File 'lib/uscreen_api/customers.rb', line 107

def tokenized_url(id:)
  response = client.connection.post("#{PATH}/#{id}/tokenized_url")
  handle_errors(response.status, response.body)

  response.body
end

#update(id:, email: nil, name: nil, password: nil, custom_fields: {}) ⇒ Object

Update a customer information

Parameters:

  • id (Integer)

    Customer ID or Email

  • email (String) (defaults to: nil)

    New customer email

  • name (String) (defaults to: nil)

    New customer name

  • password (String) (defaults to: nil)

    New customer password

  • custom_fields (Hash) (defaults to: {})

    Custom fields



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/uscreen_api/customers.rb', line 83

def update(
  id:,
  email: nil,
  name: nil,
  password: nil,
  custom_fields: {}
)
  response = client.connection.put("#{PATH}/#{id}") do |req|
    req.body = {}

    req.body["email"] = email if email
    req.body["name"] = name if name
    req.body["password"] = password if password

    custom_fields.each { |key, value| req.body[key] = value } if custom_fields.any?
  end

  handle_errors(response.status, response.body)

  response.body
end