Skip to content

Commit 4de8da9

Browse files
committed
Added Rubocop (through BestPracticeProject), RSpec 3 and added more specs
1 parent 0a16ec2 commit 4de8da9

29 files changed

+422
-210
lines changed

Gemfile.lock

Lines changed: 37 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,15 @@ PATH
77
GEM
88
remote: http://rubygems.org/
99
specs:
10+
ast (2.1.0)
11+
astrolabe (1.3.1)
12+
parser (~> 2.2)
13+
best_practice_project (0.0.4)
14+
psych (~> 2.0.0)
1015
codeclimate-test-reporter (0.4.7)
1116
simplecov (>= 0.7.1, < 1.0.0)
1217
datet (0.0.25)
13-
diff-lcs (1.1.3)
18+
diff-lcs (1.2.5)
1419
docile (1.1.5)
1520
erubis (2.7.0)
1621
hayabusa (0.0.25)
@@ -33,21 +38,38 @@ GEM
3338
mime-types (>= 1.16, < 3)
3439
mime-types (2.4.3)
3540
multi_json (1.11.0)
41+
parser (2.2.3.0)
42+
ast (>= 1.1, < 3.0)
3643
php4r (0.0.4)
3744
datet
3845
http2
3946
string-strtr
47+
powerpack (0.1.1)
48+
psych (2.0.15)
49+
rainbow (2.0.0)
4050
rake (10.4.2)
4151
rdoc (3.12.2)
4252
json (~> 1.4)
43-
rspec (2.8.0)
44-
rspec-core (~> 2.8.0)
45-
rspec-expectations (~> 2.8.0)
46-
rspec-mocks (~> 2.8.0)
47-
rspec-core (2.8.0)
48-
rspec-expectations (2.8.0)
49-
diff-lcs (~> 1.1.2)
50-
rspec-mocks (2.8.0)
53+
rspec (3.4.0)
54+
rspec-core (~> 3.4.0)
55+
rspec-expectations (~> 3.4.0)
56+
rspec-mocks (~> 3.4.0)
57+
rspec-core (3.4.1)
58+
rspec-support (~> 3.4.0)
59+
rspec-expectations (3.4.0)
60+
diff-lcs (>= 1.2.0, < 2.0)
61+
rspec-support (~> 3.4.0)
62+
rspec-mocks (3.4.0)
63+
diff-lcs (>= 1.2.0, < 2.0)
64+
rspec-support (~> 3.4.0)
65+
rspec-support (3.4.1)
66+
rubocop (0.35.0)
67+
astrolabe (~> 1.3)
68+
parser (>= 2.2.3.0, < 3.0)
69+
powerpack (~> 0.1)
70+
rainbow (>= 1.99.1, < 3.0)
71+
ruby-progressbar (~> 1.7)
72+
ruby-progressbar (1.7.5)
5173
ruby_process (0.0.9)
5274
tsafe
5375
wref
@@ -67,11 +89,16 @@ PLATFORMS
6789
ruby
6890

6991
DEPENDENCIES
92+
best_practice_project (>= 0.0.4)
7093
bundler (>= 1.0.0)
7194
codeclimate-test-reporter
7295
hayabusa (~> 0.0.25)
7396
http2!
7497
rake
7598
rdoc (~> 3.12)
76-
rspec (~> 2.8.0)
99+
rspec (~> 3.4.0)
100+
rubocop (~> 0.35.0)
77101
sqlite3
102+
103+
BUNDLED WITH
104+
1.10.6

Rakefile

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,40 @@
11
# encoding: utf-8
22

3-
require 'rubygems'
4-
require 'bundler'
3+
require "rubygems"
4+
require "bundler"
55
begin
66
Bundler.setup(:default, :development)
77
rescue Bundler::BundlerError => e
88
$stderr.puts e.message
99
$stderr.puts "Run `bundle install` to install missing gems"
1010
exit e.status_code
1111
end
12-
require 'rake'
12+
require "rake"
1313

14-
require 'rspec/core'
15-
require 'rspec/core/rake_task'
14+
require "rspec/core"
15+
require "rspec/core/rake_task"
1616
RSpec::Core::RakeTask.new(:spec) do |spec|
17-
spec.pattern = FileList['spec/**/*_spec.rb']
17+
spec.pattern = FileList["spec/**/*_spec.rb"]
1818
end
1919

2020
RSpec::Core::RakeTask.new(:rcov) do |spec|
21-
spec.pattern = 'spec/**/*_spec.rb'
21+
spec.pattern = "spec/**/*_spec.rb"
2222
spec.rcov = true
2323
end
2424

25-
task :default => :spec
25+
task default: :spec
2626

27-
require 'rdoc/task'
27+
require "rdoc/task"
2828
Rake::RDocTask.new do |rdoc|
29-
version = File.exist?('VERSION') ? File.read('VERSION') : ""
29+
version = File.exist?("VERSION") ? File.read("VERSION") : ""
3030

