Skip to content

Commit df417ff

Browse files
committed
small refactoring and remove a few hash rockets
1 parent 51d0610 commit df417ff

File tree

3 files changed

+113
-111
lines changed

3 files changed

+113
-111
lines changed

lib/zendesk_api/collection.rb

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ module ZendeskAPI
88
# actually fetched until explicitly needed (e.g. #each, {#fetch}).
99
class Collection
1010
include ZendeskAPI::Sideloading
11-
include ZendeskAPI::Pagination
11+
include Pagination
1212

1313
# Options passed in that are automatically converted from an array to a comma-separated list.
1414
SPECIALLY_JOINED_PARAMS = [:ids, :only]
@@ -198,6 +198,16 @@ def all(start_page = @options["page"], &block)
198198
_all(start_page, &block)
199199
end
200200

201+
def each_page!(*args, &block)
202+
warn "ZendeskAPI::Collection#each_page! is deprecated, please use ZendeskAPI::Collection#all!"
203+
all!(*args, &block)
204+
end
205+
206+
def each_page(*args, &block)
207+
warn "ZendeskAPI::Collection#each_page is deprecated, please use ZendeskAPI::Collection#all"
208+
all(*args, &block)
209+
end
210+
201211
# Replaces the current (loaded or not) resources with the passed in collection
202212
# @option collection [Array] The collection to replace this one with
203213
# @raise [ArgumentError] if any resources passed in don't belong in this collection

lib/zendesk_api/pagination.rb

Lines changed: 80 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -1,107 +1,99 @@
11
module ZendeskAPI
2-
# Contains all methods related to pagination in an attempt to slim down collection.rb
3-
module Pagination
4-
DEFAULT_PAGE_SIZE = 100
5-
def more_results?(response)
6-
Helpers.present?(response["meta"]) && response["meta"]["has_more"]
7-
end
8-
alias has_more_results? more_results? # For backward compatibility with 1.33.0 and 1.34.0
9-
10-
def each_page!(*args, &block)
11-
warn "ZendeskAPI::Collection#each_page! is deprecated, please use ZendeskAPI::Collection#all!"
12-
all!(*args, &block)
13-
end
14-
15-
def each_page(*args, &block)
16-
warn "ZendeskAPI::Collection#each_page is deprecated, please use ZendeskAPI::Collection#all"
17-
all(*args, &block)
18-
end
19-
20-
# Changes the per_page option. Returns self, so it can be chained. No execution.
21-
# @return [Collection] self
22-
def per_page(count)
23-
clear_cache if count
24-
@options["per_page"] = count
25-
self
26-
end
2+
class Collection
3+
# Contains all methods related to pagination in an attempt to slim down collection.rb
4+
module Pagination
5+
DEFAULT_PAGE_SIZE = 100
6+
def more_results?(response)
7+
Helpers.present?(response["meta"]) && response["meta"]["has_more"]
8+
end
9+
alias has_more_results? more_results? # For backward compatibility with 1.33.0 and 1.34.0
10+
11+
# Changes the per_page option. Returns self, so it can be chained. No execution.
12+
# @return [Collection] self
13+
def per_page(count)
14+
clear_cache if count
15+
@options["per_page"] = count
16+
self
17+
end
2718

28-
# Changes the page option. Returns self, so it can be chained. No execution.
29-
# @return [Collection] self
30-
def page(number)
31-
clear_cache if number
32-
@options["page"] = number
33-
self
34-
end
19+
# Changes the page option. Returns self, so it can be chained. No execution.
20+
# @return [Collection] self
21+
def page(number)
22+
clear_cache if number
23+
@options["page"] = number
24+
self
25+
end
3526

36-
def first_page?
37-
!@prev_page
38-
end
27+
def first_page?
28+
!@prev_page
29+
end
3930

40-
def last_page?
41-
!@next_page || @next_page == @query
42-
end
31+
def last_page?
32+
!@next_page || @next_page == @query
33+
end
4334

44-
private
35+
private
4536

46-
def page_links(body)
47-
if body["meta"] && body["links"]
48-
[body["links"]["next"], body["links"]["prev"]]
49-
else
50-
[body["next_page"], body["previous_page"]]
37+
def page_links(body)
38+
if body["meta"] && body["links"]
39+
[body["links"]["next"], body["links"]["prev"]]
40+
else
41+
[body["next_page"], body["previous_page"]]
42+
end
5143
end
52-
end
5344

