Skip to content
This repository was archived by the owner on Dec 12, 2023. It is now read-only.

Commit f42c751

Browse files
committed
Cleanup and refactor code. Remove specs
1 parent 75728e8 commit f42c751

20 files changed

+204
-332
lines changed

lib/config.rb

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,13 @@ module FreshdeskApiV2
22
class Config
33
attr_accessor :domain, :api_key, :username, :password
44

5+
def initialize(hash = {})
6+
@domain = hash[:domain]
7+
@api_key = hash[:api_key]
8+
@username = hash[:username]
9+
@password = hash[:password]
10+
end
11+
512
def validate!
613
validate_domain!
714
validate_credentials!
@@ -10,9 +17,7 @@ def validate!
1017
private
1118

1219
def validate_domain!
13-
if domain.nil? || domain.to_s.length == 0
14-
raise ConfigurationException, 'Domain is required.'
15-
end
20+
raise ConfigurationException, 'Domain is required.' if domain.nil? || domain.to_s.length == 0
1621
end
1722

1823
def validate_credentials!

lib/freshdesk_api_v2.rb

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,11 @@
66
require 'uri'
77
require 'cgi'
88
require 'json'
9+
require 'freshdesk_api_v2/exceptions'
910
require 'freshdesk_api_v2/search_args'
1011
require 'freshdesk_api_v2/utils'
11-
require 'freshdesk_api_v2/entity'
12+
require 'freshdesk_api_v2/base'
13+
require 'freshdesk_api_v2/list_only_base'
1214
require 'freshdesk_api_v2/contacts'
1315
require 'freshdesk_api_v2/companies'
1416
require 'freshdesk_api_v2/contact_fields'
@@ -17,20 +19,6 @@
1719
require 'freshdesk_api_v2/client'
1820

1921
module FreshdeskApiV2
20-
class ConfigurationException < StandardError; end
21-
class PaginationException < StandardError; end
22-
class SearchException < StandardError; end
23-
class CreationException < StandardError; end
24-
class UpdateException < StandardError; end
25-
26-
class InvalidSearchException < StandardError
27-
attr_reader :errors
28-
def initialize(error_description, errors)
29-
super(error_description)
30-
@errors = errors
31-
end
32-
end
33-
3422
def self.configure(&block)
3523
@config = Config.new
3624
block.call(@config)

lib/freshdesk_api_v2/entity.rb renamed to lib/freshdesk_api_v2/base.rb

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
module FreshdeskApiV2
2-
class Entity
2+
class Base
33
def initialize(http)
44
@http = http
55
end
@@ -29,11 +29,13 @@ def show(id)
2929

3030
def create(attributes)
3131
validate_create_attributes!(attributes)
32+
attributes = prepare_attributes!(attributes)
3233
http_create(attributes)
3334
end
3435

3536
def update(id, attributes)
3637
validate_update_attributes!(attributes)
38+
attributes = prepare_attributes!(attributes)
3739
http_update(id, attributes)
3840
end
3941

@@ -62,6 +64,16 @@ def validate_pagination!(first_page, last_page)
6264
raise PaginationException, 'last_page must be a number greater than or equal to first_page' if last_page.to_i < first_page.to_i
6365
end
6466

67+
def prepare_attributes!(attributes)
68+
clean = attributes.reject { |key, value | !allowed_attributes.include?(key) || value.nil? }
69+
custom_fields = clean['custom_fields']
70+
if !custom_fields.nil? && custom_fields.any?
71+
custom_fields = custom_fields.reject { |_, value| value.nil? }
72+
clean['custom_fields'] = custom_fields if !custom_fields.nil? && custom_fields.any?
73+
end
74+
clean
75+
end
76+
6577
private
6678

6779
def http_show(id)
@@ -76,7 +88,7 @@ def http_create(attributes)
7688

7789
def http_destroy(id)
7890
response = @http.delete("#{api_url}/#{id}")
79-
response.code
91+
response.status
8092
end
8193

8294
def http_update(id, attributes)

lib/freshdesk_api_v2/client.rb

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,16 @@
11
module FreshdeskApiV2
22
class Client
3+
attr_reader :configuration
4+
35
def initialize(configuration = nil)
4-
configuration = FreshdeskApiV2.configuration if configuration.nil?
5-
configuration.validate!
6-
@http = Http.new(configuration)
6+
if !configuration.nil?
7+
@configuration = Config.new(configuration) if configuration.is_a?(Hash)
8+
@configuration = configuration if configuration.is_a?(FreshdeskApiV2::Config)
9+
else
10+
@configuration = FreshdeskApiV2.configuration
11+
end
12+
@configuration.validate!
13+
@http = Http.new(@configuration)
714
end
815

