http2 0.0.28 → 0.0.29

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: a28eaa08bb929414333c763236db9b220a499eb8
4
- data.tar.gz: 8cb38c1995fc84a1562b3c8a117ed702fff75db1
3
+ metadata.gz: 73d24887d39ea86831b94629d6dc98eed99333eb
4
+ data.tar.gz: 364f8d08601a1279bfdf2a214d02d545473a806a
5
5
  SHA512:
6
- metadata.gz: 63c24b5f90f05a1e8f67a59cbe7765475bd087ae384b873c47ee90794f52e1ea5c88386769e30bdc51234fde4f32ae2454d16e990acf13fc97ba5e7a81e4bcce
7
- data.tar.gz: 0f42d6609f0520b481723dd5c0d648371cd8b2b0f050c6608f45e81b9a511c87d4a651942d168c21f92d30fbb08f826c40450666ec8da657ba5f27ef5b493242
6
+ metadata.gz: a856ec3dd70d2db8640a53b109592acfd17af7ff66a3130a2200f4cf5b38317dc9e51cf2996a871a5d959f957de78361f63b069d88dc84d69fb19e8deeba9771
7
+ data.tar.gz: fd6d03348ab61b6b5b18cca4c0f27369d23bdf756d9f8fc9d5ee40a92e16fd90b054265f171b5ff8599f2141656873980011dfb0273096dc3cc0b690ed042d46
data/README.md CHANGED
@@ -10,7 +10,7 @@ Example of usage:
10
10
  require "rubygems"
11
11
  require "http2"
12
12
 
13
- Http2.new(:host => "www.google.dk") do |http|
13
+ Http2.new(host: "www.google.dk") do |http|
14
14
  #Get-request.
15
15
  res = http.get("path/to/something")
16
16
  puts res.body
@@ -18,17 +18,17 @@ Http2.new(:host => "www.google.dk") do |http|
18
18
  puts "Specific header: #{res.header("HeaderName")}"
19
19
 
20
20
  #Post-request.