54-
def cbp_response?(body)
55-
!!(body["meta"] && body["links"])
56-
end
57-
58-
def set_cbp_options
59-
@options_per_page_was = @options.delete("per_page")
60-
# Default to CBP by using the page param as a map
61-
@options.page = { size: (@options_per_page_was || DEFAULT_PAGE_SIZE) }
62-
end
45+
def cbp_response?(body)
46+
!!(body["meta"] && body["links"])
47+
end
6348

64-
# CBP requests look like: `/resources?page[size]=100`
65-
# OBP requests look like: `/resources?page=2`
66-
def cbp_request?
67-
@options["page"].is_a?(Hash)
68-
end
49+
def set_cbp_options
50+
@options_per_page_was = @options.delete("per_page")
51+
# Default to CBP by using the page param as a map
52+
@options.page = { size: (@options_per_page_was || DEFAULT_PAGE_SIZE) }
53+
end
6954

70-
def intentional_obp_request?
71-
Helpers.present?(@options["page"]) && !cbp_request?
72-
end
55+
# CBP requests look like: `/resources?page[size]=100`
56+
# OBP requests look like: `/resources?page=2`
57+
def cbp_request?
58+
@options["page"].is_a?(Hash)
59+
end
7360

74-
def supports_cbp?
75-
@resource_class.cbp_path_regexes.any? { |supported_path_regex| path.match?(supported_path_regex) }
76-
end
61+
def intentional_obp_request?
62+
Helpers.present?(@options["page"]) && !cbp_request?
63+
end
7764

78-
def first_cbp_request?
79-
# @next_page will be nil when making the first cbp request
80-
@next_page.nil?
81-
end
65+
def supports_cbp?
66+
@resource_class.cbp_path_regexes.any? { |supported_path_regex| path.match?(supported_path_regex) }
67+
end
8268

83-
def set_page_and_count(body)
84-
@count = (body["count"] || @resources.size).to_i
85-
@next_page, @prev_page = page_links(body)
69+
def first_cbp_request?
70+
# @next_page will be nil when making the first cbp request
71+
@next_page.nil?
72+
end
8673

87-
if cbp_response?(body)
88-
set_cbp_response_options(body)
89-
elsif @next_page =~ /page=(\d+)/
90-
@options["page"] = Regexp.last_match(1).to_i - 1
91-
elsif @prev_page =~ /page=(\d+)/
92-
@options["page"] = Regexp.last_match(1).to_i + 1
74+
def set_page_and_count(body)
75+
@count = (body["count"] || @resources.size).to_i
76+
@next_page, @prev_page = page_links(body)
77+
78+
if cbp_response?(body)
79+
set_cbp_response_options(body)
80+
elsif @next_page =~ /page=(\d+)/
81+
@options["page"] = Regexp.last_match(1).to_i - 1
82+
elsif @prev_page =~ /page=(\d+)/
83+
@options["page"] = Regexp.last_match(1).to_i + 1
84+
end
9385
end
94-
end
9586

96-
def set_cbp_response_options(body)
97-
@options.page = {} unless cbp_request?
98-
# the line above means an intentional CBP request where page[size] is passed on the query
99-
# this is to cater for CBP responses where we don't specify page[size] but the endpoint
100-
# responds CBP by default. i.e `client.trigger_categories.fetch`
101-
@options.page.merge!(
102-
before: body["meta"]["before_cursor"],
103-
after: body["meta"]["after_cursor"]
104-
)
87+
def set_cbp_response_options(body)
88+
@options.page = {} unless cbp_request?
89+
# the line above means an intentional CBP request where page[size] is passed on the query
90+
# this is to cater for CBP responses where we don't specify page[size] but the endpoint
91+
# responds CBP by default. i.e `client.trigger_categories.fetch`
92+
@options.page.merge!(
93+
before: body["meta"]["before_cursor"],
94+
after: body["meta"]["after_cursor"]
95+
)
96+
end
10597
end
10698
end
10799
end

lib/zendesk_api/resources.rb

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -266,10 +266,10 @@ def path(options = {})
266266
end
267267

268268
class Topic < Resource
269-
has_many :subscriptions, :class => TopicSubscription, :inline => true
270-
has_many Tag, :extend => Tag::Update, :inline => :create
269+
has_many :subscriptions, class: TopicSubscription, inline: true
270+
has_many Tag, extend: Tag::Update, inline: :create
271271
has_many Attachment
272-
has_many :uploads, :class => Attachment, :inline => true
272+
has_many :uploads, class: Attachment, inline: true
273273
end
274274

