From 31f823e402c9196e0571aa11f8d5c0578bca1293 Mon Sep 17 00:00:00 2001 From: John Keyes Date: Mon, 6 Feb 2017 22:05:09 +0000 Subject: [PATCH] Adding support for multi-page Event.find_all. --- lib/intercom/api_operations/find_all.rb | 2 +- lib/intercom/service/base_service.rb | 6 ++++++ lib/intercom/service/event.rb | 12 ++++++++++++ spec/spec_helper.rb | 14 +++++++++++++- spec/unit/intercom/event_spec.rb | 19 +++++++++++++++++++ 5 files changed, 51 insertions(+), 2 deletions(-) diff --git a/lib/intercom/api_operations/find_all.rb b/lib/intercom/api_operations/find_all.rb index 6f06f3b0..fff8423f 100644 --- a/lib/intercom/api_operations/find_all.rb +++ b/lib/intercom/api_operations/find_all.rb @@ -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 diff --git a/lib/intercom/service/base_service.rb b/lib/intercom/service/base_service.rb index ee2c6f6a..6fbc2c8c 100644 --- a/lib/intercom/service/base_service.rb +++ b/lib/intercom/service/base_service.rb @@ -1,3 +1,5 @@ +require 'intercom/client_collection_proxy' + module Intercom module Service class BaseService @@ -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) diff --git a/lib/intercom/service/event.rb b/lib/intercom/service/event.rb index a4799c11..33371014 100644 --- a/lib/intercom/service/event.rb +++ b/lib/intercom/service/event.rb @@ -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 @@ -12,6 +20,10 @@ class Event < BaseService def collection_class Intercom::Event end + + def collection_proxy_class + Intercom::EventCollectionProxy + end end end end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 1c412fee..14ed7ad6 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -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 diff --git a/spec/unit/intercom/event_spec.rb b/spec/unit/intercom/event_spec.rb index 77274800..a87ce70d 100644 --- a/spec/unit/intercom/event_spec.rb +++ b/spec/unit/intercom/event_spec.rb @@ -11,6 +11,25 @@ client.events.find_all(type: 'user', email: 'joe@example.com').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: 'joe@example.com').returns(page_of_events(false)) + event_names = [] + client.events.find_all(type: 'user', email: 'joe@example.com').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: 'joe@example.com').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: 'joe@example.com').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' => 'joe@example.com', 'metadata' => {'invitee_email' => 'pi@example.org', :invite_code => 'ADDAFRIEND', 'found_date' => 12909364407}}).returns(:status => 202)