From 75cffff18061c84d801c0632905965a21fb9dfa4 Mon Sep 17 00:00:00 2001 From: Robyn-Dale Samuda Date: Sun, 11 Jul 2021 20:03:23 -0500 Subject: [PATCH 01/11] Update dependencies --- Gemfile | 1 - Gemfile.lock | 98 +++++++++++++++++++++++----------------------- demio-ruby.gemspec | 12 +++--- 3 files changed, 55 insertions(+), 56 deletions(-) diff --git a/Gemfile b/Gemfile index 37e7e9f..d2fceab 100644 --- a/Gemfile +++ b/Gemfile @@ -2,5 +2,4 @@ source "https://rubygems.org" git_source(:github) {|repo_name| "https://github.com/#{repo_name}" } -# Specify your gem's dependencies in demio_ruby.gemspec gemspec diff --git a/Gemfile.lock b/Gemfile.lock index e8fdf38..0cd75c4 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -6,68 +6,68 @@ PATH GEM remote: https://rubygems.org/ specs: - addressable (2.5.2) - public_suffix (>= 2.0.2, < 4.0) - ast (2.4.0) - coderay (1.1.2) - crack (0.4.3) - safe_yaml (~> 1.0.0) - diff-lcs (1.3) - hashdiff (0.3.7) - jaro_winkler (1.5.1) - method_source (0.8.2) - parallel (1.12.1) - parser (2.5.1.0) - ast (~> 2.4.0) - powerpack (0.1.2) - pry (0.10.4) - coderay (~> 1.1.0) - method_source (~> 0.8.1) - slop (~> 3.4) - public_suffix (3.0.2) + addressable (2.8.0) + public_suffix (>= 2.0.2, < 5.0) + ast (2.4.2) + coderay (1.1.3) + crack (0.4.5) + rexml + diff-lcs (1.4.4) + hashdiff (1.0.1) + method_source (1.0.0) + parallel (1.20.1) + parser (3.0.2.0) + ast (~> 2.4.1) + pry (0.14.1) + coderay (~> 1.1) + method_source (~> 1.0) + public_suffix (4.0.6) rainbow (3.0.0) - rake (10.5.0) - rspec (3.7.0) - rspec-core (~> 3.7.0) - rspec-expectations (~> 3.7.0) - rspec-mocks (~> 3.7.0) - rspec-core (3.7.0) - rspec-support (~> 3.7.0) - rspec-expectations (3.7.0) + rake (13.0.6) + regexp_parser (2.1.1) + rexml (3.2.5) + rspec (3.10.0) + rspec-core (~> 3.10.0) + rspec-expectations (~> 3.10.0) + rspec-mocks (~> 3.10.0) + rspec-core (3.10.1) + rspec-support (~> 3.10.0) + rspec-expectations (3.10.1) diff-lcs (>= 1.2.0, < 2.0) - rspec-support (~> 3.7.0) - rspec-mocks (3.7.0) + rspec-support (~> 3.10.0) + rspec-mocks (3.10.2) diff-lcs (>= 1.2.0, < 2.0) - rspec-support (~> 3.7.0) - rspec-support (3.7.0) - rubocop (0.57.2) - jaro_winkler (~> 1.5.1) + rspec-support (~> 3.10.0) + rspec-support (3.10.2) + rubocop (1.18.3) parallel (~> 1.10) - parser (>= 2.5) - powerpack (~> 0.1) + parser (>= 3.0.0.0) rainbow (>= 2.2.2, < 4.0) + regexp_parser (>= 1.8, < 3.0) + rexml + rubocop-ast (>= 1.7.0, < 2.0) ruby-progressbar (~> 1.7) - unicode-display_width (~> 1.0, >= 1.0.1) - ruby-progressbar (1.9.0) - safe_yaml (1.0.4) - slop (3.6.0) - unicode-display_width (1.4.0) - webmock (3.4.2) + unicode-display_width (>= 1.4.0, < 3.0) + rubocop-ast (1.7.0) + parser (>= 3.0.1.1) + ruby-progressbar (1.11.0) + unicode-display_width (2.0.0) + webmock (3.13.0) addressable (>= 2.3.6) crack (>= 0.3.2) - hashdiff + hashdiff (>= 0.4.0, < 2.0.0) PLATFORMS ruby DEPENDENCIES - bundler (~> 1.16) + bundler (~> 2.2.23) demio-ruby! - pry (~> 0.10.3) - rake (~> 10.0) - rspec (~> 3.0) - rubocop (~> 0.57.1) - webmock (~> 3.4) + pry (~> 0.14.1) + rake (~> 13.0.6) + rspec (~> 3.10.0) + rubocop (~> 1.18.3) + webmock (~> 3.13.0) BUNDLED WITH - 1.16.1 + 2.2.23 diff --git a/demio-ruby.gemspec b/demio-ruby.gemspec index d0c13f8..870c3cb 100644 --- a/demio-ruby.gemspec +++ b/demio-ruby.gemspec @@ -24,10 +24,10 @@ Gem::Specification.new do |spec| spec.required_ruby_version = '>= 2.1' - spec.add_development_dependency "bundler", "~> 1.16" - spec.add_development_dependency "rake", "~> 10.0" - spec.add_development_dependency "rspec", "~> 3.0" - spec.add_development_dependency "rubocop", "~> 0.57.1" - spec.add_development_dependency "pry", "~> 0.10.3" - spec.add_development_dependency "webmock", "~> 3.4" + spec.add_development_dependency "bundler", "~> 2.2.23" + spec.add_development_dependency "rake", "~> 13.0.6" + spec.add_development_dependency "rspec", "~> 3.10.0" + spec.add_development_dependency "rubocop", "~> 1.18.3" + spec.add_development_dependency "pry", "~> 0.14.1" + spec.add_development_dependency "webmock", "~> 3.13.0" end From 9bc8dba850aabdd2b0509c6152dff1c2c567d0c1 Mon Sep 17 00:00:00 2001 From: Robyn-Dale Samuda Date: Sun, 11 Jul 2021 20:06:47 -0500 Subject: [PATCH 02/11] Bump to version 0.2.0 and rubocop fixes --- Gemfile.lock | 2 +- bin/console | 1 + lib/demio/client.rb | 4 ++-- lib/demio/version.rb | 4 +++- spec/client_spec.rb | 2 ++ spec/demio_spec.rb | 2 ++ spec/event_date_spec.rb | 2 ++ spec/event_spec.rb | 2 ++ spec/events_spec.rb | 2 ++ spec/register_spec.rb | 2 ++ spec/spec_helper.rb | 2 ++ 11 files changed, 21 insertions(+), 4 deletions(-) diff --git a/Gemfile.lock b/Gemfile.lock index 0cd75c4..a0f95df 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,7 +1,7 @@ PATH remote: . specs: - demio-ruby (0.1.0) + demio-ruby (0.2.0) GEM remote: https://rubygems.org/ diff --git a/bin/console b/bin/console index e93130b..69ad680 100755 --- a/bin/console +++ b/bin/console @@ -1,4 +1,5 @@ #!/usr/bin/env ruby +# frozen_string_literal: true require "bundler/setup" require "demio" diff --git a/lib/demio/client.rb b/lib/demio/client.rb index a763552..dea62f5 100644 --- a/lib/demio/client.rb +++ b/lib/demio/client.rb @@ -54,9 +54,9 @@ def make_request(verb_klass, uri, payload = {}, limit = 10) response = http.request request case response - when Net::HTTPSuccess then + when Net::HTTPSuccess response - when Net::HTTPRedirection then + when Net::HTTPRedirection location = response["location"] make_request(verb_klass, location, payload, limit - 1) else diff --git a/lib/demio/version.rb b/lib/demio/version.rb index 1174147..b2cfd16 100644 --- a/lib/demio/version.rb +++ b/lib/demio/version.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + module Demio - VERSION = "0.1.0".freeze + VERSION = "0.2.0" end diff --git a/spec/client_spec.rb b/spec/client_spec.rb index ac6b866..f5c87df 100644 --- a/spec/client_spec.rb +++ b/spec/client_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + RSpec.describe Demio::Client do before(:each) do options = { diff --git a/spec/demio_spec.rb b/spec/demio_spec.rb index 84b3f47..0c99231 100644 --- a/spec/demio_spec.rb +++ b/spec/demio_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + RSpec.describe Demio do it "has a version number" do expect(Demio::VERSION).not_to be nil diff --git a/spec/event_date_spec.rb b/spec/event_date_spec.rb index 55d7710..785eb66 100644 --- a/spec/event_date_spec.rb +++ b/spec/event_date_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + RSpec.describe Demio::Client::EventDate do before(:each) do options = { diff --git a/spec/event_spec.rb b/spec/event_spec.rb index 95a89db..5c03b27 100644 --- a/spec/event_spec.rb +++ b/spec/event_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + RSpec.describe Demio::Client::Event do before(:each) do options = { diff --git a/spec/events_spec.rb b/spec/events_spec.rb index 1975350..375e20d 100644 --- a/spec/events_spec.rb +++ b/spec/events_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + RSpec.describe Demio::Client::Events do before(:each) do options = { diff --git a/spec/register_spec.rb b/spec/register_spec.rb index e2c2497..a4cd171 100644 --- a/spec/register_spec.rb +++ b/spec/register_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + RSpec.describe Demio::Client::Register do before(:each) do options = { diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index b8a87a1..3529627 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require "bundler/setup" require "demio" require "webmock/rspec" From 141a7bc46f98de816310f402e9a577d231f4f19b Mon Sep 17 00:00:00 2001 From: Robyn-Dale Samuda Date: Sun, 11 Jul 2021 20:40:49 -0500 Subject: [PATCH 03/11] Delete .travis.yml --- .travis.yml | 9 --------- 1 file changed, 9 deletions(-) delete mode 100644 .travis.yml diff --git a/.travis.yml b/.travis.yml deleted file mode 100644 index ef48502..0000000 --- a/.travis.yml +++ /dev/null @@ -1,9 +0,0 @@ -sudo: false -language: ruby -rvm: - - 2.5.0 - - 2.4.0 - - 2.3.0 - - 2.2.0 -before_install: - - gem install bundler -v 1.16.1 From 21cd95e1904f4525f84a18e6f0e4a8b711196900 Mon Sep 17 00:00:00 2001 From: Robyn-Dale Samuda Date: Sun, 11 Jul 2021 20:40:57 -0500 Subject: [PATCH 04/11] Use Ruby 2.7.4 --- .ruby-version | 1 + 1 file changed, 1 insertion(+) create mode 100644 .ruby-version diff --git a/.ruby-version b/.ruby-version new file mode 100644 index 0000000..a4dd9db --- /dev/null +++ b/.ruby-version @@ -0,0 +1 @@ +2.7.4 From 53ccf1a2215f3e1a23b37aaa0f68b69911d04453 Mon Sep 17 00:00:00 2001 From: Robyn-Dale Samuda Date: Sun, 11 Jul 2021 21:27:28 -0500 Subject: [PATCH 05/11] Reduce ABC size for #make_request --- lib/demio/client.rb | 41 +++++++++++++++++++++++------------------ spec/client_spec.rb | 35 ++++++++++++++++------------------- 2 files changed, 39 insertions(+), 37 deletions(-) diff --git a/lib/demio/client.rb b/lib/demio/client.rb index dea62f5..5b910a0 100644 --- a/lib/demio/client.rb +++ b/lib/demio/client.rb @@ -40,28 +40,33 @@ def ping private - def make_request(verb_klass, uri, payload = {}, limit = 10) - raise TooManyRedirectsError, "too many HTTP redirects" if limit.zero? + def make_request(verb_klass, uri, payload = {}, request_limit = 10) + raise TooManyRedirectsError, "too many HTTP redirects" if request_limit.zero? uri = format_uri(uri) Net::HTTP.start(uri.host, uri.port, use_ssl: true) do |http| - request = verb_klass.new uri - request = create_headers(request) - - request.body = payload.to_json if verb_klass == Net::HTTP::Put - - response = http.request request - - case response - when Net::HTTPSuccess - response - when Net::HTTPRedirection - location = response["location"] - make_request(verb_klass, location, payload, limit - 1) - else - response.value - end + response = http.request(create_request(verb_klass, uri, payload)) + handle_response(response, request_limit) + end + end + + def create_request(verb_klass, uri, payload) + request = verb_klass.new(uri) + request = create_headers(request) + request.body = payload.to_json if verb_klass == Net::HTTP::Put + request + end + + def handle_response(response, request_limit) + case response + when Net::HTTPSuccess + response + when Net::HTTPRedirection + location = response["location"] + make_request(verb_klass, location, payload, request_limit - 1) + else + response.value end end diff --git a/spec/client_spec.rb b/spec/client_spec.rb index f5c87df..53ae7bf 100644 --- a/spec/client_spec.rb +++ b/spec/client_spec.rb @@ -1,13 +1,13 @@ # frozen_string_literal: true RSpec.describe Demio::Client do - before(:each) do - options = { + let(:options) do + { api_secret: "12345", api_key: "xyz0987" } - @client = Demio::Client.new(options) end + let(:client) { described_class.new(options) } context "request headers" do it "correctly sets headers" do @@ -16,37 +16,34 @@ "https://my.demio.com/api/v1/ping/query?api_key=xyz0987&api_secret=12345" ) - @client.ping + client.ping expect( a_request( :get, "https://my.demio.com/api/v1/ping/query?api_key=xyz0987&api_secret=12345" - ) - .with(headers: { - "Api-Key" => "xyz0987", - "Api-Secret" => "12345", - "Content-Type" => "application/json", - "User-Agent" => "Demio Ruby Client - #{Demio::VERSION}" - }) + ).with( + headers: { + "Api-Key" => "xyz0987", + "Api-Secret" => "12345", + "Content-Type" => "application/json", + "User-Agent" => "Demio Ruby Client - #{Demio::VERSION}" + }) ).to have_been_made.times(1) end end context "#ping" do it "makes a GET ping request" do - ping_response = { - "pong" => true - } + ping_response = { "pong" => true } - stub_request(:get, "https://my.demio.com/api/v1/ping/query?api_key=xyz0987&api_secret=12345") - .to_return(body: ping_response.to_json, status: 200) + stub_request(:get, "https://my.demio.com/api/v1/ping/query?api_key=xyz0987&api_secret=12345"). + to_return(body: ping_response.to_json, status: 200) - response = @client.ping + response = client.ping expect(response.code).to eq("200") expect( a_request( - :get, - "https://my.demio.com/api/v1/ping/query?api_key=xyz0987&api_secret=12345" + :get, "https://my.demio.com/api/v1/ping/query?api_key=xyz0987&api_secret=12345" ) ).to have_been_made.times(1) end From 68b3bd4f38cee4e152d6becd7c62906f04bd5144 Mon Sep 17 00:00:00 2001 From: Robyn-Dale Samuda Date: Mon, 12 Jul 2021 10:05:14 -0500 Subject: [PATCH 06/11] Fix redirection bug and harden tests --- lib/demio/client.rb | 16 +++++++---- spec/client_spec.rb | 67 +++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 75 insertions(+), 8 deletions(-) diff --git a/lib/demio/client.rb b/lib/demio/client.rb index 5b910a0..4e0f3ed 100644 --- a/lib/demio/client.rb +++ b/lib/demio/client.rb @@ -18,6 +18,8 @@ class Client attr_reader :api_key, :api_secret + REQUEST_REDIRECT_FOLLOW_LIMIT = 10 + def initialize(options = {}) @api_key = options[:api_key] @api_secret = options[:api_secret] @@ -40,14 +42,14 @@ def ping private - def make_request(verb_klass, uri, payload = {}, request_limit = 10) + def make_request(verb_klass, uri, payload = {}, request_limit = REQUEST_REDIRECT_FOLLOW_LIMIT) raise TooManyRedirectsError, "too many HTTP redirects" if request_limit.zero? - uri = format_uri(uri) + uri = redirected_request?(request_limit) ? URI(uri) : format_uri(uri) Net::HTTP.start(uri.host, uri.port, use_ssl: true) do |http| response = http.request(create_request(verb_klass, uri, payload)) - handle_response(response, request_limit) + handle_response(response, verb_klass, payload, request_limit) end end @@ -58,12 +60,12 @@ def create_request(verb_klass, uri, payload) request end - def handle_response(response, request_limit) + def handle_response(response, verb_klass, payload, request_limit) case response when Net::HTTPSuccess response when Net::HTTPRedirection - location = response["location"] + location = response["Location"] make_request(verb_klass, location, payload, request_limit - 1) else response.value @@ -78,6 +80,10 @@ def create_headers(request) request end + def redirected_request?(request_limit) + request_limit < REQUEST_REDIRECT_FOLLOW_LIMIT + end + def format_uri(path) URI(base_uri + path) end diff --git a/spec/client_spec.rb b/spec/client_spec.rb index 53ae7bf..7397f1b 100644 --- a/spec/client_spec.rb +++ b/spec/client_spec.rb @@ -7,6 +7,7 @@ api_key: "xyz0987" } end + let(:stubbed_response_body) { [{ id: 1, value: "test" }].to_json } let(:client) { described_class.new(options) } context "request headers" do @@ -27,7 +28,8 @@ "Api-Secret" => "12345", "Content-Type" => "application/json", "User-Agent" => "Demio Ruby Client - #{Demio::VERSION}" - }) + } + ) ).to have_been_made.times(1) end end @@ -36,8 +38,8 @@ it "makes a GET ping request" do ping_response = { "pong" => true } - stub_request(:get, "https://my.demio.com/api/v1/ping/query?api_key=xyz0987&api_secret=12345"). - to_return(body: ping_response.to_json, status: 200) + stub_request(:get, "https://my.demio.com/api/v1/ping/query?api_key=xyz0987&api_secret=12345") + .to_return(body: ping_response.to_json, status: 200) response = client.ping expect(response.code).to eq("200") @@ -48,4 +50,63 @@ ).to have_been_made.times(1) end end + + context "#get" do + it "makes a GET request" do + target_path = "endpoint?query=1" + stub_request(:get, "https://my.demio.com/api/v1/#{target_path}") + .to_return(body: stubbed_response_body, status: 200) + + response = client.get(target_path) + expect(response.code).to eq("200") + expect(response.body).to eq(stubbed_response_body) + expect( + a_request( + :get, "https://my.demio.com/api/v1/#{target_path}" + ) + ).to have_been_made.times(1) + end + end + + context "#put" do + it "makes a PUT request" do + target_path = "endpoint" + payload = { event_id: 1234 } + + stub_request(:put, "https://my.demio.com/api/v1/#{target_path}") + .with(body: payload.to_json) + .to_return(body: stubbed_response_body, status: 200) + + response = client.put(target_path, payload) + expect(response.code).to eq("200") + expect(response.body).to eq(stubbed_response_body) + expect(a_request(:put, "https://my.demio.com/api/v1/#{target_path}") + .with(body: payload.to_json)) + .to have_been_made.times(1) + end + end + + context "response is a redirect" do + it "follows the redirect" do + target_path = "endpoint?query=1" + follow_url = "https://my.demio.com/api/v1/redirect" + stub_request(:get, "https://my.demio.com/api/v1/#{target_path}") + .to_return(headers: { "Location" => follow_url }, body: stubbed_response_body, status: 301) + stub_request(:get, follow_url) + .to_return(body: stubbed_response_body, status: 200) + + response = client.get(target_path) + expect(response.code).to eq("200") + end + + it "raises an error if number of redirects exceed the limit" do + target_path = "endpoint?query=1" + stub_const("Demio::Client::REQUEST_REDIRECT_FOLLOW_LIMIT", 1) + stub_request(:get, "https://my.demio.com/api/v1/#{target_path}") + .to_return(body: stubbed_response_body, status: 301) + + expect { client.get(target_path) } + .to raise_error(Demio::TooManyRedirectsError, "too many HTTP redirects") + end + end end From e045891ac5b96b7287baf053280f295741f0dbe8 Mon Sep 17 00:00:00 2001 From: Robyn-Dale Samuda Date: Mon, 12 Jul 2021 10:14:20 -0500 Subject: [PATCH 07/11] Update old test syntax --- spec/event_date_spec.rb | 15 ++++++++------- spec/event_spec.rb | 15 ++++++++------- spec/events_spec.rb | 15 ++++++++------- spec/register_spec.rb | 15 ++++++++------- 4 files changed, 32 insertions(+), 28 deletions(-) diff --git a/spec/event_date_spec.rb b/spec/event_date_spec.rb index 785eb66..14d13a9 100644 --- a/spec/event_date_spec.rb +++ b/spec/event_date_spec.rb @@ -1,30 +1,31 @@ # frozen_string_literal: true RSpec.describe Demio::Client::EventDate do - before(:each) do - options = { + let(:options) do + { api_secret: "12345", api_key: "xyz0987" } - @client = Demio::Client.new(options) end + let(:client) { Demio::Client.new(options) } it "makes a GET request to Demio's event date endpoint" do event_id = 4567 date_id = 35 - event_response = { + stubbed_event_response_body = { "date_id" => 35, "status" => "scheduled", "timestamp" => 1_456_723_800, "datetime" => "March 26th, 2016 at 8:00PM EET", "zone" => "Europe/Kiev" - } + }.to_json stub_request(:get, "https://my.demio.com/api/v1/event/#{event_id}/#{date_id}") - .to_return(body: event_response.to_json, status: 200) + .to_return(body: stubbed_event_response_body, status: 200) - response = @client.event_date(event_id, date_id) + response = client.event_date(event_id, date_id) expect(response.code).to eq("200") + expect(response.body).to eq(stubbed_event_response_body) expect(a_request(:get, "https://my.demio.com/api/v1/event/#{event_id}/#{date_id}")) .to have_been_made.times(1) end diff --git a/spec/event_spec.rb b/spec/event_spec.rb index 5c03b27..eb13ef1 100644 --- a/spec/event_spec.rb +++ b/spec/event_spec.rb @@ -1,28 +1,29 @@ # frozen_string_literal: true RSpec.describe Demio::Client::Event do - before(:each) do - options = { + let(:options) do + { api_secret: "12345", api_key: "xyz0987" } - @client = Demio::Client.new(options) end + let(:client) { Demio::Client.new(options) } it "makes a GET request to Demio's event fetch endpoint" do event_id = 4567 - event = { + stubbed_response_body = { "id" => event_id, "date_id" => 35, "name" => "John Doe", "email" => "john.doe@gmail.com" - } + }.to_json stub_request(:get, "https://my.demio.com/api/v1/event/#{event_id}") - .to_return(body: event.to_json, status: 200) + .to_return(body: stubbed_response_body, status: 200) - response = @client.event(event_id) + response = client.event(event_id) expect(response.code).to eq("200") + expect(response.body).to eq(stubbed_response_body) expect(a_request(:get, "https://my.demio.com/api/v1/event/#{event_id}")) .to have_been_made.times(1) end diff --git a/spec/events_spec.rb b/spec/events_spec.rb index 375e20d..9647b12 100644 --- a/spec/events_spec.rb +++ b/spec/events_spec.rb @@ -1,16 +1,16 @@ # frozen_string_literal: true RSpec.describe Demio::Client::Events do - before(:each) do - options = { + let(:options) do + { api_secret: "12345", api_key: "xyz0987" } - @client = Demio::Client.new(options) end + let(:client) { Demio::Client.new(options) } it "makes a GET request to Demio's event listing endpoint" do - response = [ + stubbed_response_body = [ { "id" => 62, "name" => "First Webinar", @@ -29,12 +29,13 @@ "zone" => "America/New_York", "registration_url" => "http://my.demio.loc/ref/pgRzWQhYLPmuboOA" } - ] + ].to_json stub_request(:get, "https://my.demio.com/api/v1/events") - .to_return(body: response.to_json, status: 200) + .to_return(body: stubbed_response_body, status: 200) - response = @client.events + response = client.events expect(response.code).to eq("200") + expect(response.body).to eq(stubbed_response_body) expect(a_request(:get, "https://my.demio.com/api/v1/events")) .to have_been_made.times(1) end diff --git a/spec/register_spec.rb b/spec/register_spec.rb index a4cd171..de30659 100644 --- a/spec/register_spec.rb +++ b/spec/register_spec.rb @@ -1,13 +1,13 @@ # frozen_string_literal: true RSpec.describe Demio::Client::Register do - before(:each) do - options = { + let(:options) do + { api_secret: "12345", api_key: "xyz0987" } - @client = Demio::Client.new(options) end + let(:client) { Demio::Client.new(options) } it "makes a PUT request to Demio's event registration endpoint" do payload = { @@ -16,16 +16,17 @@ "name" => "Jane Doe", "email" => "jane.doe@gmail.com" } - registration_response = { + stubbed_registration_response_body = { "join_link" => "https://event.demio.com/join/fPaSYijVHXI6ZJgE" - } + }.to_json stub_request(:put, "https://my.demio.com/api/v1/event/register") .with(body: payload.to_json) - .to_return(body: registration_response.to_json, status: 200) + .to_return(body: stubbed_registration_response_body, status: 200) - response = @client.register(payload) + response = client.register(payload) expect(response.code).to eq("200") + expect(response.body).to eq(stubbed_registration_response_body) expect(a_request(:put, "https://my.demio.com/api/v1/event/register") .with(body: payload.to_json)) .to have_been_made.times(1) From 8a349062be5b06864ca7d33594fa207def499477 Mon Sep 17 00:00:00 2001 From: Robyn-Dale Samuda Date: Mon, 12 Jul 2021 10:20:55 -0500 Subject: [PATCH 08/11] Fix event_date path bug --- lib/demio/event_date.rb | 8 ++++---- lib/demio/register.rb | 1 + spec/register_spec.rb | 4 ++-- 3 files changed, 7 insertions(+), 6 deletions(-) diff --git a/lib/demio/event_date.rb b/lib/demio/event_date.rb index 7d8c353..90dadf9 100644 --- a/lib/demio/event_date.rb +++ b/lib/demio/event_date.rb @@ -5,11 +5,11 @@ class Client module EventDate # Fetches info about a specific date from Demio # - # @param id [String] - # @param date_id [String] + # @param event_id [String] + # @param event_date_id [String] # @return [Net::HTTPCreated] - def event_date(id, date_id) - get "event/#{id}/#{date_id}" + def event_date(event_id, event_date_id) + get "event/#{event_id}/date/#{event_date_id}" end end end diff --git a/lib/demio/register.rb b/lib/demio/register.rb index e93b790..51a7b58 100644 --- a/lib/demio/register.rb +++ b/lib/demio/register.rb @@ -4,6 +4,7 @@ module Demio class Client module Register # Adds a registrant to an event in Demio + # Docs: https://publicdemioapi.docs.apiary.io/#reference/events/register/register # # @param payload [Hash] # @return [Net::HTTPCreated] diff --git a/spec/register_spec.rb b/spec/register_spec.rb index de30659..3c92813 100644 --- a/spec/register_spec.rb +++ b/spec/register_spec.rb @@ -22,10 +22,10 @@ stub_request(:put, "https://my.demio.com/api/v1/event/register") .with(body: payload.to_json) - .to_return(body: stubbed_registration_response_body, status: 200) + .to_return(body: stubbed_registration_response_body, status: 201) response = client.register(payload) - expect(response.code).to eq("200") + expect(response.code).to eq("201") expect(response.body).to eq(stubbed_registration_response_body) expect(a_request(:put, "https://my.demio.com/api/v1/event/register") .with(body: payload.to_json)) From 2f14c1f9d00521790bbe925d0a5b1ff1343d48d6 Mon Sep 17 00:00:00 2001 From: Robyn-Dale Samuda Date: Mon, 12 Jul 2021 10:41:44 -0500 Subject: [PATCH 09/11] Add support for participants endpoint --- README.md | 6 ++++-- lib/demio/client.rb | 2 ++ lib/demio/participants.rb | 15 ++++++++++++++ spec/participants_spec.rb | 41 +++++++++++++++++++++++++++++++++++++++ 4 files changed, 62 insertions(+), 2 deletions(-) create mode 100644 lib/demio/participants.rb create mode 100644 spec/participants_spec.rb diff --git a/README.md b/README.md index 8a519de..7fc3d0c 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,7 @@ # Demio Ruby Client -[![Build Status](https://travis-ci.org/samudary/demio-ruby.svg?branch=master)](https://travis-ci.org/samudary/demio-ruby) +![example workflow](https://github.com/samudary/demio-ruby/actions/workflows/ruby.yml/badge.svg) + A Ruby gem for interacting with the [Demio API](https://publicdemioapi.docs.apiary.io/). @@ -37,7 +38,8 @@ client = Demio::Client.new( | :------------------------- | :--------------------------------------------------- | | List events | `#events` | | Fetch an event | `#event(event_id)` | -| Fetch event date info | `#event_date(event_id, date_id)` | +| Fetch event date info | `#event_date(event_id, event_date_id)` | +| Fetch participants list | `#participants(event_date_id)` | | Register a registrant | `#register(payload = {})` | | Ping | `#ping` | diff --git a/lib/demio/client.rb b/lib/demio/client.rb index 4e0f3ed..ae7da4e 100644 --- a/lib/demio/client.rb +++ b/lib/demio/client.rb @@ -4,6 +4,7 @@ require "demio/event" require "demio/event_date" require "demio/events" +require "demio/participants" require "demio/register" require "json" require "net/http" @@ -14,6 +15,7 @@ class Client include Event include EventDate include Events + include Participants include Register attr_reader :api_key, :api_secret diff --git a/lib/demio/participants.rb b/lib/demio/participants.rb new file mode 100644 index 0000000..61b1cf9 --- /dev/null +++ b/lib/demio/participants.rb @@ -0,0 +1,15 @@ +# frozen_string_literal: true + +module Demio + class Client + module Participants + # Returns Event Date participants list + # + # @param event_date_id [String] + # @return [Net::HTTPOk] + def participants(event_date_id) + get "report/#{event_date_id}/participants" + end + end + end +end diff --git a/spec/participants_spec.rb b/spec/participants_spec.rb new file mode 100644 index 0000000..8a464df --- /dev/null +++ b/spec/participants_spec.rb @@ -0,0 +1,41 @@ +# frozen_string_literal: true + +RSpec.describe Demio::Client::Participants do + let(:options) do + { + api_secret: "12345", + api_key: "xyz0987" + } + end + let(:client) { Demio::Client.new(options) } + + it "makes a GET request to Demio's participants endpoint" do + stubbed_response_body = { + "participants" => [ + { + "email" => "simulation+dave@demio.com", + "name" => "Dave", + "custom_fields" => [], + "attended" => false, + "status" => "did not attend" + }, + { + "email" => "simulation+ansley@demio.com", + "name" => "Ansley", + "custom_fields" => [], + "attended" => false, + "status" => "did not attend" + } + ] + }.to_json + event_date_id = 1234 + stub_request(:get, "https://my.demio.com/api/v1/report/#{event_date_id}/participants") + .to_return(body: stubbed_response_body, status: 200) + + response = client.participants(event_date_id) + expect(response.code).to eq("200") + expect(response.body).to eq(stubbed_response_body) + expect(a_request(:get, "https://my.demio.com/api/v1/report/#{event_date_id}/participants")) + .to have_been_made.times(1) + end +end From 8b30bd5a0a6c1be5377d6649d612f67494fec16b Mon Sep 17 00:00:00 2001 From: Robyn-Dale Samuda Date: Mon, 12 Jul 2021 10:42:05 -0500 Subject: [PATCH 10/11] More documentation and test fixes --- lib/demio/event.rb | 2 +- lib/demio/event_date.rb | 2 +- lib/demio/events.rb | 2 +- spec/client_spec.rb | 4 ++++ spec/event_date_spec.rb | 4 ++-- 5 files changed, 9 insertions(+), 5 deletions(-) diff --git a/lib/demio/event.rb b/lib/demio/event.rb index 35e4e0b..70bcafd 100644 --- a/lib/demio/event.rb +++ b/lib/demio/event.rb @@ -6,7 +6,7 @@ module Event # Fetches a single event from Demio # # @param id [String] - # @return [Net::HTTPCreated] + # @return [Net::HTTPOk] def event(id) get "event/#{id}" end diff --git a/lib/demio/event_date.rb b/lib/demio/event_date.rb index 90dadf9..6f39b54 100644 --- a/lib/demio/event_date.rb +++ b/lib/demio/event_date.rb @@ -7,7 +7,7 @@ module EventDate # # @param event_id [String] # @param event_date_id [String] - # @return [Net::HTTPCreated] + # @return [Net::HTTPOk] def event_date(event_id, event_date_id) get "event/#{event_id}/date/#{event_date_id}" end diff --git a/lib/demio/events.rb b/lib/demio/events.rb index 337ec03..3d2470d 100644 --- a/lib/demio/events.rb +++ b/lib/demio/events.rb @@ -5,7 +5,7 @@ class Client module Events # Fetches all events from Demio # - # @return [Net::HTTPCreated] + # @return [Net::HTTPOk] def events get "events" end diff --git a/spec/client_spec.rb b/spec/client_spec.rb index 7397f1b..204d8a0 100644 --- a/spec/client_spec.rb +++ b/spec/client_spec.rb @@ -97,6 +97,10 @@ response = client.get(target_path) expect(response.code).to eq("200") + expect(a_request(:get, "https://my.demio.com/api/v1/#{target_path}")) + .to have_been_made.times(1) + expect(a_request(:get, follow_url)) + .to have_been_made.times(1) end it "raises an error if number of redirects exceed the limit" do diff --git a/spec/event_date_spec.rb b/spec/event_date_spec.rb index 14d13a9..ebe25ec 100644 --- a/spec/event_date_spec.rb +++ b/spec/event_date_spec.rb @@ -20,13 +20,13 @@ "zone" => "Europe/Kiev" }.to_json - stub_request(:get, "https://my.demio.com/api/v1/event/#{event_id}/#{date_id}") + stub_request(:get, "https://my.demio.com/api/v1/event/#{event_id}/date/#{date_id}") .to_return(body: stubbed_event_response_body, status: 200) response = client.event_date(event_id, date_id) expect(response.code).to eq("200") expect(response.body).to eq(stubbed_event_response_body) - expect(a_request(:get, "https://my.demio.com/api/v1/event/#{event_id}/#{date_id}")) + expect(a_request(:get, "https://my.demio.com/api/v1/event/#{event_id}/date/#{date_id}")) .to have_been_made.times(1) end end From c5e464caa04cce4dea62519ce4f36217a6410dd8 Mon Sep 17 00:00:00 2001 From: Robyn-Dale Samuda Date: Mon, 12 Jul 2021 11:11:39 -0500 Subject: [PATCH 11/11] Add support for filter params --- lib/demio/event.rb | 6 ++++-- lib/demio/events.rb | 6 ++++-- lib/demio/participants.rb | 6 ++++-- spec/event_spec.rb | 22 ++++++++++++++++++---- spec/events_spec.rb | 21 ++++++++++++++++++--- spec/participants_spec.rb | 23 +++++++++++++++++++---- 6 files changed, 67 insertions(+), 17 deletions(-) diff --git a/lib/demio/event.rb b/lib/demio/event.rb index 70bcafd..8e24859 100644 --- a/lib/demio/event.rb +++ b/lib/demio/event.rb @@ -6,9 +6,11 @@ module Event # Fetches a single event from Demio # # @param id [String] + # @param active [Boolean] # @return [Net::HTTPOk] - def event(id) - get "event/#{id}" + def event(id, active = nil) + active_param = active ? "?active=#{active}" : nil + get "event/#{id}#{active_param}" end end end diff --git a/lib/demio/events.rb b/lib/demio/events.rb index 3d2470d..5922a98 100644 --- a/lib/demio/events.rb +++ b/lib/demio/events.rb @@ -5,9 +5,11 @@ class Client module Events # Fetches all events from Demio # + # @param type [String] # @return [Net::HTTPOk] - def events - get "events" + def events(type = nil) + type_param = type ? "?type=#{type}" : nil + get "events#{type_param}" end end end diff --git a/lib/demio/participants.rb b/lib/demio/participants.rb index 61b1cf9..cff390a 100644 --- a/lib/demio/participants.rb +++ b/lib/demio/participants.rb @@ -6,9 +6,11 @@ module Participants # Returns Event Date participants list # # @param event_date_id [String] + # @params status [String] # @return [Net::HTTPOk] - def participants(event_date_id) - get "report/#{event_date_id}/participants" + def participants(event_date_id, status = nil) + status_param = status ? "?status=#{status}" : nil + get "report/#{event_date_id}/participants#{status_param}" end end end diff --git a/spec/event_spec.rb b/spec/event_spec.rb index eb13ef1..c4afd8f 100644 --- a/spec/event_spec.rb +++ b/spec/event_spec.rb @@ -8,16 +8,17 @@ } end let(:client) { Demio::Client.new(options) } - - it "makes a GET request to Demio's event fetch endpoint" do - event_id = 4567 - stubbed_response_body = { + let(:event_id) { 4567 } + let(:stubbed_response_body) do + { "id" => event_id, "date_id" => 35, "name" => "John Doe", "email" => "john.doe@gmail.com" }.to_json + end + it "makes a GET request to Demio's event endpoint" do stub_request(:get, "https://my.demio.com/api/v1/event/#{event_id}") .to_return(body: stubbed_response_body, status: 200) @@ -27,4 +28,17 @@ expect(a_request(:get, "https://my.demio.com/api/v1/event/#{event_id}")) .to have_been_made.times(1) end + + context "with additional param" do + it "makes a GET request to Demio's event endpoint with param" do + stub_request(:get, "https://my.demio.com/api/v1/event/#{event_id}?active=true") + .to_return(body: stubbed_response_body, status: 200) + + response = client.event(event_id, true) + expect(response.code).to eq("200") + expect(response.body).to eq(stubbed_response_body) + expect(a_request(:get, "https://my.demio.com/api/v1/event/#{event_id}?active=true")) + .to have_been_made.times(1) + end + end end diff --git a/spec/events_spec.rb b/spec/events_spec.rb index 9647b12..ee4f97f 100644 --- a/spec/events_spec.rb +++ b/spec/events_spec.rb @@ -8,9 +8,8 @@ } end let(:client) { Demio::Client.new(options) } - - it "makes a GET request to Demio's event listing endpoint" do - stubbed_response_body = [ + let(:stubbed_response_body) do + [ { "id" => 62, "name" => "First Webinar", @@ -30,6 +29,9 @@ "registration_url" => "http://my.demio.loc/ref/pgRzWQhYLPmuboOA" } ].to_json + end + + it "makes a GET request to Demio's event listing endpoint" do stub_request(:get, "https://my.demio.com/api/v1/events") .to_return(body: stubbed_response_body, status: 200) @@ -39,4 +41,17 @@ expect(a_request(:get, "https://my.demio.com/api/v1/events")) .to have_been_made.times(1) end + + context "with additional param" do + it "makes a GET request to Demio's event listing endpoint with param" do + stub_request(:get, "https://my.demio.com/api/v1/events?type=upcoming") + .to_return(body: stubbed_response_body, status: 200) + + response = client.events("upcoming") + expect(response.code).to eq("200") + expect(response.body).to eq(stubbed_response_body) + expect(a_request(:get, "https://my.demio.com/api/v1/events?type=upcoming")) + .to have_been_made.times(1) + end + end end diff --git a/spec/participants_spec.rb b/spec/participants_spec.rb index 8a464df..4caa63d 100644 --- a/spec/participants_spec.rb +++ b/spec/participants_spec.rb @@ -8,9 +8,8 @@ } end let(:client) { Demio::Client.new(options) } - - it "makes a GET request to Demio's participants endpoint" do - stubbed_response_body = { + let(:stubbed_response_body) do + { "participants" => [ { "email" => "simulation+dave@demio.com", @@ -28,7 +27,10 @@ } ] }.to_json - event_date_id = 1234 + end + let(:event_date_id) { 1234 } + + it "makes a GET request to Demio's participants endpoint" do stub_request(:get, "https://my.demio.com/api/v1/report/#{event_date_id}/participants") .to_return(body: stubbed_response_body, status: 200) @@ -38,4 +40,17 @@ expect(a_request(:get, "https://my.demio.com/api/v1/report/#{event_date_id}/participants")) .to have_been_made.times(1) end + + context "with additional param" do + it "makes a GET request to Demio's participants endpoint with param" do + stub_request(:get, "https://my.demio.com/api/v1/report/#{event_date_id}/participants?status=left-early") + .to_return(body: stubbed_response_body, status: 200) + + response = client.participants(event_date_id, "left-early") + expect(response.code).to eq("200") + expect(response.body).to eq(stubbed_response_body) + expect(a_request(:get, "https://my.demio.com/api/v1/report/#{event_date_id}/participants?status=left-early")) + .to have_been_made.times(1) + end + end end