Skip to content
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

27: Add pagination support (Ruby) #32

Closed
wants to merge 1 commit into from
Closed
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
4 changes: 2 additions & 2 deletions ruby/bin/github_repo_cloner
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#!/usr/bin/env ruby

require_relative '../lib/clone_handler'
require_relative '../lib/clone_all_handler'

CloneHandler.new(ARGV.first).call
CloneAllHandler.new(ARGV.first).call
21 changes: 21 additions & 0 deletions ruby/lib/clone_all_handler.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
require_relative 'clone_page_handler'

class CloneAllHandler
def initialize(username)
@username = username
@page = 1
@has_next_page = true
end

def call
while has_next_page
self.has_next_page = ClonePageHandler.new(username, page).call
self.page += 1
end
end

private

attr_reader :username
attr_accessor :page, :has_next_page
end
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What changes has_next_page to be false?
I looked in ClonePageHandler but am not finding the code.

11 changes: 6 additions & 5 deletions ruby/lib/clone_handler.rb → ruby/lib/clone_page_handler.rb
Original file line number Diff line number Diff line change
@@ -1,23 +1,24 @@
require 'json'
require 'net/http'

class CloneHandler
def initialize(username)
class ClonePageHandler
def initialize(username, page)
@username = username
@page = page
end

def call
return unless clonable?
return false unless clonable?

Kernel.system(clone_command)
end

private

attr_reader :username
attr_reader :username, :page

def info_uri
URI("https://api.github.com/users/#{username}/repos")
URI("https://api.github.com/users/#{username}/repos?page=#{page}")
end

def response
Expand Down
18 changes: 18 additions & 0 deletions ruby/spec/clone_all_handler_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
require 'clone_all_handler'
require 'clone_page_handler'

RSpec.describe CloneAllHandler do
describe '#call' do
let(:username) { 'murjax' }

subject(:call) { described_class.new(username).call }

it 'calls ClonePageHandler with each page until blank page reached' do
expect(ClonePageHandler).to receive(:new).with(username, 1).and_return(OpenStruct.new(call: true))
expect(ClonePageHandler).to receive(:new).with(username, 2).and_return(OpenStruct.new(call: true))
expect(ClonePageHandler).to receive(:new).with(username, 3).and_return(OpenStruct.new(call: false))

call
end
end
end
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
require 'clone_handler'
require 'clone_page_handler'
require 'json'
require 'net/http'

RSpec.describe CloneHandler do
RSpec.describe ClonePageHandler do
describe '#call' do
let(:username) { 'murjax' }
let(:page) { 2 }
let(:clone_url1) { 'https://github.com/murjax/spring_engine.git' }
let(:clone_url2) { 'https://github.com/murjax/burger_bot.git' }
let(:name1) { 'spring_engine' }
let(:name2) { 'burger_bot' }
let(:repo_info_url) { "https://api.github.com/users/#{username}/repos" }
let(:repo_info_url) { "https://api.github.com/users/#{username}/repos?page=#{page}" }
let(:repo_info_uri) { URI(repo_info_url) }
let(:serialized_response_body) { JSON.generate(response_body) }
let(:response) { OpenStruct.new(code: code, body: serialized_response_body) }

subject(:call) { described_class.new(username).call }
subject(:call) { described_class.new(username, page).call }

context 'valid username with repos' do
let(:response_body) do
Expand All @@ -30,9 +31,9 @@

it 'runs clone commands' do
expect(Net::HTTP).to receive(:get_response).with(repo_info_uri).and_return(response)
expect(Kernel).to receive(:system).with(final_command)
expect(Kernel).to receive(:system).with(final_command).and_return(true)

call
expect(call).to eq(true)
end
end

Expand All @@ -44,7 +45,7 @@
expect(Net::HTTP).to receive(:get_response).with(repo_info_uri).and_return(response)
expect(Kernel).not_to receive(:system)

call
expect(call).to eq(false)
end
end

Expand All @@ -56,7 +57,7 @@
expect(Net::HTTP).to receive(:get_response).with(repo_info_uri).and_return(response)
expect(Kernel).not_to receive(:system)

call
expect(call).to eq(false)
end
end

Expand All @@ -67,7 +68,7 @@
expect(Net::HTTP).not_to receive(:get_response)
expect(Kernel).not_to receive(:system)

call
expect(call).to eq(false)
end
end

Expand All @@ -78,7 +79,7 @@
expect(Net::HTTP).not_to receive(:get_response)
expect(Kernel).not_to receive(:system)

call
expect(call).to eq(false)
end
end
end
Expand Down