Class: BaseCRM::TasksService

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

Constant Summary collapse

OPTS_KEYS_TO_PERSIST =
Set[:completed, :content, :due_date, :owner_id, :remind_at, :resource_id, :resource_type]

Instance Method Summary collapse

Constructor Details

#initialize(client) ⇒ TasksService

Returns a new instance of TasksService.



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

def initialize(client)
  @client = client
end

Instance Method Details

#allEnumerable

Retrieve all tasks

get ‘/tasks’

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/tasks_service.rb', line 17

def all
  PaginatedResource.new(self)
end

#create(task) ⇒ Task

Create a task

post ‘/tasks’

Creates a new task You can create either a floating task or create a related task and associate it with one of the resource types below:

  • [Leads](/docs/rest/reference/leads)

  • [Contacts](/docs/rest/reference/contacts)

  • [Deals](/docs/rest/reference/deals)

Parameters:

  • task (Task, Hash)

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

Returns:

  • (Task)

    The resulting object represting created resource.



63
64
65
66
67
68
69
70
# File 'lib/basecrm/services/tasks_service.rb', line 63

def create(task)
  validate_type!(task)

  attributes = sanitize(task)
  _, _, root = @client.post("/tasks", attributes)

  Task.new(root[:data])
end

#destroy(id) ⇒ Boolean

Delete a task

delete ‘/tasks/BaseCRM#id

Delete an existing task If the specified task does not exist, this query will return an error This operation cannot be undone

Parameters:

  • id (Integer)

    Unique identifier of a Task

Returns:

  • (Boolean)

    Status of the operation.



120
121
122
123
# File 'lib/basecrm/services/tasks_service.rb', line 120

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

#find(id) ⇒ Task

Retrieve a single task

get ‘/tasks/BaseCRM#id

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

Parameters:

  • id (Integer)

    Unique identifier of a Task

Returns:

  • (Task)

    Searched resource object.



82
83
84
85
86
# File 'lib/basecrm/services/tasks_service.rb', line 82

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

  Task.new(root[:data])
end

#update(task) ⇒ Task

Update a task

put ‘/tasks/BaseCRM#id

Updates task information If the specified task does not exist, this query will return an error

Parameters:

  • task (Task, Hash)

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

Returns:

  • (Task)

    The resulting object represting updated resource.



98
99
100
101
102
103
104
105
106
107
# File 'lib/basecrm/services/tasks_service.rb', line 98

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

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

  Task.new(root[:data])
end

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

Retrieve all tasks

get ‘/tasks’

Returns all tasks available to the user, according to the parameters provided If you ask for tasks without any parameter provided Base API will return you both floating and related tasks Although you can narrow the search set to either of them via query parameters

Parameters:

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

    Search options

Options Hash (options):

  • :completed (Boolean)

    Indicates whether the query will return tasks that are completed or not.

  • :creator_id (Integer)

    Unique identifier of the user. Returns all tasks created by the user.

  • :ids (String)

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

  • :overdue (Boolean)

    Indicates whether the query will return tasks where the ‘due_date` parameter has been passed or not.

  • :owner_id (Integer)

    Unique identifier of the user. Returns all tasks owned by the user.

  • :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.

  • :q (String)

    A query string to search for. Performs a full text search on the ‘content` field.

  • :remind (Boolean)

    Indicates whether the query will return tasks with reminders or without reminders.

  • :resource_id (Integer)

    Unique identifier of the resource that you’re searching for.

  • :resource_type (String)

    Name of the resource type to search for.

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

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

  • :type (String)

    Type of tasks to search for.

Returns:

  • (Array<Task>)

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



44
45
46
47
48
# File 'lib/basecrm/services/tasks_service.rb', line 44

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

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