31-
rdoc.rdoc_dir = 'rdoc'
31+
rdoc.rdoc_dir = "rdoc"
3232
rdoc.title = "http2 #{version}"
33-
rdoc.rdoc_files.include('README*')
34-
rdoc.rdoc_files.include('lib/**/*.rb')
33+
rdoc.rdoc_files.include("README*")
34+
rdoc.rdoc_files.include("lib/**/*.rb")
3535
end
3636

3737
Bundler::GemHelper.install_tasks
38+
39+
require "best_practice_project"
40+
BestPracticeProject.load_tasks

config/rubocop.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
---
2+
inherit_from: rubocop_todo.yml

config/rubocop_todo.yml

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
inherit_from: "$$$best_practice_project_config_path$$$"
2+
3+
# This configuration was generated by
4+
# `rubocop --auto-gen-config`
5+
# on 2015-11-25 09:59:59 +0100 using RuboCop version 0.35.0.
6+
# The point is for the user to remove these configuration records
7+
# one by one as the offenses are removed from the code base.
8+
# Note that changes in the inspected code, or installation of new
9+
# versions of RuboCop, may require this file to be generated again.
10+
11+
# Offense count: 3
12+
Metrics/AbcSize:
13+
Max: 39
14+
15+
# Offense count: 12
16+
Metrics/CyclomaticComplexity:
17+
Max: 13
18+
19+
# Offense count: 4
20+
# Configuration parameters: AllowURI, URISchemes.
21+
Metrics/LineLength:
22+
Max: 221
23+
24+
# Offense count: 8
25+
Metrics/PerceivedComplexity:
26+
Max: 13
27+
28+
# Offense count: 6
29+
# Configuration parameters: MinBodyLength.
30+
Style/GuardClause:
31+
Exclude:
32+
- 'lib/http2.rb'
33+
- 'lib/http2/response_reader.rb'
34+
35+
# Offense count: 3
36+
# Cop supports --auto-correct.
37+
Style/PerlBackrefs:
38+
Exclude:
39+
- 'lib/http2/utils.rb'
40+
41+
# Offense count: 5
42+
# Cop supports --auto-correct.
43+
# Configuration parameters: EnforcedStyle, SupportedStyles, AllowInnerSlashes.
44+
Style/RegexpLiteral:
45+
Exclude:
46+
- 'lib/http2.rb'
47+
- 'lib/http2/connection.rb'
48+
- 'lib/http2/response_reader.rb'
49+
- 'spec/http2_spec.rb'
50+
51+
# Offense count: 1
52+
# Cop supports --auto-correct.
53+
# Configuration parameters: ExactNameMatch, AllowPredicates, AllowDSLWriters, IgnoreClassMethods, Whitelist.
54+
Style/TrivialAccessors:
55+
Exclude:
56+
- 'lib/http2/connection.rb'
57+
58+
# Offense count: 1
59+
# Cop supports --auto-correct.
60+
# Configuration parameters: WordRegex.
61+
Style/WordArray:
62+
MinSize: 3

http2.gemspec

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,12 @@ Gem::Specification.new do |s|
2525

2626
s.add_runtime_dependency("string-cases", "~> 0")
2727
s.add_development_dependency("rake")
28-
s.add_development_dependency("rspec", "~> 2.8.0")
28+
s.add_development_dependency("rspec", "~> 3.4.0")
2929
s.add_development_dependency("rdoc", "~> 3.12")
3030
s.add_development_dependency("bundler", ">= 1.0.0")
3131
s.add_development_dependency("hayabusa", "~> 0.0.25")
3232
s.add_development_dependency("sqlite3")
3333
s.add_development_dependency("codeclimate-test-reporter")
34+
s.add_development_dependency("best_practice_project", ">= 0.0.4")
35+
s.add_development_dependency("rubocop", "~> 0.35.0")
3436
end
35-

lib/http2.rb

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
require "monitor" unless ::Kernel.const_defined?(:Monitor)
44
require "string-cases"
55

6-
#This class tries to emulate a browser in Ruby without any visual stuff. Remember cookies, keep sessions alive, reset connections according to keep-alive rules and more.
6+
# This class tries to emulate a browser in Ruby without any visual stuff. Remember cookies, keep sessions alive, reset connections according to keep-alive rules and more.
77
#===Examples
88
# Http2.new(host: "www.somedomain.com", port: 80, ssl: false, debug: false) do |http|
99
# res = http.get("index.rhtml?show=some_page")
@@ -18,7 +18,7 @@ class Http2
1818
# Autoloader for subclasses.
1919
def self.const_missing(name)
2020
require "#{File.dirname(__FILE__)}/http2/#{::StringCases.camel_to_snake(name)}.rb"
21-
return Http2.const_get(name)
21+
Http2.const_get(name)
2222
end
2323

2424
# Converts a URL to "is.gd"-short-URL.
@@ -45,17 +45,21 @@ def initialize(args = {})
4545
begin
4646
yield(self)
4747
ensure
48-
self.destroy
48+
destroy
4949
end
5050
end
5151
end
5252

5353
def host
54-
@args[:host]
54+
@args.fetch(:host)
5555
end
5656

5757
def port
58-
@args[:port]
58+
@args.fetch(:port)
59+
end
60+
61+
def ssl?
62+
@args[:ssl] ? true : false
5963
end
6064

