Class: BaseCRM::TagsService

Inherits:
Object
  • Object
show all
Defined in:
lib/basecrm/services/tags_service.rb

Constant Summary collapse

OPTS_KEYS_TO_PERSIST =
Set[:name, :resource_type]

Instance Method Summary collapse

Constructor Details

#initialize(client) ⇒ TagsService

Returns a new instance of TagsService.



7
8
9
# File 'lib/basecrm/services/tags_service.rb', line 7

def initialize(client)
  @client = client
end

Instance Method Details

#allEnumerable

Retrieve all tags

get ‘/tags’

If you want to use filtering or sorting (see #where).

Returns:

  • (Enumerable)

    Paginated resource you can use to iterate over all the resources.



17
18
19
# File 'lib/basecrm/services/tags_service.rb', line 17

def all
  PaginatedResource.new(self)
end

#create(tag) ⇒ Tag

Create a tag

post ‘/tags’

Creates a new tag Notice the tag’s name must be unique within the scope of the resource_type

Parameters:

  • tag (Tag, Hash)

    Either object of the Tag type or Hash. This object’s attributes describe the object to be created.

Returns:

  • (Tag)

    The resulting object represting created resource.



52
53
54
55
56
57
58
59
# File 'lib/basecrm/services/tags_service.rb', line 52

def create(tag)
  validate_type!(tag)

  attributes = sanitize(tag)
  _, _, root = @client.post("/tags", attributes)

  Tag.new(root[:data])
end

#destroy(id) ⇒ Boolean

Delete a tag

delete ‘/tags/BaseCRM#id

Deletes an existing tag If the specified tag is assigned to any resource, we will remove this tag from all such resources If the specified tag does not exist, this query will return an error This operation cannot be undone

Parameters:

  • id (Integer)

    Unique identifier of a Tag

Returns:

  • (Boolean)

    Status of the operation.



111
112
113
114
# File 'lib/basecrm/services/tags_service.rb', line 111

def destroy(id)
  status, _, _ = @client.delete("/tags/#{id}")
  status == 204
end

#find(id) ⇒ Tag

Retrieve a single tag

get ‘/tags/BaseCRM#id

Returns a single tag available to the user according to the unique ID provided If the specified tag does not exist, this query will return an error

Parameters:

  • id (Integer)

    Unique identifier of a Tag

Returns:

  • (Tag)

    Searched resource object.



71
72
73
74
75
# File 'lib/basecrm/services/tags_service.rb', line 71

def find(id)
  _, _, root = @client.get("/tags/#{id}")

  Tag.new(root[:data])
end

#update(tag) ⇒ Tag

Update a tag

put ‘/tags/BaseCRM#id

Updates a tag’s information If the specified tag does not exist, this query will return an error Notice if you want to update a tag, you must make sure the tag’s name is unique within the scope of the specified resource

Parameters:

  • tag (Tag, Hash)

    Either object of the Tag type or Hash. This object’s attributes describe the object to be updated.

Returns:

  • (Tag)

    The resulting object represting updated resource.



88
89
90
91
92
93
94
95
96
97
# File 'lib/basecrm/services/tags_service.rb', line 88

def update(tag)
  validate_type!(tag)
  params = extract_params!(tag, :id)
  id = params[:id]

  attributes = sanitize(tag)
  _, _, root = @client.put("/tags/#{id}", attributes)

  Tag.new(root[:data])
end

#where(options = {}) ⇒ Array<Tag>

Retrieve all tags

get ‘/tags’

Returns all tags available to the user, according to the parameters provided

Parameters:

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

    Search options

Options Hash (options):

  • :creator_id (Integer)

    User ID. Returns all tags created by that user.

  • :ids (String)

    Comma-separated list of deal tag IDs to be returned in a request.

  • :name (String)

    Name of the tag to search for. This parameter is used in a strict sense.

  • :page (Integer) — default: 1

    Page number to start from. Page numbering starts at 1, and omitting the ‘page` parameter will return the first page.

  • :per_page (Integer) — default: 25

    Number of records to return per page. The default limit is 25 and the maximum number that can be returned is 100.

  • :resource_type (String)

    Type name of resource to search for.

  • :sort_by (String) — default: updated_at:asc

    A field to sort by. The default order is ascending. If you want to change the sort order to descending, append ‘:desc` to the field e.g. `sort_by=name:desc`.

Returns:

  • (Array<Tag>)

    The list of Tags for the first page, unless otherwise specified.



36
37
38
39
40
# File 'lib/basecrm/services/tags_service.rb', line 36

def where(options = {})
  _, _, root = @client.get("/tags", options)

  root[:items].map{ |item| Tag.new(item[:data]) }
end