@@ -4,43 +4,44 @@ def initialize(http)
4
4
@http = http
5
5
end
6
6
7
+ # pagination_options: page: integer > 0, per_page: integer > 0 && <= 100
7
8
def list ( pagination_options = { } )
8
- per_page = pagination_options [ :per_page ] || Utils ::MAX_PAGE_SIZE
9
- raise PaginationException , "Max per page is #{ Utils ::MAX_PAGE_SIZE } " if per_page . to_i > Utils ::MAX_PAGE_SIZE
10
- first_page , last_page = extract_list_pagination ( pagination_options )
11
- validate_list_pagination! ( first_page , last_page )
12
- paginated_get ( first_page , last_page , per_page )
9
+ page = pagination_options [ :page ]
10
+ per_page = pagination_options [ :per_page ]
11
+ validate_list_pagination! ( page , per_page )
12
+ @http . get ( "#{ endpoint } ?page=#{ page } &per_page=#{ per_page } " )
13
13
end
14
14
15
15
# TODO - Note that queries that by email or things that have special characters to not work yet in
16
- # Freshdesk
16
+ # Freshdesk. This appears to be a bug on their side.
17
+ # query: An instance of FreshdeskApiV2::SearchArgs
18
+ # pagination_options: page: integer > 0 && <= 10, per_page: integer > 0 && <= 30
17
19
def search ( query , pagination_options = { } )
18
- raise SearchException , 'You must provide a query' if query . nil?
19
- raise SearchException , 'You must provide a query of type FreshdeskApiV2::SearchArgs' unless query . is_a? ( FreshdeskApiV2 ::SearchArgs )
20
- raise SearchException , 'You must provide a query' unless query . valid?
21
- first_page , last_page = extract_search_pagination ( pagination_options )
22
- validate_search_pagination! ( first_page , last_page )
23
- paginated_search ( query . to_query , first_page , last_page )
20
+ validate_search_query! ( query )
21
+ page = pagination_options [ :page ]
22
+ per_page = pagination_options [ :per_page ]
23
+ validate_search_pagination! ( page , per_page )
24
+ @http . get ( "#{ endpoint } /search/#{ endpoint } ?page=#{ page } &query=#{ query } " )
24
25
end
25
26
26
- def show ( id )
27
- get ( id )
27
+ def get ( id )
28
+ @http . get ( "/ #{ endpoint } / #{ id } " )
28
29
end
29
30
30
31
def create ( attributes )
31
32
validate_create_attributes! ( attributes )
32
33
attributes = prepare_attributes! ( attributes )
33
- post ( attributes )
34
+ @http . post ( endpoint , attributes )
34
35
end
35
36
36
37
def update ( id , attributes )
37
38
validate_update_attributes! ( attributes )
38
39
attributes = prepare_attributes! ( attributes )
39
- put ( id , attributes )
40
+ @http . put ( " #{ endpoint } / #{ id } " , attributes )
40
41
end
41
42
42
43
def destroy ( id )
43
- delete ( id )
44
+ @http . delete ( " #{ endpoint } / #{ id } " )
44
45
end
45
46
46
47
protected
@@ -59,16 +60,19 @@ def validate_update_attributes!(attributes)
59
60
raise UpdateException , 'Please provide attributes' if attributes . nil? || attributes . count == 0
60
61
end
61
62
62
- def validate_list_pagination! ( first_page , last_page )
63
- raise PaginationException , 'first_page must be a number greater than 0' if first_page . to_i <= 0
64
- raise PaginationException , 'last_page must be a number greater than or equal to first_page' if last_page . to_i < first_page . to_i
63
+ def validate_list_pagination! ( page , per_page )
64
+ raise PaginationException , 'page must be a number greater than 0' if !page . nil? && page . to_i <= 0
65
+ unless per_page . nil?
66
+ raise PaginationException , 'per_page must be a number greater than 0' if per_page . to_i <= 0
67
+ raise PaginationException , "per_page must be a number less than or equal to #{ Utils ::MAX_LIST_PER_PAGE } " if per_page . to_i > Utils ::MAX_LIST_PER_PAGE
68
+ end
65
69
end
66
70
67
- def validate_search_pagination! ( first_page , last_page )
68
- raise PaginationException , 'first_page must be a number greater than 0' if first_page . to_i <= 0
69
- unless last_page . nil?
70
- raise PaginationException , "last_page cannot exceed #{ Utils :: MAX_SEARCH_PAGES } " if last_page . to_i > Utils :: MAX_SEARCH_PAGES
71
- raise PaginationException , 'last_page must be a number greater than or equal to first_page' if last_page . to_i < first_page . to_i
71
+ def validate_search_pagination! ( page , per_page )
72
+ raise PaginationException , 'page must be a number greater than 0' if ! page . nil? && page . to_i <= 0
73
+ unless per_page . nil?
74
+ raise PaginationException , 'per_page must be a number greater than 0' if per_page . to_i <= 0
75
+ raise PaginationException , "per_page must be a number less than or equal to #{ Utils :: MAX_SEARCH_PER_PAGE } " if per_page . to_i > Utils :: MAX_SEARCH_PER_PAGE
72
76
end
73
77
end
74
78
@@ -82,57 +86,10 @@ def prepare_attributes!(attributes)
82
86
clean
83
87
end
84
88
85
- private
86
-
87
- def get ( id )
88
- response = @http . get ( "#{ api_url } /#{ id } " )
89
- JSON . parse ( response . body )
90
- end
91
-
92
- def post ( attributes )
93
- response = @http . post ( "#{ api_url } " , attributes )
94
- JSON . parse ( response . body )
95
- end
96
-
97
- def delete ( id )
98
- response = @http . delete ( "#{ api_url } /#{ id } " )
99
- response . status
100
- end
101
-
102
- def put ( id , attributes )
103
- response = @http . put ( "#{ api_url } /#{ id } " , attributes )
104
- JSON . parse ( response . body )
105
- end
106
-
107
- def paginated_get ( first_page , last_page , per_page )
108
- url = "#{ api_url } ?page=#{ first_page } &per_page=#{ per_page } "
109
- @http . paginated_get ( url , last_page )
110
- end
111
-
112
- # For example, see: https://developers.freshdesk.com/api/#filter_contacts
113
- def paginated_search ( query , first_page , last_page )
114
- url = "#{ base_api_url } /search/#{ endpoint } ?page=#{ first_page } &query=#{ query } "
115
- @http . paginated_search ( url , last_page )
116
- end
117
-
118
- def extract_list_pagination ( options )
119
- first_page = options [ :first_page ] || Utils ::DEFAULT_PAGE
120
- last_page = options [ :last_page ] || Utils ::INTEGER_MAX
121
- [ first_page , last_page ]
122
- end
123
-
124
- def extract_search_pagination ( options )
125
- first_page = options [ :first_page ] || Utils ::DEFAULT_PAGE
126
- last_page = options [ :last_page ]
127
- [ first_page , last_page ]
128
- end
129
-
130
- def base_api_url
131
- "https://#{ @http . domain } .freshdesk.com/api/v2"
132
- end
133
-
134
- def api_url
135
- "#{ base_api_url } /#{ endpoint } "
89
+ def validate_search_query! ( query )
90
+ raise SearchException , 'You must provide a query' if query . nil?
91
+ raise SearchException , 'You must provide a query of type FreshdeskApiV2::SearchArgs' unless query . is_a? ( FreshdeskApiV2 ::SearchArgs )
92
+ raise SearchException , 'You must provide a query' unless query . valid?
136
93
end
137
94
end
138
95
end
0 commit comments