Class: BaseCRM::SourcesService

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

Constant Summary collapse

OPTS_KEYS_TO_PERSIST =
Set[:name]

Instance Method Summary collapse

Constructor Details

#initialize(client) ⇒ SourcesService

Returns a new instance of SourcesService.



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

def initialize(client)
  @client = client
end

Instance Method Details

#allEnumerable

Retrieve all sources

get ‘/sources’

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

def all
  PaginatedResource.new(self)
end

#create(source) ⇒ Source

Create a source

post ‘/sources’

Creates a new source <figure class=“notice”> Source’s name must be unique </figure>

Parameters:

  • source (Source, Hash)

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

Returns:

  • (Source)

    The resulting object represting created resource.



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

def create(source)
  validate_type!(source)

  attributes = sanitize(source)
  _, _, root = @client.post("/sources", attributes)

  Source.new(root[:data])
end

#destroy(id) ⇒ Boolean

Delete a source

delete ‘/sources/BaseCRM#id

Delete an existing source If the specified source does not exist, the request will return an error This operation cannot be undone

Parameters:

  • id (Integer)

    Unique identifier of a Source

Returns:

  • (Boolean)

    Status of the operation.



112
113
114
115
# File 'lib/basecrm/services/sources_service.rb', line 112

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

#find(id) ⇒ Source

Retrieve a single source

get ‘/sources/BaseCRM#id

Returns a single source available to the user by the provided id If a source with the supplied unique identifier does not exist it returns an error

Parameters:

  • id (Integer)

    Unique identifier of a Source

Returns:

  • (Source)

    Searched resource object.



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

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

  Source.new(root[:data])
end

#update(source) ⇒ Source

Update a source

put ‘/sources/BaseCRM#id

Updates source information If the specified source does not exist, the request will return an error <figure class=“notice”> If you want to update a source, you must make sure source’s name is unique </figure>

Parameters:

  • source (Source, Hash)

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

Returns:

  • (Source)

    The resulting object represting updated resource.



90
91
92
93
94
95
96
97
98
99
# File 'lib/basecrm/services/sources_service.rb', line 90

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

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

  Source.new(root[:data])
end

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

Retrieve all sources

get ‘/sources’

Returns all deal sources available to the user according to the parameters provided

Parameters:

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

    Search options

Options Hash (options):

  • :ids (String)

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

  • :name (String)

    Name of the source 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.

  • :sort_by (String) — default: id: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=name:desc`.

Returns:

  • (Array<Source>)

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



34
35
36
37
38
# File 'lib/basecrm/services/sources_service.rb', line 34

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

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