basecrm 0.1.2 → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/LICENSE +2 -2
- data/README.md +273 -17
- data/lib/basecrm.rb +185 -1
- data/lib/basecrm/configuration.rb +59 -0
- data/lib/basecrm/envelope.rb +7 -0
- data/lib/basecrm/errors.rb +65 -0
- data/lib/basecrm/http_client.rb +91 -0
- data/lib/basecrm/middlewares/oauth_bearer_token.rb +18 -0
- data/lib/basecrm/middlewares/raise_error.rb +31 -0
- data/lib/basecrm/model.rb +6 -0
- data/lib/basecrm/models/account.rb +31 -0
- data/lib/basecrm/models/address.rb +22 -0
- data/lib/basecrm/models/associated_contact.rb +19 -0
- data/lib/basecrm/models/contact.rb +88 -0
- data/lib/basecrm/models/deal.rb +58 -0
- data/lib/basecrm/models/lead.rb +79 -0
- data/lib/basecrm/models/loss_reason.rb +22 -0
- data/lib/basecrm/models/note.rb +28 -0
- data/lib/basecrm/models/pipeline.rb +19 -0
- data/lib/basecrm/models/source.rb +22 -0
- data/lib/basecrm/models/stage.rb +34 -0
- data/lib/basecrm/models/tag.rb +25 -0
- data/lib/basecrm/models/task.rb +46 -0
- data/lib/basecrm/models/user.rb +31 -0
- data/lib/basecrm/paginated_resource.rb +32 -0
- data/lib/basecrm/services/accounts_service.rb +33 -0
- data/lib/basecrm/services/associated_contacts_service.rb +91 -0
- data/lib/basecrm/services/contacts_service.rb +138 -0
- data/lib/basecrm/services/deals_service.rb +137 -0
- data/lib/basecrm/services/leads_service.rb +140 -0
- data/lib/basecrm/services/loss_reasons_service.rb +133 -0
- data/lib/basecrm/services/notes_service.rb +134 -0
- data/lib/basecrm/services/pipelines_service.rb +50 -0
- data/lib/basecrm/services/sources_service.rb +133 -0
- data/lib/basecrm/services/stages_service.rb +52 -0
- data/lib/basecrm/services/tags_service.rb +132 -0
- data/lib/basecrm/services/tasks_service.rb +141 -0
- data/lib/basecrm/services/users_service.rb +83 -0
- data/lib/basecrm/version.rb +3 -0
- data/spec/factories/associated_contact.rb +14 -0
- data/spec/factories/contact.rb +27 -0
- data/spec/factories/deal.rb +17 -0
- data/spec/factories/lead.rb +26 -0
- data/spec/factories/loss_reason.rb +11 -0
- data/spec/factories/note.rb +13 -0
- data/spec/factories/source.rb +11 -0
- data/spec/factories/tag.rb +12 -0
- data/spec/factories/task.rb +15 -0
- data/spec/services/accounts_service_spec.rb +16 -0
- data/spec/services/associated_contacts_service_spec.rb +43 -0
- data/spec/services/contacts_service_spec.rb +58 -0
- data/spec/services/deals_service_spec.rb +58 -0
- data/spec/services/leads_service_spec.rb +58 -0
- data/spec/services/loss_reasons_service_spec.rb +58 -0
- data/spec/services/notes_service_spec.rb +58 -0
- data/spec/services/pipelines_service_spec.rb +23 -0
- data/spec/services/sources_service_spec.rb +58 -0
- data/spec/services/stages_service_spec.rb +23 -0
- data/spec/services/tags_service_spec.rb +58 -0
- data/spec/services/tasks_service_spec.rb +58 -0
- data/spec/services/users_service_spec.rb +39 -0
- data/spec/spec_helper.rb +24 -12
- data/spec/support/client_helpers.rb +19 -0
- metadata +160 -71
- data/.gitignore +0 -20
- data/.rspec +0 -2
- data/.travis.yml +0 -6
- data/Gemfile +0 -4
- data/Rakefile +0 -8
- data/basecrm.gemspec +0 -23
- data/lib/base_crm.rb +0 -24
- data/lib/base_crm/account.rb +0 -11
- data/lib/base_crm/api_client_ext.rb +0 -6
- data/lib/base_crm/config.rb +0 -21
- data/lib/base_crm/contact.rb +0 -44
- data/lib/base_crm/custom_fieldable.rb +0 -32
- data/lib/base_crm/deal.rb +0 -50
- data/lib/base_crm/forecasting.rb +0 -12
- data/lib/base_crm/lead.rb +0 -36
- data/lib/base_crm/note.rb +0 -15
- data/lib/base_crm/noteable.rb +0 -15
- data/lib/base_crm/related_object_scope.rb +0 -35
- data/lib/base_crm/resource.rb +0 -14
- data/lib/base_crm/session.rb +0 -48
- data/lib/base_crm/source.rb +0 -14
- data/lib/base_crm/task.rb +0 -16
- data/lib/base_crm/taskable.rb +0 -15
- data/lib/base_crm/version.rb +0 -3
- data/spec/base_crm/account_spec.rb +0 -20
- data/spec/base_crm/contact_spec.rb +0 -92
- data/spec/base_crm/deal_spec.rb +0 -138
- data/spec/base_crm/forecasting_spec.rb +0 -34
- data/spec/base_crm/lead_spec.rb +0 -63
- data/spec/base_crm/note_spec.rb +0 -20
- data/spec/base_crm/resource_mixin_spec.rb +0 -26
- data/spec/base_crm/session_spec.rb +0 -97
- data/spec/base_crm/source_spec.rb +0 -20
- data/spec/base_crm/task_spec.rb +0 -21
- data/spec/support/noteable_shared_examples.rb +0 -64
- data/spec/support/taskable_shared_examples.rb +0 -69
@@ -0,0 +1,91 @@
|
|
1
|
+
# WARNING: This code is auto-generated from the BaseCRM API Discovery JSON Schema
|
2
|
+
|
3
|
+
module BaseCRM
|
4
|
+
class AssociatedContactsService
|
5
|
+
OPTS_KEYS_TO_PERSIST = Set[:contact_id, :role]
|
6
|
+
|
7
|
+
def initialize(client)
|
8
|
+
@client = client
|
9
|
+
end
|
10
|
+
|
11
|
+
# Retrieve deal's associated contacts
|
12
|
+
#
|
13
|
+
# get '/deals/{deal_id}/associated_contacts'
|
14
|
+
#
|
15
|
+
# If you want to use filtering or sorting (see #where).
|
16
|
+
# @return [Enumerable] Paginated resource you can use to iterate over all the resources.
|
17
|
+
def all(deal_id)
|
18
|
+
PaginatedResource.new(self, deal_id)
|
19
|
+
end
|
20
|
+
|
21
|
+
# Retrieve deal's associated contacts
|
22
|
+
#
|
23
|
+
# get '/deals/{deal_id}/associated_contacts'
|
24
|
+
#
|
25
|
+
# Returns all deal associated contacts
|
26
|
+
#
|
27
|
+
# @param deal_id [Integer] Unique identifier of a Deal
|
28
|
+
# @param options [Hash] Search options
|
29
|
+
# @option options [Integer] :page (1) Page number to start from. Page numbering starts at 1, and omitting the `page` parameter will return the first page.
|
30
|
+
# @option options [Integer] :per_page (25) Number of records to return per page. Default limit is *25* and the maximum number that can be returned is *100*.
|
31
|
+
# @return [Array<AssociatedContact>] The list of AssociatedContacts for the first page, unless otherwise specified.
|
32
|
+
def where(deal_id, options = {})
|
33
|
+
_, _, root = @client.get("/deals/#{deal_id}/associated_contacts", options)
|
34
|
+
|
35
|
+
root[:items].map{ |item| AssociatedContact.new(item[:data]) }
|
36
|
+
end
|
37
|
+
|
38
|
+
|
39
|
+
# Create an associated contact
|
40
|
+
#
|
41
|
+
# post '/deals/{deal_id}/associated_contacts'
|
42
|
+
#
|
43
|
+
# Creates a deal's associated contact and its role
|
44
|
+
# If the specified deal or contact does not exist, the request will return an error
|
45
|
+
#
|
46
|
+
# @param deal_id [Integer] Unique identifier of a Deal
|
47
|
+
# @param associated_contact [AssociatedContact, Hash] Either object of the AssociatedContact type or Hash. This object's attributes describe the object to be created.
|
48
|
+
# @return [AssociatedContact] The resulting object represting created resource.
|
49
|
+
def create(deal_id, associated_contact)
|
50
|
+
validate_type!(associated_contact)
|
51
|
+
|
52
|
+
attributes = sanitize(associated_contact)
|
53
|
+
_, _, root = @client.post("/deals/#{deal_id}/associated_contacts", attributes)
|
54
|
+
|
55
|
+
AssociatedContact.new(root[:data])
|
56
|
+
end
|
57
|
+
|
58
|
+
|
59
|
+
# Remove an associated contact
|
60
|
+
#
|
61
|
+
# delete '/deals/{deal_id}/associated_contacts/{contact_id}'
|
62
|
+
#
|
63
|
+
# Remove a deal's associated contact
|
64
|
+
# If a deal with the supplied unique identifier does not exist, it returns an error
|
65
|
+
# This operation cannot be undone
|
66
|
+
#
|
67
|
+
# @param deal_id [Integer] Unique identifier of a Deal
|
68
|
+
# @param contact_id [Integer] Unique identifier of a Contact
|
69
|
+
# @return [Boolean] Status of the operation.
|
70
|
+
def destroy(deal_id, contact_id)
|
71
|
+
status, _, _ = @client.delete("/deals/#{deal_id}/associated_contacts/#{contact_id}")
|
72
|
+
status == 204
|
73
|
+
end
|
74
|
+
|
75
|
+
|
76
|
+
private
|
77
|
+
def validate_type!(associated_contact)
|
78
|
+
raise TypeError unless associated_contact.is_a?(AssociatedContact) || associated_contact.is_a?(Hash)
|
79
|
+
end
|
80
|
+
|
81
|
+
def extract_params!(associated_contact, *args)
|
82
|
+
params = associated_contact.to_h.select{ |k, _| args.include?(k) }
|
83
|
+
raise ArgumentError, "one of required attributes is missing. Expected: #{args.join(',')}" if params.count != args.length
|
84
|
+
params
|
85
|
+
end
|
86
|
+
|
87
|
+
def sanitize(associated_contact)
|
88
|
+
associated_contact.to_h.select { |k, _| OPTS_KEYS_TO_PERSIST.include?(k) }
|
89
|
+
end
|
90
|
+
end
|
91
|
+
end
|
@@ -0,0 +1,138 @@
|
|
1
|
+
# WARNING: This code is auto-generated from the BaseCRM API Discovery JSON Schema
|
2
|
+
|
3
|
+
module BaseCRM
|
4
|
+
class ContactsService
|
5
|
+
OPTS_KEYS_TO_PERSIST = Set[:address, :contact_id, :custom_fields, :customer_status, :description, :email, :facebook, :fax, :first_name, :industry, :is_organization, :last_name, :linkedin, :mobile, :name, :owner_id, :phone, :prospect_status, :skype, :tags, :title, :twitter, :website]
|
6
|
+
|
7
|
+
def initialize(client)
|
8
|
+
@client = client
|
9
|
+
end
|
10
|
+
|
11
|
+
# Retrieve all contacts
|
12
|
+
#
|
13
|
+
# get '/contacts'
|
14
|
+
#
|
15
|
+
# If you want to use filtering or sorting (see #where).
|
16
|
+
# @return [Enumerable] Paginated resource you can use to iterate over all the resources.
|
17
|
+
def all
|
18
|
+
PaginatedResource.new(self)
|
19
|
+
end
|
20
|
+
|
21
|
+
# Retrieve all contacts
|
22
|
+
#
|
23
|
+
# get '/contacts'
|
24
|
+
#
|
25
|
+
# Returns all contacts available to the user according to the parameters provided
|
26
|
+
#
|
27
|
+
# @param options [Hash] Search options
|
28
|
+
# @option options [String] :"address[city]" City name.
|
29
|
+
# @option options [String] :"address[country]" Country name.
|
30
|
+
# @option options [String] :"address[postal_code]" Zip code or equivalent
|
31
|
+
# @option options [Integer] :contact_id The unique identifier of the organization that the contact belongs to.
|
32
|
+
# @option options [String] :email Email address of the contact.
|
33
|
+
# @option options [String] :first_name First name of the contact.
|
34
|
+
# @option options [String] :ids Comma-separated list of the IDs for the contacts you want to be returned in your request.
|
35
|
+
# @option options [Boolean] :is_organization Indicates whether or not this contact refers to an organization or an individual.
|
36
|
+
# @option options [String] :last_name Last name of the contact.
|
37
|
+
# @option options [String] :name Name of the contact.
|
38
|
+
# @option options [Integer] :page (1) The page number to start from. Page numbering is 1-based and omitting the `page` parameter will return the first page.
|
39
|
+
# @option options [Integer] :per_page (25) The number of records to return per page. Default limit is *25* and maximum number that can be returned is *100*.
|
40
|
+
# @option options [String] :sort_by (last_name:asc) A field to sort by. **Default** ordering is **ascending**. If you want to change the sort order to descending, append `:desc` to the field e.g. `sort_by=last_name:desc`.
|
41
|
+
# @return [Array<Contact>] The list of Contacts for the first page, unless otherwise specified.
|
42
|
+
def where(options = {})
|
43
|
+
_, _, root = @client.get("/contacts", options)
|
44
|
+
|
45
|
+
root[:items].map{ |item| Contact.new(item[:data]) }
|
46
|
+
end
|
47
|
+
|
48
|
+
|
49
|
+
# Create a contact
|
50
|
+
#
|
51
|
+
# post '/contacts'
|
52
|
+
#
|
53
|
+
# Create a new contact
|
54
|
+
# A contact may represent a single individual or an organization
|
55
|
+
#
|
56
|
+
# @param contact [Contact, Hash] Either object of the Contact type or Hash. This object's attributes describe the object to be created.
|
57
|
+
# @return [Contact] The resulting object represting created resource.
|
58
|
+
def create(contact)
|
59
|
+
validate_type!(contact)
|
60
|
+
|
61
|
+
attributes = sanitize(contact)
|
62
|
+
_, _, root = @client.post("/contacts", attributes)
|
63
|
+
|
64
|
+
Contact.new(root[:data])
|
65
|
+
end
|
66
|
+
|
67
|
+
|
68
|
+
# Retrieve a single contact
|
69
|
+
#
|
70
|
+
# get '/contacts/{id}'
|
71
|
+
#
|
72
|
+
# Returns a single contact available to the user, according to the unique contact ID provided
|
73
|
+
# If the specified contact does not exist, the request will return an error
|
74
|
+
#
|
75
|
+
# @param id [Integer] Unique identifier of a Contact
|
76
|
+
# @return [Contact] Searched resource object.
|
77
|
+
def find(id)
|
78
|
+
_, _, root = @client.get("/contacts/#{id}")
|
79
|
+
|
80
|
+
Contact.new(root[:data])
|
81
|
+
end
|
82
|
+
|
83
|
+
|
84
|
+
# Update a contact
|
85
|
+
#
|
86
|
+
# put '/contacts/{id}'
|
87
|
+
#
|
88
|
+
# Updates contact information
|
89
|
+
# If the specified contact does not exist, the request will return an error
|
90
|
+
# **Notice** When updating contact tags, you need to provide all tags
|
91
|
+
# Any missing tag will be removed from a contact's tags
|
92
|
+
#
|
93
|
+
# @param contact [Contact, Hash] Either object of the Contact type or Hash. This object's attributes describe the object to be updated.
|
94
|
+
# @return [Contact] The resulting object represting updated resource.
|
95
|
+
def update(contact)
|
96
|
+
validate_type!(contact)
|
97
|
+
params = extract_params!(contact, :id)
|
98
|
+
id = params[:id]
|
99
|
+
|
100
|
+
attributes = sanitize(contact)
|
101
|
+
_, _, root = @client.put("/contacts/#{id}", attributes)
|
102
|
+
|
103
|
+
Contact.new(root[:data])
|
104
|
+
end
|
105
|
+
|
106
|
+
|
107
|
+
# Delete a contact
|
108
|
+
#
|
109
|
+
# delete '/contacts/{id}'
|
110
|
+
#
|
111
|
+
# Delete an existing contact
|
112
|
+
# If the specified contact does not exist, the request will return an error
|
113
|
+
# This operation cannot be undone
|
114
|
+
#
|
115
|
+
# @param id [Integer] Unique identifier of a Contact
|
116
|
+
# @return [Boolean] Status of the operation.
|
117
|
+
def destroy(id)
|
118
|
+
status, _, _ = @client.delete("/contacts/#{id}")
|
119
|
+
status == 204
|
120
|
+
end
|
121
|
+
|
122
|
+
|
123
|
+
private
|
124
|
+
def validate_type!(contact)
|
125
|
+
raise TypeError unless contact.is_a?(Contact) || contact.is_a?(Hash)
|
126
|
+
end
|
127
|
+
|
128
|
+
def extract_params!(contact, *args)
|
129
|
+
params = contact.to_h.select{ |k, _| args.include?(k) }
|
130
|
+
raise ArgumentError, "one of required attributes is missing. Expected: #{args.join(',')}" if params.count != args.length
|
131
|
+
params
|
132
|
+
end
|
133
|
+
|
134
|
+
def sanitize(contact)
|
135
|
+
contact.to_h.select { |k, _| OPTS_KEYS_TO_PERSIST.include?(k) }
|
136
|
+
end
|
137
|
+
end
|
138
|
+
end
|
@@ -0,0 +1,137 @@
|
|
1
|
+
# WARNING: This code is auto-generated from the BaseCRM API Discovery JSON Schema
|
2
|
+
|
3
|
+
module BaseCRM
|
4
|
+
class DealsService
|
5
|
+
OPTS_KEYS_TO_PERSIST = Set[:contact_id, :currency, :custom_fields, :hot, :loss_reason_id, :name, :owner_id, :source_id, :stage_id, :tags, :value]
|
6
|
+
|
7
|
+
def initialize(client)
|
8
|
+
@client = client
|
9
|
+
end
|
10
|
+
|
11
|
+
# Retrieve all deals
|
12
|
+
#
|
13
|
+
# get '/deals'
|
14
|
+
#
|
15
|
+
# If you want to use filtering or sorting (see #where).
|
16
|
+
# @return [Enumerable] Paginated resource you can use to iterate over all the resources.
|
17
|
+
def all
|
18
|
+
PaginatedResource.new(self)
|
19
|
+
end
|
20
|
+
|
21
|
+
# Retrieve all deals
|
22
|
+
#
|
23
|
+
# get '/deals'
|
24
|
+
#
|
25
|
+
# Returns all deals available to the user according to the parameters provided
|
26
|
+
#
|
27
|
+
# @param options [Hash] Search options
|
28
|
+
# @option options [Integer] :contact_id Unique identifier of a primary contact.
|
29
|
+
# @option options [Integer] :creator_id Unique identifier of the user the deal was created by. Returns all deals created by the user.
|
30
|
+
# @option options [Boolean] :hot Indicator of whether or not the deal is hot.
|
31
|
+
# @option options [String] :ids Comma-separated list of deal IDs to be returned in a request.
|
32
|
+
# @option options [Integer] :organization_id Unique identifier of an organization.
|
33
|
+
# @option options [Integer] :owner_id Unique identifier of the user the deal is owned by. Returns all deals owned by the user.
|
34
|
+
# @option options [Integer] :page (1) Page number to start from. Page numbering starts at 1, and omitting the `page` parameter will return the first page.
|
35
|
+
# @option options [Integer] :per_page (25) Number of records to return per page. Default limit is *25* and the maximum number that can be returned is *100*.
|
36
|
+
# @option options [String] :sort_by (id:asc) A field to sort by. **Default** ordering is **ascending**. If you want to change the sort ordering to descending, append `:desc` to the field e.g. `sort_by=value:desc`.
|
37
|
+
# @option options [Integer] :source_id Id of the Source.
|
38
|
+
# @option options [Integer] :stage_id Id of the Stage.
|
39
|
+
# @return [Array<Deal>] The list of Deals for the first page, unless otherwise specified.
|
40
|
+
def where(options = {})
|
41
|
+
_, _, root = @client.get("/deals", options)
|
42
|
+
|
43
|
+
root[:items].map{ |item| Deal.new(item[:data]) }
|
44
|
+
end
|
45
|
+
|
46
|
+
|
47
|
+
# Create a deal
|
48
|
+
#
|
49
|
+
# post '/deals'
|
50
|
+
#
|
51
|
+
# Create a new deal
|
52
|
+
#
|
53
|
+
# @param deal [Deal, Hash] Either object of the Deal type or Hash. This object's attributes describe the object to be created.
|
54
|
+
# @return [Deal] The resulting object represting created resource.
|
55
|
+
def create(deal)
|
56
|
+
validate_type!(deal)
|
57
|
+
|
58
|
+
attributes = sanitize(deal)
|
59
|
+
_, _, root = @client.post("/deals", attributes)
|
60
|
+
|
61
|
+
Deal.new(root[:data])
|
62
|
+
end
|
63
|
+
|
64
|
+
|
65
|
+
# Retrieve a single deal
|
66
|
+
#
|
67
|
+
# get '/deals/{id}'
|
68
|
+
#
|
69
|
+
# Returns a single deal available to the user, according to the unique deal ID provided
|
70
|
+
# If the specified deal does not exist, the request will return an error
|
71
|
+
#
|
72
|
+
# @param id [Integer] Unique identifier of a Deal
|
73
|
+
# @return [Deal] Searched resource object.
|
74
|
+
def find(id)
|
75
|
+
_, _, root = @client.get("/deals/#{id}")
|
76
|
+
|
77
|
+
Deal.new(root[:data])
|
78
|
+
end
|
79
|
+
|
80
|
+
|
81
|
+
# Update a deal
|
82
|
+
#
|
83
|
+
# put '/deals/{id}'
|
84
|
+
#
|
85
|
+
# Updates deal information
|
86
|
+
# If the specified deal does not exist, the request will return an error
|
87
|
+
# <figure class="notice">
|
88
|
+
# In order to modify tags used on a record, you need to supply the entire set
|
89
|
+
# `tags` are replaced every time they are used in a request
|
90
|
+
# </figure>
|
91
|
+
#
|
92
|
+
# @param deal [Deal, Hash] Either object of the Deal type or Hash. This object's attributes describe the object to be updated.
|
93
|
+
# @return [Deal] The resulting object represting updated resource.
|
94
|
+
def update(deal)
|
95
|
+
validate_type!(deal)
|
96
|
+
params = extract_params!(deal, :id)
|
97
|
+
id = params[:id]
|
98
|
+
|
99
|
+
attributes = sanitize(deal)
|
100
|
+
_, _, root = @client.put("/deals/#{id}", attributes)
|
101
|
+
|
102
|
+
Deal.new(root[:data])
|
103
|
+
end
|
104
|
+
|
105
|
+
|
106
|
+
# Delete a deal
|
107
|
+
#
|
108
|
+
# delete '/deals/{id}'
|
109
|
+
#
|
110
|
+
# Delete an existing deal and remove all of the associated contacts from the deal in a single call
|
111
|
+
# If the specified deal does not exist, the request will return an error
|
112
|
+
# This operation cannot be undone
|
113
|
+
#
|
114
|
+
# @param id [Integer] Unique identifier of a Deal
|
115
|
+
# @return [Boolean] Status of the operation.
|
116
|
+
def destroy(id)
|
117
|
+
status, _, _ = @client.delete("/deals/#{id}")
|
118
|
+
status == 204
|
119
|
+
end
|
120
|
+
|
121
|
+
|
122
|
+
private
|
123
|
+
def validate_type!(deal)
|
124
|
+
raise TypeError unless deal.is_a?(Deal) || deal.is_a?(Hash)
|
125
|
+
end
|
126
|
+
|
127
|
+
def extract_params!(deal, *args)
|
128
|
+
params = deal.to_h.select{ |k, _| args.include?(k) }
|
129
|
+
raise ArgumentError, "one of required attributes is missing. Expected: #{args.join(',')}" if params.count != args.length
|
130
|
+
params
|
131
|
+
end
|
132
|
+
|
133
|
+
def sanitize(deal)
|
134
|
+
deal.to_h.select { |k, _| OPTS_KEYS_TO_PERSIST.include?(k) }
|
135
|
+
end
|
136
|
+
end
|
137
|
+
end
|
@@ -0,0 +1,140 @@
|
|
1
|
+
# WARNING: This code is auto-generated from the BaseCRM API Discovery JSON Schema
|
2
|
+
|
3
|
+
module BaseCRM
|
4
|
+
class LeadsService
|
5
|
+
OPTS_KEYS_TO_PERSIST = Set[:address, :custom_fields, :description, :email, :facebook, :fax, :first_name, :industry, :last_name, :linkedin, :mobile, :organization_name, :owner_id, :phone, :skype, :status, :tags, :title, :twitter, :website]
|
6
|
+
|
7
|
+
def initialize(client)
|
8
|
+
@client = client
|
9
|
+
end
|
10
|
+
|
11
|
+
# Retrieve all leads
|
12
|
+
#
|
13
|
+
# get '/leads'
|
14
|
+
#
|
15
|
+
# If you want to use filtering or sorting (see #where).
|
16
|
+
# @return [Enumerable] Paginated resource you can use to iterate over all the resources.
|
17
|
+
def all
|
18
|
+
PaginatedResource.new(self)
|
19
|
+
end
|
20
|
+
|
21
|
+
# Retrieve all leads
|
22
|
+
#
|
23
|
+
# get '/leads'
|
24
|
+
#
|
25
|
+
# Returns all leads available to the user, according to the parameters provided
|
26
|
+
#
|
27
|
+
# @param options [Hash] Search options
|
28
|
+
# @option options [String] :"address[city]" City name.
|
29
|
+
# @option options [String] :"address[country]" Country name.
|
30
|
+
# @option options [String] :"address[postal_code]" Zip or Postal code
|
31
|
+
# @option options [Integer] :creator_id User ID. Returns all leads created by that user.
|
32
|
+
# @option options [String] :first_name First name of the lead.
|
33
|
+
# @option options [String] :ids Comma-separated list of lead IDs to be returned in a request.
|
34
|
+
# @option options [String] :last_name Last name of the lead.
|
35
|
+
# @option options [String] :organization_name Organization name of the lead.
|
36
|
+
# @option options [Integer] :owner_id User ID. Returns all leads owned by that user.
|
37
|
+
# @option options [Integer] :page (1) Page number to start from. Page numbering starts at 1 and omitting the `page` parameter will return the first page.
|
38
|
+
# @option options [Integer] :per_page (25) Number of records to return per page. The default limit is *25* and the maximum number that can be returned is *100*.
|
39
|
+
# @option options [String] :sort_by (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=last_name:desc`.
|
40
|
+
# @option options [String] :status Status of the lead.
|
41
|
+
# @return [Array<Lead>] The list of Leads for the first page, unless otherwise specified.
|
42
|
+
def where(options = {})
|
43
|
+
_, _, root = @client.get("/leads", options)
|
44
|
+
|
45
|
+
root[:items].map{ |item| Lead.new(item[:data]) }
|
46
|
+
end
|
47
|
+
|
48
|
+
|
49
|
+
# Create a lead
|
50
|
+
#
|
51
|
+
# post '/leads'
|
52
|
+
#
|
53
|
+
# Creates a new lead
|
54
|
+
# A lead may represent a single individual or an organization
|
55
|
+
#
|
56
|
+
# @param lead [Lead, Hash] Either object of the Lead type or Hash. This object's attributes describe the object to be created.
|
57
|
+
# @return [Lead] The resulting object represting created resource.
|
58
|
+
def create(lead)
|
59
|
+
validate_type!(lead)
|
60
|
+
|
61
|
+
attributes = sanitize(lead)
|
62
|
+
_, _, root = @client.post("/leads", attributes)
|
63
|
+
|
64
|
+
Lead.new(root[:data])
|
65
|
+
end
|
66
|
+
|
67
|
+
|
68
|
+
# Retrieve a single lead
|
69
|
+
#
|
70
|
+
# get '/leads/{id}'
|
71
|
+
#
|
72
|
+
# Returns a single lead available to the user, according to the unique lead ID provided
|
73
|
+
# If the specified lead does not exist, this query returns an error
|
74
|
+
#
|
75
|
+
# @param id [Integer] Unique identifier of a Lead
|
76
|
+
# @return [Lead] Searched resource object.
|
77
|
+
def find(id)
|
78
|
+
_, _, root = @client.get("/leads/#{id}")
|
79
|
+
|
80
|
+
Lead.new(root[:data])
|
81
|
+
end
|
82
|
+
|
83
|
+
|
84
|
+
# Update a lead
|
85
|
+
#
|
86
|
+
# put '/leads/{id}'
|
87
|
+
#
|
88
|
+
# Updates lead information
|
89
|
+
# If the specified lead does not exist, this query returns an error
|
90
|
+
# <figure class="notice">
|
91
|
+
# In order to modify tags, you need to supply the entire set of tags
|
92
|
+
# `tags` are replaced every time they are used in a request
|
93
|
+
# </figure>
|
94
|
+
#
|
95
|
+
# @param lead [Lead, Hash] Either object of the Lead type or Hash. This object's attributes describe the object to be updated.
|
96
|
+
# @return [Lead] The resulting object represting updated resource.
|
97
|
+
def update(lead)
|
98
|
+
validate_type!(lead)
|
99
|
+
params = extract_params!(lead, :id)
|
100
|
+
id = params[:id]
|
101
|
+
|
102
|
+
attributes = sanitize(lead)
|
103
|
+
_, _, root = @client.put("/leads/#{id}", attributes)
|
104
|
+
|
105
|
+
Lead.new(root[:data])
|
106
|
+
end
|
107
|
+
|
108
|
+
|
109
|
+
# Delete a lead
|
110
|
+
#
|
111
|
+
# delete '/leads/{id}'
|
112
|
+
#
|
113
|
+
# Delete an existing lead
|
114
|
+
# If the specified lead does not exist, this query returns an error
|
115
|
+
# This operation cannot be undone
|
116
|
+
#
|
117
|
+
# @param id [Integer] Unique identifier of a Lead
|
118
|
+
# @return [Boolean] Status of the operation.
|
119
|
+
def destroy(id)
|
120
|
+
status, _, _ = @client.delete("/leads/#{id}")
|
121
|
+
status == 204
|
122
|
+
end
|
123
|
+
|
124
|
+
|
125
|
+
private
|
126
|
+
def validate_type!(lead)
|
127
|
+
raise TypeError unless lead.is_a?(Lead) || lead.is_a?(Hash)
|
128
|
+
end
|
129
|
+
|
130
|
+
def extract_params!(lead, *args)
|
131
|
+
params = lead.to_h.select{ |k, _| args.include?(k) }
|
132
|
+
raise ArgumentError, "one of required attributes is missing. Expected: #{args.join(',')}" if params.count != args.length
|
133
|
+
params
|
134
|
+
end
|
135
|
+
|
136
|
+
def sanitize(lead)
|
137
|
+
lead.to_h.select { |k, _| OPTS_KEYS_TO_PERSIST.include?(k) }
|
138
|
+
end
|
139
|
+
end
|
140
|
+
end
|