Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Adding support for multi-page results for Event.find_all. #318

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

Merged
merged 1 commit into from
Feb 9, 2017
Merged
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
2 changes: 1 addition & 1 deletion lib/intercom/api_operations/find_all.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def find_all(params)
finder_details[:url] = "/#{collection_name}"
finder_details[:params] = params
end
ClientCollectionProxy.new(collection_name, finder_details: finder_details, client: @client)
collection_proxy_class.new(collection_name, finder_details: finder_details, client: @client)
end

private
Expand Down
6 changes: 6 additions & 0 deletions lib/intercom/service/base_service.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
require 'intercom/client_collection_proxy'

module Intercom
module Service
class BaseService
Expand All @@ -11,6 +13,10 @@ def collection_class
raise NotImplementedError
end

def collection_proxy_class
Intercom::ClientCollectionProxy
end

def from_api(api_response)
object = collection_class.new
object.from_response(api_response)
Expand Down
12 changes: 12 additions & 0 deletions lib/intercom/service/event.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
require 'intercom/client_collection_proxy'
require 'intercom/service/base_service'
require 'intercom/api_operations/save'
require 'intercom/api_operations/bulk/submit'

module Intercom
class EventCollectionProxy < ClientCollectionProxy

def paging_info_present?(response_hash)
!!(response_hash['pages'])
end
end

module Service
class Event < BaseService
include ApiOperations::FindAll
Expand All @@ -12,6 +20,10 @@ class Event < BaseService
def collection_class
Intercom::Event
end

def collection_proxy_class
Intercom::EventCollectionProxy
end
end
end
end
14 changes: 13 additions & 1 deletion spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -568,11 +568,23 @@ def test_event_list
"type" => "event.list",
"events" => [ test_event ],
"pages" => {
"next" => "https =>//api.intercom.io/events?type=user&intercom_user_id=55a3b&before=144474756550"
"next" => "https://api.intercom.io/events?type=user&intercom_user_id=55a3b&before=144474756550"
}
}
end

def page_of_events(include_next_link=false)
{
"type" => "event.list",
"events" => [ test_event ],
"pages" =>
{
"next" => (include_next_link ? "https://api.intercom.io/events?type=user&intercom_user_id=55a3b&before=144474756550" : nil),
}
}
end


def error_on_modify_frozen
RUBY_VERSION =~ /1.8/ ? TypeError : RuntimeError
end
Expand Down
19 changes: 19 additions & 0 deletions spec/unit/intercom/event_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,25 @@
client.events.find_all(type: 'user', email: '[email protected]').first
end

it "has the correct collection proxy class" do
client.events.collection_proxy_class.must_equal Intercom::EventCollectionProxy
end

it "stops iterating if no next link" do
client.expects(:get).with("/events", type: 'user', email: '[email protected]').returns(page_of_events(false))
event_names = []
client.events.find_all(type: 'user', email: '[email protected]').each { |event| event_names << event.event_name }
event_names.must_equal %W(invited-friend)
end

it "keeps iterating if next link" do
client.expects(:get).with("/events", type: 'user', email: '[email protected]').returns(page_of_events(true))
client.expects(:get).with("https://api.intercom.io/events?type=user&intercom_user_id=55a3b&before=144474756550", {}).returns(page_of_events(false))
event_names = []
client.events.find_all(type: 'user', email: '[email protected]').each { |event| event_names << event.event_name }
event_names.must_equal %W(invited-friend invited-friend)
end

it "creates an event with metadata" do
client.expects(:post).with('/events', {'event_name' => 'Eventful 1', 'created_at' => created_time.to_i, 'email' => '[email protected]', 'metadata' => {'invitee_email' => '[email protected]', :invite_code => 'ADDAFRIEND', 'found_date' => 12909364407}}).returns(:status => 202)

Expand Down