6165
def reconnect
@@ -68,7 +72,7 @@ def new_url
6872
builder.port = port
6973
builder.protocol = @args[:protocol]
7074

71-
return builder
75+
builder
7276
end
7377

7478
# Closes current connection if any, changes the arguments on the object and reconnects keeping all cookies and other stuff intact.
@@ -104,9 +108,9 @@ def parse_args(*args)
104108
raise "Invalid arguments: '#{args.class.name}'"
105109
end
106110

107-
raise "Invalid URL: '#{args[:url]}'" unless args[:url].to_s.split("\n").length == 1
111+
raise "Invalid URL: '#{args[:url]}'" if args[:url] != "" && args[:url].to_s.split("\n").length != 1
108112

109-
return args
113+
args
110114
end
111115

112116
# Returns a result-object based on the arguments.
@@ -154,7 +158,7 @@ def default_headers(args = {})
154158

155159
headers.merge!(@args[:extra_headers]) if @args[:extra_headers]
156160
headers.merge!(args[:headers]) if args[:headers]
157-
return headers
161+
headers
158162
end
159163

160164
# Posts to a certain page.
@@ -172,29 +176,29 @@ def post_multipart(*args)
172176
end
173177

174178
# Returns a header-string which normally would be used for a request in the given state.
175-
def header_str(headers_hash, args = {})
179+
def header_str(headers_hash)
176180
headers_hash["Cookie"] = cookie_header_string
177181

178182
headers_str = ""
179183
headers_hash.each do |key, val|
180184
headers_str << "#{key}: #{val}#{@nl}"
181185
end
182186

183-
return headers_str
187+
headers_str
184188
end
185189

186190
def cookie_header_string
187191
cstr = ""
188192

189193
first = true
190-
@cookies.each do |cookie_name, cookie|
194+
@cookies.each do |_cookie_name, cookie|
191195
cstr << "; " unless first
192196
first = false if first
193197
ensure_single_lines([cookie.name, cookie.value])
194198
cstr << "#{Http2::Utils.urlenc(cookie.name)}=#{Http2::Utils.urlenc(cookie.value)}"
195199
end
196200

197-
return cstr
201+
cstr
198202
end
199203

200204
def cookie(name)
@@ -231,13 +235,13 @@ def inspect
231235
private
232236

233237
def host_header
234-
#Possible to give custom host-argument.
238+
# Possible to give custom host-argument.
235239
host = args[:host] || self.host
236240
port = args[:port] || self.port
237241

238242
host_header_string = "#{host}" # Copy host string to avoid changing the original string if port has been given!
239243
host_header_string << ":#{port}" if port && ![80, 443].include?(port.to_i) && !@args[:skip_port_in_host_header]
240-
return host_header_string
244+
host_header_string
241245
end
242246

243247
# Registers the states from a result.
@@ -247,7 +251,6 @@ def autostate_register(res)
247251

248252
res.body.to_s.scan(/<input type="hidden" name="__(EVENTTARGET|EVENTARGUMENT|VIEWSTATE|LASTFOCUS)" id="(.*?)" value="(.*?)" \/>/) do |match|
249253
name = "__#{match[0]}"
250-
id = match[1]
251254
value = match[2]
252255

253256
puts "Http2: Registered autostate-value with name '#{name}' and value '#{value}'." if @debug
@@ -266,22 +269,22 @@ def parse_init_args(args)
266269
args = {host: args} if args.is_a?(String)
267270
raise "Arguments wasnt a hash." unless args.is_a?(Hash)
268271

269-
args.each do |key, val|
272+
args.each do |key, _val|
270273
raise "Invalid key: '#{key}'." unless VALID_ARGUMENTS_INITIALIZE.include?(key)
271274
end
272275

273276
args[:proxy][:connect] = true if args[:proxy] && !args[:proxy].key?(:connect) && args[:ssl]
274277

275278
raise "No host was given." unless args[:host]
276-
return args
279+
args
277280
end
278281

279282
def set_default_values
280283
@debug = @args[:debug]
281284
@autostate_values = {} if autostate
282285
@nl = @args[:nl] || "\r\n"
283286

284-
if !@args[:port]
287+
unless @args[:port]
285288
if @args[:ssl]
286289
@args[:port] = 443
287290
else

lib/http2/base_request.rb

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class Http2::BaseRequest
2+
attr_reader :http2, :args, :debug
3+
4+
VALID_ARGUMENTS_POST = [:post, :url, :default_headers, :headers, :json, :method, :cookies, :on_content, :content_type]
5+
6+
def initialize(http2, args)
7+
@http2 = http2
8+
@args = http2.parse_args(args)
9+
@debug = http2.debug
10+
@nl = http2.nl
11+
12+
@args.each do |key, _val|
13+
raise "Invalid key: '#{key}'." unless VALID_ARGUMENTS_POST.include?(key)
14+
end
15+
16+
@conn = @http2.connection
17+
end
18+
19+
def path
20+
@args.fetch(:url)
21+
end
22+
end

0 commit comments

Comments
 (0)