Skip to content

Commit

Permalink
Switch from stringex gem to sterile. Move from httpbin.org to (#13)
Browse files Browse the repository at this point in the history
httpbingo.org. Run rake rubocop.
  • Loading branch information
gurgeous authored May 11, 2021
1 parent 112b21f commit 8e3f46c
Show file tree
Hide file tree
Showing 15 changed files with 80 additions and 95 deletions.
1 change: 1 addition & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ Naming/BinaryOperatorParameterName: { Enabled: false } # silly
Naming/HeredocDelimiterNaming: { Enabled: false } # silly
Naming/MethodParameterName: { Enabled: false } # silly
Style/AccessorGrouping: { Enabled: false } # silly
Style/AsciiComments: { Enabled: false } # silly
Style/ClassAndModuleChildren: { Enabled: false } # silly
Style/Documentation: { Enabled: false } # we don't need this
Style/DoubleNegation: { Enabled: false } # silly
Expand Down
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ source 'http://rubygems.org'

group :development do
gem 'minitest'
gem 'mocha'
gem 'rake'
gem 'rubocop', '~> 0.91.0', require: false
gem 'webmock'
Expand Down
6 changes: 5 additions & 1 deletion Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,13 @@ spec = Gem::Specification.load('sinew.gemspec')
#

# test (default)
Rake::TestTask.new { _1.libs << 'test' }
task default: :test

Rake::TestTask.new do
_1.libs << 'test'
_1.warning = false # sterile has a few issues here
end

# Watch rb files, run tests whenever something changes
task :watch do
# https://superuser.com/a/665208 / https://unix.stackexchange.com/a/42288
Expand Down
29 changes: 6 additions & 23 deletions lib/sinew/output.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,6 @@
require 'csv'
require 'set'
require 'stringex'

#
# Stringex customizations
#

# turn '&amp;' into '&', not 'and'
Stringex::Localization::DefaultConversions::HTML_ENTITIES[:amp] = '&'
require 'sterile'

#
# CSV output.
Expand Down Expand Up @@ -109,24 +102,14 @@ def normalize(s)
# strip html tags. Note that we replace tags with spaces
s = s.gsub(/<[^>]+>/, ' ')

#
# Below uses stringex
#
# github.com/rsl/stringex/blob/master/lib/stringex/string_extensions.rb
# github.com/rsl/stringex/blob/master/lib/stringex/localization/conversion_expressions.rb
#

# Converts MS Word 'smart punctuation' to ASCII
s = s.convert_smart_punctuation

# "&aacute;".convert_accented_html_entities # => "a"
s = s.convert_accented_html_entities
s = Sterile.plain_format(s)

# &amp, &frac, etc.
s = s.convert_miscellaneous_html_entities
# &aacute; &amp; etc.
s = Sterile.decode_entities(s)

# convert unicode => regular characters
s = s.to_ascii
# "šţɽĩɳģ" => "string"
s = Sterile.transliterate(s)

# squish
s = s.squish
Expand Down
4 changes: 2 additions & 2 deletions sample.sinew
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
get 'http://httpbin.org'
get 'http://httpbingo.org'
noko.css('ul li a').each do |a|
row = {}
row[:url] = a[:href]
row[:title] = a.text
csv_emit(row)
end

get 'http://httpbin.org/redirect/2'
get 'http://httpbingo.org/redirect/2'
2 changes: 1 addition & 1 deletion sinew.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,5 @@ Gem::Specification.new do |s|
s.add_runtime_dependency 'nokogiri', '~> 1.11'
s.add_runtime_dependency 'scripto', '~> 0'
s.add_runtime_dependency 'slop', '~> 4.8'
s.add_runtime_dependency 'stringex', '~> 2.8'
s.add_runtime_dependency 'sterile', '~> 1.0'
end
2 changes: 1 addition & 1 deletion test/recipes/basic.sinew
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
get 'http://httpbin.org/html'
get 'http://httpbingo.org/html'
raw.scan(/<h1>([^<]+)/) do
csv_emit(h1: $1)
end
Expand Down
2 changes: 1 addition & 1 deletion test/recipes/noko.sinew
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
get 'http://httpbin.org/xml'
get 'http://httpbingo.org/xml'
noko.css('slide title').each do |title|
csv_emit(title: title.text)
end
Expand Down
6 changes: 3 additions & 3 deletions test/recipes/uri.sinew
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
# This tests get by URI, URI math, and csv_emit with uri
get(URI.parse('http://httpbin.org/html'))
get(URI.parse('http://httpbingo.org/html'))
csv_emit(url: uri)

get(uri + '../get')
csv_emit(url: uri)

# OUTPUT
# url
# http://httpbin.org/html
# http://httpbin.org/get
# http://httpbingo.org/html
# http://httpbingo.org/get
2 changes: 1 addition & 1 deletion test/recipes/xml.sinew
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
get 'http://httpbin.org/html'
get 'http://httpbingo.org/html'
noko.css('h1').each do |h1|
csv_emit(h1: h1.text)
end
Expand Down
42 changes: 21 additions & 21 deletions test/test_cache.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,46 +3,46 @@
class TestCache < MiniTest::Test
def test_get
2.times do
sinew.dsl.get('http://httpbin.org/get', c: 3, d: 4)
sinew.dsl.get('http://httpbingo.org/get', c: 3, d: 4)
end
if !test_network?
assert_requested :get, 'http://httpbin.org/get?c=3&d=4', times: 1
assert_requested :get, 'http://httpbingo.org/get?c=3&d=4', times: 1
end
assert_equal({ c: '3', d: '4' }, sinew.dsl.json[:args])
assert_equal({ c: [ '3' ], d: [ '4' ] }, sinew.dsl.json[:args])
end

def test_post
2.times do
sinew.dsl.post('http://httpbin.org/post', c: 5, d: 6)
sinew.dsl.post('http://httpbingo.org/post', c: 5, d: 6)
end
if !test_network?
assert_requested :post, 'http://httpbin.org/post', times: 1
assert_requested :post, 'http://httpbingo.org/post', times: 1
end
assert_equal({ c: '5', d: '6' }, sinew.dsl.json[:form])
assert_equal({ c: [ '5' ], d: [ '6' ] }, sinew.dsl.json[:form])
end

def test_redirect
2.times do
sinew.dsl.get('http://httpbin.org/redirect/2')
sinew.dsl.get('http://httpbingo.org/redirect/2')
end
if !test_network?
assert_requested :get, 'http://httpbin.org/redirect/2', times: 1
assert_requested :get, 'http://httpbin.org/redirect/1', times: 1
assert_requested :get, 'http://httpbin.org/get', times: 1
assert_requested :get, 'http://httpbingo.org/redirect/2', times: 1
assert_requested :get, 'http://httpbingo.org/redirect/1', times: 1
assert_requested :get, 'http://httpbingo.org/get', times: 1
end
assert_equal 'http://httpbin.org/get', sinew.dsl.url
assert_equal 'http://httpbingo.org/get', sinew.dsl.url
end

def test_error
# gotta set this or the retries mess up our request counts
sinew.runtime_options.retries = 0
assert_output(/failed with 500/) do
2.times do
sinew.dsl.get('http://httpbin.org/status/500')
sinew.dsl.get('http://httpbingo.org/status/500')
end
end
if !test_network?
assert_requested :get, 'http://httpbin.org/status/500', times: 1
assert_requested :get, 'http://httpbingo.org/status/500', times: 1
assert_equal '500', sinew.dsl.raw
end
end
Expand All @@ -54,21 +54,21 @@ def test_timeout
sinew.runtime_options.retries = 0
assert_output(/failed with 999/) do
2.times do
sinew.dsl.get('http://httpbin.org/delay/1')
sinew.dsl.get('http://httpbingo.org/delay/1')
end
end
assert_requested :get, 'http://httpbin.org/delay/1', times: 1
assert_requested :get, 'http://httpbingo.org/delay/1', times: 1
assert_empty sinew.dsl.raw
end

def test_force
sinew.options[:force] = true

2.times do
sinew.dsl.get('http://httpbin.org/get', c: 3, d: 4)
sinew.dsl.get('http://httpbingo.org/get', c: 3, d: 4)
end
if !test_network?
assert_requested :get, 'http://httpbin.org/get?c=3&d=4', times: 2
assert_requested :get, 'http://httpbingo.org/get?c=3&d=4', times: 2
end
end

Expand All @@ -81,16 +81,16 @@ def test_force_errors
sinew.runtime_options.retries = 0

2.times do
sinew.dsl.get('http://httpbin.org/get', c: 3, d: 4)
sinew.dsl.get('http://httpbingo.org/get', c: 3, d: 4)
end

assert_output(/failed with 999/) do
2.times do
sinew.dsl.get('http://httpbin.org/delay/1')
sinew.dsl.get('http://httpbingo.org/delay/1')
end
end

assert_requested :get, 'http://httpbin.org/get?c=3&d=4', times: 1
assert_requested :get, 'http://httpbin.org/delay/1', times: 2
assert_requested :get, 'http://httpbingo.org/get?c=3&d=4', times: 1
assert_requested :get, 'http://httpbingo.org/delay/1', times: 2
end
end
9 changes: 5 additions & 4 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
require 'minitest/autorun'
require 'minitest/pride'
require 'mocha/minitest'
require 'webmock/minitest' unless ENV['SINEW_TEST_NETWORK']

# to run one test, do this:
Expand Down Expand Up @@ -54,7 +55,7 @@ def stub_network
#

def respond_html(_request)
# this html was carefully chosen to somewhat match httpbin.org/html
# this html was carefully chosen to somewhat match httpbingo.org/html
html = <<~EOF
<body>
<h1>Herman Melville - Moby-Dick</h1>
Expand All @@ -65,7 +66,7 @@ def respond_html(_request)
protected :respond_html

def respond_xml(_request)
# this xml was carefully chosen to somewhat match httpbin.org/xml
# this xml was carefully chosen to somewhat match httpbingo.org/xml
xml = <<~EOF
<!-- A SAMPLE set of slides -->
<slideshow>
Expand All @@ -87,14 +88,14 @@ def respond_echo(request)

# args
response[:args] = if request.uri.query
CGI.parse(request.uri.query).map { |k, v| [ k, v.first ] }.to_h
CGI.parse(request.uri.query)
else
{}
end

# form
if request.headers['Content-Type'] == 'application/x-www-form-urlencoded'
response[:form] = CGI.parse(request.body).map { |k, v| [ k, v.first ] }.to_h
response[:form] = CGI.parse(request.body)
end

# json
Expand Down
25 changes: 10 additions & 15 deletions test/test_main.rb
Original file line number Diff line number Diff line change
@@ -1,29 +1,24 @@
require_relative 'test_helper'

require 'base64'
require 'mocha'

class TestMain < MiniTest::Test
def test_rate_limit
def setup
super

# true network requests call sleep for timeouts, which interferes with our
# instrumentation of Kernel#sleep
skip if test_network?
end

slept = false

# change Kernel#sleep to not really sleep!
Kernel.send(:alias_method, :old_sleep, :sleep)
Kernel.send(:define_method, :sleep) do |_duration|
slept = true
end
def test_rate_limit
# don't sleep, but expect it to get called
Sinew::Connection::RateLimit.any_instance.expects(:sleep)

sinew.runtime_options.rate_limit = 1
sinew.dsl.get('http://httpbin.org/html')
sinew.dsl.get('http://httpbin.org/get')
assert(slept)

# restore old Kernel#sleep
Kernel.send(:alias_method, :sleep, :old_sleep)
Kernel.send(:undef_method, :old_sleep)
sinew.dsl.get('http://httpbingo.org/html')
sinew.dsl.get('http://httpbingo.org/get')
end

def test_gunzip
Expand Down
Loading

0 comments on commit 8e3f46c

Please sign in to comment.