275275
class Activity < Resource
@@ -326,7 +326,7 @@ class Request < Resource
326326
class Comment < DataResource
327327
include Save
328328

329-
has_many :uploads, :class => Attachment, :inline => true
329+
has_many :uploads, class: Attachment, inline: true
330330
has :author, :class => User
331331

332332
def save
@@ -368,7 +368,7 @@ class TicketRelated < DataResource; end
368368
class TicketEvent < DataResource
369369
class Event < Data; end
370370

371-
has_many :child_events, :class => Event
371+
has_many :child_events, class: Event
372372
has Ticket
373373
has :updater, :class => User
374374

@@ -413,8 +413,8 @@ class Event < Data
413413
class Comment < DataResource
414414
include Save
415415

416-
has_many :uploads, :class => Attachment, :inline => true
417-
has :author, :class => User
416+
has_many :uploads, class: Attachment, inline: true
417+
has :author, class: User
418418

419419
def save
420420
if new_record?
@@ -441,35 +441,35 @@ class << self
441441
has :submitter, :class => User
442442
has :assignee, :class => User
443443

444-
has_many :collaborators, :class => User, :inline => true, :extend => (Module.new do
444+
has_many :collaborators, class: User, inline: true, extend: (Module.new do
445445
def to_param
446446
map(&:id)
447447
end
448448
end)
449449

450450
has_many Audit
451-
has :metrics, :class => TicketMetric
451+
has :metrics, class: TicketMetric
452452
has Group
453453
has Organization
454454
has Brand
455-
has :related, :class => TicketRelated
455+
has :related, class: TicketRelated
456456

457-
has Comment, :inline => true
457+
has Comment, inline: true
458458
has_many Comment
459459

460-
has :last_comment, :class => Comment, :inline => true
461-
has_many :last_comments, :class => Comment, :inline => true
460+
has :last_comment, class: Comment, inline: true
461+
has_many :last_comments, class: Comment, inline: true
462462

463-
has_many Tag, :extend => Tag::Update, :inline => :create
463+
has_many Tag, extend: Tag::Update, inline: :create
464464

465-
has_many :incidents, :class => Ticket
465+
has_many :incidents, class: Ticket
466466

467467
# Gets a incremental export of tickets from the start_time until now.
468468
# @param [Client] client The {Client} object to be used
469469
# @param [Integer] start_time The start_time parameter
470470
# @return [Collection] Collection of {Ticket}
471471
def self.incremental_export(client, start_time)
472-
ZendeskAPI::Collection.new(client, self, :path => "incremental/tickets?start_time=#{start_time.to_i}")
472+
ZendeskAPI::Collection.new(client, self, path: "incremental/tickets?start_time=#{start_time.to_i}")
473473
end
474474

475475
# Imports a ticket through the imports/tickets endpoint using save!
@@ -478,7 +478,7 @@ def self.incremental_export(client, start_time)
478478
# @return [Ticket] Created object or nil
479479
def self.import!(client, attributes)
480480
new(client, attributes).tap do |ticket|
481-
ticket.save!(:path => "imports/tickets")
481+
ticket.save!(path: "imports/tickets")
482482
end
483483
end
484484

@@ -488,7 +488,7 @@ def self.import!(client, attributes)
488488
# @return [Ticket] Created object or nil
489489
def self.import(client, attributes)
490490
ticket = new(client, attributes)
491-
return unless ticket.save(:path => "imports/tickets")
491+
return unless ticket.save(path: "imports/tickets")
492492
ticket
493493
end
494494
end
@@ -522,9 +522,9 @@ class ViewRow < DataResource
522522
# @internal Optional columns
523523

524524
has Group
525-
has :assignee, :class => User
526-
has :requester, :class => User
527-
has :submitter, :class => User
525+
has :assignee, class: User
526+
has :requester, class: User
527+
has :submitter, class: User
528528
has Organization
529529

530530
def self.model_key
@@ -671,7 +671,7 @@ def self.cbp_path_regexes
671671
end
672672

673673
class Group < Resource
674-
has_many :memberships, :class => GroupMembership, :path => "memberships"
674+
has_many :memberships, class: GroupMembership, path: "memberships"
675675

676676
def self.cbp_path_regexes
677677
[/groups$/, %r{groups/assignable$}]

0 commit comments

Comments
 (0)