21
- res = http.post(:url => "path/to/something", :post => {
21
+ res = http.post(url: "path/to/something", post: {
22
22
  "some_post_val" => "some_value"
23
23
  })
24
24
 
25
25
  res.content_type #=> "text/html"
26
26
 
27
27
  #Post-multipart (upload).
28
- res = http.post_multipart(:url => "path/to/something", :post => {
28
+ res = http.post_multipart(url: "path/to/something", post: {
29
29
  "test_file1" => {
30
- :fpath => fpath,
31
- :filename => "specfile"
30
+ fpath: fpath,
31
+ filename: "specfile"
32
32
  }
33
33
  })
34
34
 
@@ -36,6 +36,28 @@ Http2.new(:host => "www.google.dk") do |http|
36
36
  end
37
37
  ```
38
38
 
39
+ ## Get parameters.
40
+
41
+ ```ruby
42
+ http.host => example.com
43
+ http.port => 80
44
+ ```
45
+
46
+ ## Response details.
47
+
48
+ ```ruby
49
+ resp = http.get("path/to/something")
50
+ resp.content_type #=> "text/html"
51
+ resp.content_length #=> 136
52
+ resp.header("content-length") #=> "136"
53
+ resp.headers #=> {"content-type" => ["text/html"], "content-length" => ["136"]}
54
+ resp.code #=> "200"
55
+ resp.charset #=> "utf-8"
56
+ resp.http_version #=> "1.1"
57
+ resp.body #=> "<html><body>..."
58
+ resp.requested_url #=> "http://example.com/maybe/redirected/path/to/something"
59
+ ```
60
+
39
61
 
40
62
  ## Reconnect
41
63
 
@@ -59,4 +81,3 @@ http.reconnect
59
81
 
60
82
  Copyright (c) 2012 Kasper Johansen. See LICENSE.txt for
61
83
  further details.
62
-
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.28
1
+ 0.0.29
data/http2.gemspec CHANGED
@@ -2,16 +2,16 @@
2
2
  # DO NOT EDIT THIS FILE DIRECTLY
3
3
  # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
4
  # -*- encoding: utf-8 -*-
5
- # stub: http2 0.0.28 ruby lib
5
+ # stub: http2 0.0.29 ruby lib
6
6
 
7
7
  Gem::Specification.new do |s|
8
8
  s.name = "http2"
9
- s.version = "0.0.28"
9
+ s.version = "0.0.29"
10
10
 
11
11
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
12
12
  s.require_paths = ["lib"]
13
13
  s.authors = ["Kasper Johansen"]
14
- s.date = "2014-12-03"
14
+ s.date = "2014-12-04"
15
15
  s.description = "A lightweight framework for doing http-connections in Ruby. Supports cookies, keep-alive, compressing and much more."
16
16
  s.email = "k@spernj.org"
17
17
  s.extra_rdoc_files = [
@@ -41,6 +41,7 @@ Gem::Specification.new do |s|
41
41
  "lib/http2.rb",
42
42
  "shippable.yml",
43
43
  "spec/http2/post_data_generator_spec.rb",
44
+ "spec/http2/response_spec.rb",
44
45
  "spec/http2/url_builder_spec.rb",
45
46
  "spec/http2_spec.rb",
46
47
  "spec/spec_helper.rb"
data/include/errors.rb CHANGED
@@ -3,9 +3,9 @@ class Http2::Errors
3
3
  class Http2error < RuntimeError
4
4
  attr_accessor :response
5
5
  end
6
-
6
+
7
7
  class Noaccess < Http2error; end
8
8
  class Internalserver < Http2error; end
9
9
  class Notfound < Http2error; end
10
10
  class Badrequest < Http2error; end
11
- end
11
+ end
@@ -53,7 +53,7 @@ private
53
53
 
54
54
  def generate_key_value(key, value)
55
55
  if value.is_a?(Hash) || value.is_a?(Array)
56
- return ::Http2::PostDataGenerator.new(value, :orig_key => key).generate
56
+ return ::Http2::PostDataGenerator.new(value, orig_key: key).generate
57
57
  else
58
58
  data = ::Http2::PostDataGenerator.new(value).generate
59
59
  return "#{Http2::Utils.urlenc(key)}=#{Http2::Utils.urlenc(data)}"
data/include/response.rb CHANGED
@@ -7,7 +7,7 @@ class Http2::Response
7
7
  #This method should not be called manually.
8
8
  def initialize(args = {})
9
9
  @args = args
10
- @args[:headers] = {} if !@args.key?(:headers)
10
+ @args[:headers] = {} unless @args.key?(:headers)
11
11
  @body = args[:body] || ""
12
12
  @debug = @args[:debug]
13
13
  end
@@ -31,7 +31,7 @@ class Http2::Response
31
31
  #===Examples
32
32
  # print "No content-type was given." if !http.header?("content-type")
33
33
  def header?(key)
34
- return true if @args[:headers].key?(key) and @args[:headers][key].first.to_s.length > 0
34
+ return true if @args[:headers].key?(key) && @args[:headers][key].first.to_s.length > 0
35
35
  return false
36
36
  end
37
37
 
@@ -43,7 +43,7 @@ class Http2::Response
43
43
  #===Examples
44
44
  # res.requested_url #=> "?show=status&action=getstatus"
45
45
  def requested_url
46
- raise "URL could not be detected." if !@args[:request_args][:url]
46
+ raise "URL could not be detected." unless @args[:request_args][:url]
47
47
  return @args[:request_args][:url]
48
48
  end
49
49
 
@@ -53,7 +53,7 @@ class Http2::Response
53
53
  validate_body_versus_content_length!
54
54
  end
55
55
 
56
- private
56
+ private
57
57
 
58
58
  # Checks that the length of the body is the same as the given content-length if given.
59
59
  def validate_body_versus_content_length!
@@ -62,7 +62,7 @@ class Http2::Response
62
62
  return nil
63
63
  end
64
64
 
65
- content_length = self.header("content-length").to_i
65
+ content_length = header("content-length").to_i
66
66
  body_length = @body.bytesize
67
67
 
68
68
  puts "Http2: Body length: #{body_length}" if @debug
@@ -70,4 +70,4 @@ class Http2::Response
70
70
 
71
71
  raise "Body does not match the given content-length: '#{body_length}', '#{content_length}'." if body_length != content_length
72
72
  end
73
- end
73
+ end
@@ -4,7 +4,7 @@ class Http2::ResponseReader
4
4
  def initialize(args)
5
5
  @mode = "headers"
6
6
  @transfer_encoding = nil
7
- @response = Http2::Response.new(:request_args => args, :debug => @debug)
7
+ @response = Http2::Response.new(request_args: args, debug: @debug)
8
8
  @rec_count = 0
9
9
  @args, @debug, @http2, @sock = args[:args], args[:http2].debug, args[:http2], args[:sock]
10
10
  @nl = @http2.nl
@@ -169,7 +169,7 @@ private
169
169
  content_type_line.gsub!(match_charset[0], "")
170
170
  end
171
171
 
172
- @response.content_type = @content_type_line
172
+ @response.content_type = content_type_line
173
173
  end
174
174
 
175
175
  #Parse a header-line and saves it on the object.
data/include/utils.rb CHANGED
@@ -7,7 +7,7 @@ class Http2::Utils
7
7
  '%' + $1.unpack('H2' * $1.bytesize).join('%').upcase
8
8
  end.tr(' ', '+')
9
9
  end
10
-
10
+
11
11
  #URL-decodes a string.
12
12
  def self.urldec(string)
13
13
  #Thanks to CGI framework
@@ -15,26 +15,26 @@ class Http2::Utils
15
15
  [$1.delete('%')].pack('H*')
16
16
  end
17
17
  end
18
-
18
+
19
19
  #Parses a cookies-string and returns them in an array.
20
20
  def self.parse_set_cookies(str)
21
21
  str = String.new(str.to_s)
22
22
  return [] if str.length <= 0
23
23
  args = {}
24
24
  cookie_start_regex = /^(.+?)=(.*?)(;\s*|$)/
25
-
25
+
26
26
  match = str.match(cookie_start_regex)
27
27
  raise "Could not match cookie: '#{str}'." if !match
28
28
  str.gsub!(cookie_start_regex, "")
29
-
29
+
30
30
  args["name"] = self.urldec(match[1].to_s)
31
31
  args["value"] = self.urldec(match[2].to_s)
32
-
32
+
33
33
  while match = str.match(/(.+?)=(.*?)(;\s*|$)/)
34
34
  str = str.gsub(match[0], "")
35
35
  args[match[1].to_s.downcase] = match[2].to_s
36
36
  end
37
-
37
+
38
38
  return [args]
39
39
  end
40
- end
40
+ end
data/lib/http2.rb CHANGED
@@ -5,7 +5,7 @@ require "string-cases"
5
5
 
6
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.
7
7
  #===Examples
8
- # Http2.new(:host => "www.somedomain.com", :port => 80, :ssl => false, :debug => false) do |http|
8
+ # Http2.new(host: "www.somedomain.com", port: 80, ssl: false, debug: false) do |http|
9
9
  # res = http.get("index.rhtml?show=some_page")
10
10
  # html = res.body
11
11
  # print html
@@ -23,7 +23,7 @@ class Http2
23
23
 
24
24
  #Converts a URL to "is.gd"-short-URL.
25
25
  def self.isgdlink(url)
26
- Http2.new(:host => "is.gd") do |http|
26
+ Http2.new(host: "is.gd") do |http|
27
27
  resp = http.get("/api.php?longurl=#{url}")
28
28
  return resp.body
29
29
  end
@@ -50,14 +50,22 @@ class Http2
50
50
  end
51
51
  end
52
52
 
53
+ def host
54
+ @args[:host]
55
+ end
56
+
57
+ def port
58
+ @args[:port]
59
+ end
60
+
53
61
  def reconnect
54
62
  @connection.reconnect
55
63
  end
56
64
 
57
65
  def new_url
58
66
  builder = Http2::UrlBuilder.new
59
- builder.host = @args[:host]
60
- builder.port = @args[:port]
67
+ builder.host = host
68
+ builder.port = port
61
69
  builder.protocol = @args[:protocol]
62
70
 
63
71
  return builder
@@ -89,7 +97,7 @@ class Http2
89
97
  #Forces various stuff into arguments-hash like URL from original arguments and enables single-string-shortcuts and more.
90
98
  def parse_args(*args)
91
99
  if args.length == 1 && args.first.is_a?(String)
92
- args = {:url => args.first}
100
+ args = {url: args.first}
93
101
  elsif args.length >= 2
94
102
  raise "Couldnt parse arguments."
95
103
  elsif args.is_a?(Array) && args.length == 1
@@ -116,9 +124,9 @@ class Http2
116
124
  # Proxies the request to another method but forces the method to be "DELETE".
117
125
  def delete(args)
118
126
  if args[:json]
119
- return post(args.merge(:method => :delete))
127
+ return post(args.merge(method: :delete))
120
128
  else
121
- return get(args.merge(:method => :delete))
129
+ return get(args.merge(method: :delete))
122
130
  end
123
131
  end
124
132
 
@@ -135,8 +143,8 @@ class Http2
135
143
  }
136
144
 
137
145
  #Possible to give custom host-argument.
138
- host = args[:host] || @args[:host]
139
- port = args[:port] || @args[:port]
146
+ host = args[:host] || self.host
147
+ port = args[:port] || self.port
140
148
 
141
149
  headers["Host"] = host
142
150
  headers["Host"] << ":#{port}" if port && ![80, 443].include?(port.to_i) && !@args[:skip_port_in_host_header]
@@ -212,6 +220,14 @@ class Http2
212
220
  ::Http2::ResponseReader.new(http2: self, sock: @sock, args: args).response
213
221
  end
214
222
 
223
+ def to_s
224
+ "<Http2 host: #{host}:#{port}>"
225
+ end
226
+
227
+ def inspect
228
+ to_s
229
+ end
230
+
215
231
  private
216
232
 
217
233
  #Registers the states from a result.
@@ -237,7 +253,7 @@ private
237
253
  end
238
254
 
239
255
  def parse_init_args(args)
240
- args = {:host => args} if args.is_a?(String)
256
+ args = {host: args} if args.is_a?(String)
241
257
  raise "Arguments wasnt a hash." unless args.is_a?(Hash)
242
258
 
243
259
  args.each do |key, val|
@@ -273,4 +289,4 @@ private
273
289
  @raise_errors = false
274
290
  end
275
291
  end
276
- end
292
+ end
data/shippable.yml CHANGED
@@ -1,6 +1,8 @@
1
1
  language: ruby
2
+ cache: bundler
3
+ archive: true
2
4
  rvm:
3
- - 2.0.0
5
+ - 2.1.2
4
6
  script:
5
7
  - CODECLIMATE_REPO_TOKEN=df980a87f4dbf846f13115f0ff1a8179bf165b645dcf12204a96c91d9e665f8e bundle exec rspec
6
8
  notifications:
@@ -0,0 +1,17 @@
1
+ require "spec_helper"
2
+
3
+ describe Http2::Response do
4
+ it "should register content type" do
5
+ Http2.new(host: "http2test.kaspernj.org") do |http|
6
+ res = http.get("content_type_test.php")
7
+ res.content_type.should eq "text/html"
8
+ end
9
+ end
10
+
11
+ it "should register content length" do
12
+ Http2.new(host: "http2test.kaspernj.org") do |http|
13
+ res = http.get("content_type_test.php")
14
+ res.content_length.should > 50
15
+ end
16
+ end
17
+ end
data/spec/http2_spec.rb CHANGED
@@ -1,9 +1,8 @@
1
- require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
1
+ require "spec_helper"
2
+ require "json"
2
3
 
3
4
  describe "Http2" do
4
5
  it "should be able to do normal post-requests." do
5
- require "json"
6
-
7
6
  #Test posting keep-alive and advanced post-data.
8
7
  Http2.new(host: "www.partyworm.dk", debug: false) do |http|
9
8
  0.upto(5) do
@@ -154,4 +153,4 @@ describe "Http2" do
154
153
  resp.code.should eq "200"
155
154
  end
156
155
  end
157
- end
156
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: http2
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.28
4
+ version: 0.0.29
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kasper Johansen
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-12-03 00:00:00.000000000 Z
11
+ date: 2014-12-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: string-cases
@@ -111,6 +111,7 @@ files:
111
111
  - lib/http2.rb
112
112
  - shippable.yml
113
113
  - spec/http2/post_data_generator_spec.rb
114
+ - spec/http2/response_spec.rb
114
115
  - spec/http2/url_builder_spec.rb
115
116
  - spec/http2_spec.rb
116
117
  - spec/spec_helper.rb