Skip to content

Commit

Permalink
simplifies url encoding and adds a few tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jduss4 committed Dec 18, 2017
1 parent 6db6c34 commit 5c9b42b
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 17 deletions.
18 changes: 7 additions & 11 deletions lib/api_bridge/helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,13 @@ def self.calculate_page count, rows
end

def self.encode url
# Split components of URL so we may encode only the query string, index 7
components = URI.split(url).map.with_index { |component, i|
i != 7 || component == "" ? component : encode_query_params(component)
}

# Assume URLs are hierarchical, i.e. (scheme)://... not opaque (scheme):...
url = "#{components[0]}://" << components[1..5].join("")
url << "?#{components[7]}" if components[7] != ""
url << components[8] if components[8]

url
# Split components of URL so we may encode only the query string
request_url, query = url.split("?")
if query
encoded = encode_query_params(query)
request_url << "?#{encoded}"
end
request_url
end

def self.escape_values options
Expand Down
28 changes: 22 additions & 6 deletions test/helper_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,28 @@ def test_calculate_page
end

def test_encode
fake_url = "http://something.unl.edu/?param="
fake_url = "http://something.unl.edu/"

# semicolons
assert_equal "#{fake_url}?param=Cather%3B+Pound",
ApiBridge.encode("#{fake_url}?param=Cather; Pound")
assert_equal "#{fake_url}?param=Cather%2C+Pound",
ApiBridge.encode("#{fake_url}?param=Cather, Pound")
# multiple types of characters
assert_equal "#{fake_url}?f[]=works%7CTitle+%282012%29+Author",
ApiBridge.encode("#{fake_url}?f[]=works|Title (2012) Author")
# multiple parameters
query_in = "f[]=works|Title (1827)&f[]=works|Title2, Author; Author2&q=*"
query_out = "f[]=works%7CTitle+%281827%29&f[]=works%7CTitle2%2C+Author%3B+Author2&q=*"
assert_equal "#{fake_url}?#{query_out}",
ApiBridge.encode("#{fake_url}?#{query_in}")
# no parameters
assert_equal fake_url, ApiBridge.encode(fake_url)
end

assert_equal "#{fake_url}Cather%3B+Pound",
ApiBridge.encode("#{fake_url}Cather; Pound")
assert_equal "#{fake_url}Cather%2C+Pound",
ApiBridge.encode("#{fake_url}Cather, Pound")
def test_encode_query_params
assert_equal "authors=Cather%3B+Pound&f[]=works%7CSomething",
ApiBridge.encode_query_params("authors=Cather; Pound&f[]=works|Something")
end

def test_escape_values
Expand All @@ -39,7 +55,7 @@ def test_is_url?
end

def test_override_options

# TODO
end

end

0 comments on commit 5c9b42b

Please sign in to comment.