diff --git a/lib/github_changelog_generator/octo_fetcher.rb b/lib/github_changelog_generator/octo_fetcher.rb index 150e6be..289dc0d 100644 --- a/lib/github_changelog_generator/octo_fetcher.rb +++ b/lib/github_changelog_generator/octo_fetcher.rb @@ -79,7 +79,7 @@ module GitHubChangelogGenerator last_response = client.last_response if (last_pg = last_response.rels[:last]) - parse_url_for_vars(last_pg.href)["page"].to_i + querystring_as_hash(last_pg.href)["page"].to_i else 1 end @@ -307,16 +307,12 @@ Make sure, that you push tags to remote repo via 'git push --tags'" "#{@options[:user]}/#{@options[:project]}" end - # Parses a URI and returns a hash of all GET variables + # Returns Hash of all querystring variables in given URI. # # @param [String] uri eg. https://api.github.com/repositories/43914960/tags?page=37&foo=1 # @return [Hash] of all GET variables. eg. { 'page' => 37, 'foo' => 1 } - def parse_url_for_vars(uri) - URI(uri).query.split("&").each_with_object({}) do |get_var, params| - k, v = get_var.split("=") - params[k] = v - params - end + def querystring_as_hash(uri) + Hash[URI.decode_www_form(URI(uri).query || "")] end end end diff --git a/spec/unit/octo_fetcher_spec.rb b/spec/unit/octo_fetcher_spec.rb index 4db2ba6..f74e25f 100644 --- a/spec/unit/octo_fetcher_spec.rb +++ b/spec/unit/octo_fetcher_spec.rb @@ -426,6 +426,20 @@ describe GitHubChangelogGenerator::OctoFetcher do end end + describe "#querystring_as_hash" do + it "works on the blank URL" do + expect { fetcher.send(:querystring_as_hash, "") }.not_to raise_error + end + + it "where there are no querystring params" do + expect { fetcher.send(:querystring_as_hash, "http://example.com") }.not_to raise_error + end + + it "returns a String-keyed Hash of querystring params" do + expect(fetcher.send(:querystring_as_hash, "http://example.com/o.html?str=18&wis=12")).to include("wis" => "12", "str" => "18") + end + end + describe "#fetch_commit" do context "when API call is valid", :vcr do it "returns commit" do