Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for resources: custom_fields #76

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
## CHANGELOG

v1.39 (2020-10-17)
**Features and Improvements**

* Added new models support: CustomField

v1.38 (2019-05-11)
**Features and Improvements**

Expand Down
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,21 @@ Actions:
* Update a contact - `client.contacts.update`
* Delete a contact - `client.contacts.destroy`

### Custom Fields

```ruby
client = BaseCRM::Client.new(access_token: "<YOUR_PERSONAL_ACCESS_TOKEN>")
client.custom_fields # => BaseCRM::CustomFieldsService
```

Actions:
* Retrieve all custom fields - `client.custom_fields.all('contact')`
* `all` excepts the following `resource_type` parameters:
* `lead`
* `contact`
* `deal`
* `prospect_and_customer`

### Deal

```ruby
Expand Down
10 changes: 10 additions & 0 deletions lib/basecrm.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
require 'basecrm/models/call'
require 'basecrm/models/call_outcome'
require 'basecrm/models/contact'
require 'basecrm/models/custom_field'
require 'basecrm/models/deal'
require 'basecrm/models/deal_source'
require 'basecrm/models/deal_unqualified_reason'
Expand Down Expand Up @@ -48,6 +49,7 @@
require 'basecrm/services/calls_service'
require 'basecrm/services/call_outcomes_service'
require 'basecrm/services/contacts_service'
require 'basecrm/services/custom_fields_service'
require 'basecrm/services/deals_service'
require 'basecrm/services/deal_sources_service'
require 'basecrm/services/deal_unqualified_reasons_service'
Expand Down Expand Up @@ -109,6 +111,14 @@ def accounts
@accounts ||= AccountsService.new(@http_client)
end

# Access all Custom Field related actions.
# @see CustomFieldsService
# @see CustomField
#
# @return [CustomFieldsService] Service object for resources.
def custom_fields
@custom_fields ||= CustomFieldsService.new(@http_client)
end
# Access all AssociatedContacts related actions.
# @see AssociatedContactsService
# @see AssociatedContact
Expand Down
20 changes: 20 additions & 0 deletions lib/basecrm/models/custom_field.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
module BaseCRM
class CustomField < Model
# @!attribute [r] created_at
# @return [DateTime] Date and time of the account's creation in UTC (ISO8601 format).
# @!attribute [r] updated_at
# @return [DateTime] Date and time of the last update in UTC (ISO8601 format).
# @!attribute [r] id
# @return [Integer] Unique identifier of the custom field.
# @!attribute [r] name
# @return [String] Name of the custom field.
# @!attribute [r] type
# @return [String] Type of the custom field.
# @!attribute [r] choices
# @return [Array] Represents choices that can be used for this particular custom field (applicable only for multi_select_list and list).
# @!attribute [r] for_organisation
# @return [Boolean] Whether custom field should be available on Contact: Company (applicable only for resource_type=contact).
# @!attribute [r] for_contact
# @return [Boolean] Whether custom field should be available on Contact: Person (applicable only for resource_type=contact).
end
end
37 changes: 37 additions & 0 deletions lib/basecrm/services/custom_fields_service.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
module BaseCRM
class CustomFieldsService
RESOURCE_TYPES = %w[lead contact deal prospect_and_customer].freeze

def initialize(client)
@client = client
end

# Retrieve custom field details for a resource type
#
# get '/:resource_type/custom_fields'
#
# Returns detailed information about your custom fields
#
# @return [Array] Array of CustomField
def all(resource_type)
raise IncorrectResourceType unless RESOURCE_TYPES.include?(resource_type)

_, _, root = @client.get("/#{resource_type}/custom_fields", {})
root[:items].map { |item| CustomField.new(item[:data]) }
end

private

def validate_type!(custom_field)
raise TypeError unless custom_field.is_a?(CustomField) || custom_field.is_a?(Hash)
end

def extract_params!(custom_field, *args)
params = custom_field.to_h.select { |k, _| args.include?(k) }
raise ArgumentError, "one of required attributes is missing. Expected: #{args.join(',')}" if params.count != args.length
params
end

class IncorrectResourceType < StandardError; end
end
end
2 changes: 1 addition & 1 deletion lib/basecrm/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module BaseCRM
VERSION = "1.3.8"
VERSION = "1.3.9"
end
21 changes: 21 additions & 0 deletions spec/services/custom_fields_service_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
require 'spec_helper'

describe BaseCRM::CustomFieldsService do
describe 'Responds to' do
subject { client.custom_fields }

it { should respond_to :all }
end

describe :all do
it "returns an array" do
expect(client.custom_fields.all('contact')).to be_instance_of(Array)
end

it 'raises IncorrectResourceType if resource type not approved' do
expect do
client.custom_fields.all('not_valid')
end.to raise_exception(BaseCRM::CustomFieldsService::IncorrectResourceType)
end
end
end