Class: Warrant::Object

Inherits:
Object
  • Object
show all
Defined in:
lib/warrant/models/object.rb

Direct Known Subclasses

Feature, Permission, PricingTier, Role, Tenant, User

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#created_atObject (readonly)

Returns the value of attribute created_at.



5
6
7
# File 'lib/warrant/models/object.rb', line 5

def created_at
  @created_at
end

#metaObject (readonly)

Returns the value of attribute meta.



5
6
7
# File 'lib/warrant/models/object.rb', line 5

def meta
  @meta
end

#object_idObject (readonly)

Returns the value of attribute object_id.



5
6
7
# File 'lib/warrant/models/object.rb', line 5

def object_id
  @object_id
end

#object_typeObject (readonly)

Returns the value of attribute object_type.



5
6
7
# File 'lib/warrant/models/object.rb', line 5

def object_type
  @object_type
end

Class Method Details

.batch_create(objects, options = {}) ⇒ Array<Object>

Batch creates multiple objects with given parameters

Examples:

Create two new objects with object type “user” and object ids “test-user-1” and “test-user-2”

Warrant::Object.batch_create([{ object_type: "user", object_id: "test-user-1" }, { object_type: "user", object_id: "test-user-2" }])

Parameters:

  • objects (Array<Hash>)

    Array of objects to create.

Options Hash (objects):

  • :object_type (String)

    The type of the object (e.g. user, tenant, role, permission, etc).

  • :object_id (String)

    Customer defined string identifier for this object. Can only contain alphanumeric chars and/or ‘-’, ‘_’, ‘|’, ‘@’. If not provided, Warrant will create a univerally unique identifier (UUID) for the object and return it. If allowing Warrant to generate an id, store the id in your application so you can provide it for authorization requests on that object. (optional)

  • :meta (Hash)

    A JSON object containing additional information about this object (e.g. role name/description, user email/name, etc.) to be persisted to Warrant. (optional)

Returns:

  • (Array<Object>)

    all created objects

Raises:



62
63
64
65
66
67
68
69
70
71
72
# File 'lib/warrant/models/object.rb', line 62

def self.batch_create(objects, options = {})
    res = APIOperations.post(URI.parse("#{::Warrant.config.api_base}/v2/objects"), params: Util.normalize_params(objects), options: options)

    case res
    when Net::HTTPSuccess
        objects = JSON.parse(res.body, symbolize_names: true)
        objects.map{ |object| Object.new(object[:objectType], object[:objectId], object[:meta], object[:createdAt]) }
    else
        APIOperations.raise_error(res)
    end
end

.batch_delete(objects, options = {}) ⇒ nil

Batch deletes multiple objects with given parameters

Examples:

Delete two objects with object type “user” and object ids “test-user-1” and “test-user-2”

Warrant::Object.batch_delete([{ object_type: "user", object_id: "test-user-1" }, { object_type: "user", object_id: "test-user-2" }])

Parameters:

  • objects (Array<Hash>)

    Array of objects to delete.

Options Hash (objects):

  • :object_type (String)

    The type of the object (e.g. user, tenant, role, permission, etc).

  • :object_id (String)

    Customer defined string identifier for this object.

Returns:

  • (nil)

    if delete was successful

Raises:



114
115
116
117
118
119
120
121
122
123
# File 'lib/warrant/models/object.rb', line 114

def self.batch_delete(objects, options = {})
    res = APIOperations.delete(URI.parse("#{::Warrant.config.api_base}/v2/objects"), params: Util.normalize_params(objects), options: options)

    case res
    when Net::HTTPSuccess
        return res['Warrant-Token']
    else
        APIOperations.raise_error(res)
    end
end

.create(params = {}, options = {}) ⇒ Object

Creates an object with the given parameters

Examples:

Create a new Object with the object type “user” and object id “test-customer”

Warrant::Object.create(object_type: "user", object_id: "test-customer")

Parameters:

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

    a customizable set of options

Options Hash (params):

  • :object_type (String)

    The type of the object (e.g. user, tenant, role, permission, etc).

  • :object_id (String)

    Customer defined string identifier for this object. Can only contain alphanumeric chars and/or ‘-’, ‘_’, ‘|’, ‘@’. If not provided, Warrant will create a univerally unique identifier (UUID) for the object and return it. If allowing Warrant to generate an id, store the id in your application so you can provide it for authorization requests on that object. (optional)

  • :meta (Hash)

    A JSON object containing additional information about this object (e.g. role name/description, user email/name, etc.) to be persisted to Warrant. (optional)

Returns:

Raises:



32
33
34
35
36
37
38
39
40
41
42
# File 'lib/warrant/models/object.rb', line 32

def self.create(params = {}, options = {})
    res = APIOperations.post(URI.parse("#{::Warrant.config.api_base}/v2/objects"), params: Util.normalize_params(params), options: options)

    case res
    when Net::HTTPSuccess
        res_json = JSON.parse(res.body, symbolize_names: true)
        Object.new(res_json[:objectType], res_json[:objectId], res_json[:meta], res_json[:createdAt])
    else
        APIOperations.raise_error(res)
    end
end

.delete(object_type, object_id, options = {}) ⇒ nil

Deletes a object with given object type and id

Examples:

Delete a Object with the object type “user” and object id “test-customer”

Warrant::Object.delete("user", "test-customer")