916
def contacts

lib/freshdesk_api_v2/companies.rb

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,25 @@
11
module FreshdeskApiV2
2-
class Companies < Entity
2+
class Companies < Base
3+
ALLOWED_ATTRIBUTES = %w[
4+
custom_fields
5+
description
6+
domains
7+
name
8+
note
9+
health_score
10+
account_tier
11+
renewal_date
12+
industry
13+
].freeze
14+
315
protected
416

517
def endpoint
618
'companies'
719
end
20+
21+
def allowed_attributes
22+
ALLOWED_ATTRIBUTES
23+
end
824
end
925
end

lib/freshdesk_api_v2/company_fields.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
module FreshdeskApiV2
2-
class CompanyFields < Entity
2+
class CompanyFields < ListOnlyBase
33
protected
44

55
def endpoint

lib/freshdesk_api_v2/contact_fields.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
module FreshdeskApiV2
2-
class ContactFields < Entity
2+
class ContactFields < ListOnlyBase
33
protected
44

55
def endpoint

lib/freshdesk_api_v2/contacts.rb

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,33 @@
11
module FreshdeskApiV2
2-
class Contacts < Entity
2+
class Contacts < Base
3+
ALLOWED_ATTRIBUTES = [
4+
:name,
5+
:phone,
6+
:email,
7+
:mobile,
8+
:twitter_id,
9+
:unique_external_id,
10+
:other_emails,
11+
:company_id,
12+
:view_all_tickets,
13+
:other_companies,
14+
:address,
15+
:avatar,
16+
:custom_fields,
17+
:description,
18+
:job_title,
19+
:tags,
20+
:timezone
21+
].freeze
22+
323
protected
424

525
def endpoint
626
'contacts'
727
end
28+
29+
def allowed_attributes
30+
ALLOWED_ATTRIBUTES
31+
end
832
end
933
end

lib/freshdesk_api_v2/exceptions.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
module FreshdeskApiV2
2+
class ConfigurationException < StandardError; end
3+
class PaginationException < StandardError; end
4+
class SearchException < StandardError; end
5+
class CreationException < StandardError; end
6+
class UpdateException < StandardError; end
7+
8+
class InvalidSearchException < StandardError
9+
attr_reader :errors
10+
def initialize(error_description, errors)
11+
super(error_description)
12+
@errors = errors
13+
end
14+
end
15+
end

lib/freshdesk_api_v2/http.rb

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -46,23 +46,23 @@ def paginate(url, last_page, collection = [])
4646
end
4747

4848
def get(url)
49-
res = construct_http_client(url, accept_headers)
49+
res = construct_http_client(url)
5050
res.get
5151
end
5252

5353
def delete(url)
54-
res = construct_http_client(url, accept_headers)
54+
res = construct_http_client(url)
5555
res.delete
5656
end
5757

5858
def put(url, attributes)
59-
res = construct_http_client(url, content_type_headers)
60-
res.put(attributes.to_json)
59+
res = construct_http_client(url)
60+
res.put(body: attributes.to_json)
6161
end
6262

6363
def post(url, attributes)
64-
res = construct_http_client(url, content_type_headers)
65-
res.post(attributes.to_json)
64+
res = construct_http_client(url)
65+
res.post(body: attributes.to_json)
6666
end
6767

6868
def domain
@@ -93,12 +93,11 @@ def next_page(url)
9393
url[Utils::PAGE_REGEX, 1]
9494
end
9595

96-
def accept_headers
97-
{ accept: 'application/json' }
98-
end
99-
100-
def content_type_headers
101-
{ content_type: 'application/json' }
96+
def headers
97+
{
98+
'Accept' => 'application/json',
99+
'Content-Type' => 'application/json'
100+
}
102101
end
103102

104103
def password_or_x
@@ -117,7 +116,7 @@ def username_or_api_key
117116
end
118117
end
119118

120-
def construct_http_client(url, headers)
119+
def construct_http_client(url)
121120
Excon.new(url,
122121
uri_parser: Addressable::URI,
123122
headers: headers,

0 commit comments

Comments
 (0)