Class: Nylas::Rules

Inherits:
Resource show all
Includes:
ApiOperations::Delete, ApiOperations::Get, ApiOperations::Post, ApiOperations::Put
Defined in:
lib/nylas/resources/rules.rb

Overview

Nylas Rules API

Instance Method Summary collapse

Methods inherited from Resource

#initialize

Constructor Details

This class inherits a constructor from Nylas::Resource

Instance Method Details

#create(request_body:) ⇒ Array(Hash, String)

Create a rule.

Parameters:

  • request_body (Hash)

    The values to create the rule with.

Returns:

  • (Array(Hash, String))

    The created rule and API Request ID.



113
114
115
116
117
118
# File 'lib/nylas/resources/rules.rb', line 113

def create(request_body:)
  post(
    path: "#{api_uri}/v3/rules",
    request_body: request_body
  )
end

#destroy(rule_id:) ⇒ Array(TrueClass, String)

Delete a rule.

Parameters:

  • rule_id (String)

    The id of the rule to delete.

Returns:

  • (Array(TrueClass, String))

    True and the API Request ID for the delete operation.



136
137
138
139
140
141
142
# File 'lib/nylas/resources/rules.rb', line 136

def destroy(rule_id:)
  _, request_id = delete(
    path: "#{api_uri}/v3/rules/#{rule_id}"
  )

  [true, request_id]
end

#find(rule_id:) ⇒ Array(Hash, String, Hash)

Return a rule.

Parameters:

  • rule_id (String)

    The id of the rule to return.

Returns:

  • (Array(Hash, String, Hash))

    The rule, API request ID, and response headers.



103
104
105
106
107
# File 'lib/nylas/resources/rules.rb', line 103

def find(rule_id:)
  get(
    path: "#{api_uri}/v3/rules/#{rule_id}"
  )
end

#list(query_params: nil) ⇒ Array(Array(Hash), String, String, Hash)

Return all rules.

The list endpoint returns a nested envelope ({ request_id, data: { items: [...], next_cursor } }), so the items and cursor are unwrapped here defensively rather than via the standard get_list helper, which would mis-read the nested shape.

Parameters:

  • query_params (Hash, nil) (defaults to: nil)

    Query params to pass to the request.

Returns:

  • (Array(Array(Hash), String, String, Hash))

    The list of rules, API Request ID, next cursor, and response headers.



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/nylas/resources/rules.rb', line 79

def list(query_params: nil)
  response = get_raw(
    path: "#{api_uri}/v3/rules",
    query_params: query_params
  )

  data = response[:data]
  # Unwrap only when the envelope actually carries an :items key. Go's
  # ListWithCursorResult serializes a nil slice as "items": null, so coerce
  # that to [] rather than falling back to the envelope hash itself.
  items = if data.is_a?(Hash) && data.key?(:items)
            data[:items] || []
          else
            data
          end
  next_cursor = data.is_a?(Hash) ? data[:next_cursor] : response[:next_cursor]

  [items, response[:request_id], next_cursor, response[:headers]]
end

#list_evaluations(grant_id:, query_params: nil) ⇒ Array(Array(Hash), String, String, Hash)

Return all rule evaluations for a grant.

This endpoint returns a flat array with no cursor, so the standard get_list helper is used (next_cursor is always nil).

Parameters:

  • grant_id (String)

    The id of the grant to query rule evaluations for.

  • query_params (Hash, nil) (defaults to: nil)

    Query params to pass to the request.

Returns:

  • (Array(Array(Hash), String, String, Hash))

    The list of rule evaluations, API Request ID, next cursor (always nil for this endpoint), and response headers.



154
155
156
157
158
159
# File 'lib/nylas/resources/rules.rb', line 154

def list_evaluations(grant_id:, query_params: nil)
  get_list(
    path: "#{api_uri}/v3/grants/#{grant_id}/rule-evaluations",
    query_params: query_params
  )
end

#update(rule_id:, request_body:) ⇒ Array(Hash, String)

Update a rule. Only the provided fields are changed (partial update).

Parameters:

  • rule_id (String)

    The id of the rule to update.

  • request_body (Hash)

    The values to update the rule with.

Returns:

  • (Array(Hash, String))

    The updated rule and API Request ID.



125
126
127
128
129
130
# File 'lib/nylas/resources/rules.rb', line 125

def update(rule_id:, request_body:)
  put(
    path: "#{api_uri}/v3/rules/#{rule_id}",
    request_body: request_body
  )
end