Parameters:

  • object_type (String)

    The type of the object (e.g. user, tenant, role, permission, etc).

  • object_id (String)

    User defined string identifier for this object.

Returns:

  • (nil)

    if delete was successful

Raises:



88
89
90
91
92
93
94
95
96
97
# File 'lib/warrant/models/object.rb', line 88

def self.delete(object_type, object_id, options = {})
    res = APIOperations.delete(URI.parse("#{::Warrant.config.api_base}/v2/objects/#{object_type}/#{object_id}"), options: options)

    case res
    when Net::HTTPSuccess
        return res['Warrant-Token']
    else
        APIOperations.raise_error(res)
    end
end

.get(object_type, object_id, options = {}) ⇒ Object

Get a object with the given object_type and object_id

Parameters:

  • object_id (String)

    Object defined string identifier for this object. If not provided, Warrant will create an id for the object and return it. In this case, you should store the id in your system as you will need to provide it for any authorization requests for that object. Note that objectIds in Warrant must be composed of alphanumeric chars and/or ‘-’, ‘_’, and ‘@’.

Returns:

  • (Object)

    retrieved object

Raises:



168
169
170
171
172
173
174
175
176
177
178
# File 'lib/warrant/models/object.rb', line 168

def self.get(object_type, object_id, options = {})
    res = APIOperations.get(URI.parse("#{::Warrant.config.api_base}/v2/objects/#{object_type}/#{object_id}"), options: options)

    case res
    when Net::HTTPSuccess
        object = JSON.parse(res.body, symbolize_names: true)
        Object.new(object[:objectType], object[:objectId], object[:meta], object[:createdAt])
    else
        APIOperations.raise_error(res)
    end
end

.list(filters = {}, options = {}) ⇒ Array<Object>

Lists all objects for your organization and environment

Examples:

List all objects

Warrant::Object.list()

Parameters:

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

    Filters to apply to result set

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

    Options to apply on a per-request basis

Options Hash (filters):

  • :object_type (String)

    Only return objects with an object_type matching this value

  • :limit (Integer)

    A positive integer representing the maximum number of items to return in the response. Must be less than or equal to 1000. Defaults to 25. (optional)

  • :prev_cursor (String)

    A cursor representing your place in a list of results. Requests containing prev_cursor will return the results immediately preceding the cursor. (optional)

  • :next_cursor (String)

    A cursor representing your place in a list of results. Requests containing next_cursor will return the results immediately following the cursor. (optional)

  • :sort_by (String)

    The column to sort the result by. Unless otherwise specified, all list endpoints are sorted by their unique identifier by default. Supported values for objects are object_type, object_id, and created_at (optional)

  • :sort_order (String)

    The order in which to sort the result by. Valid values are ASC and DESC. Defaults to ASC. (optional)

Options Hash (options):

  • :warrant_token (String)

    A valid warrant token from a previous write operation or latest. Used to specify desired consistency for this read operation. (optional)

Returns:

  • (Array<Object>)

    all objects for your organization and environment

Raises:



145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/warrant/models/object.rb', line 145

def self.list(filters = {}, options = {})
    res = APIOperations.get(URI.parse("#{::Warrant.config.api_base}/v2/objects"), params: Util.normalize_params(filters), options: options)

    case res
    when Net::HTTPSuccess
        list_result = JSON.parse(res.body, symbolize_names: true)
        objects = list_result[:results].map{ |object| Object.new(object[:objectType], object[:objectId], object[:meta], object[:createdAt]) }
        return ListResponse.new(objects, list_result[:prevCursor], list_result[:nextCursor])
    else
        APIOperations.raise_error(res)
    end
end

.update(object_type, object_id, meta, options = {}) ⇒ Object

Updates a object with the given object_type and object_id and params

Examples:

Update user “test-user”‘s email

Warrant::Object.update("user", "test-user", { email: "[email protected]" })

Parameters:

  • object_type (String)

    The type of the object (e.g. user, tenant, role, permission, etc).

  • object_id (String)

    User defined string identifier for this object.

  • meta (Hash)

    A JSON object containing additional information about this object (e.g. role name/description, user email/name, etc.) to be persisted to Warrant.

Returns:

Raises:



196
197
198
199
200
201
202
203
204
205
206
207
208
209
# File 'lib/warrant/models/object.rb', line 196

def self.update(object_type, object_id, meta, options = {})
    params = {
        meta: meta,
    }
    res = APIOperations.put(URI.parse("#{::Warrant.config.api_base}/v2/objects/#{object_type}/#{object_id}"), params: Util.normalize_params(params), options: options)

    case res
    when Net::HTTPSuccess
        res_json = JSON.parse(res.body, symbolize_names: true)
        Object.new(res_json[:objectType], res_json[:objectId], res_json[:meta], res_json[:createdAt])
    else
        APIOperations.raise_error(res)
    end
end

Instance Method Details

#update(meta, options = {}) ⇒ Object

Updates a object with the given object_type and object_id and params

Examples:

Update user “test-user”‘s email

user = Warrant::Object.get("user", "test-user")
user.update({ email: "[email protected]" })

Parameters:

  • meta (Hash)

    A JSON object containing additional information about this object (e.g. role name/description, user email/name, etc.) to be persisted to Warrant.

Returns:

Raises:



226
227
228
# File 'lib/warrant/models/object.rb', line 226

def update(meta, options = {})
    return Object.update(object_type, object_id, meta, options)
end