shl 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/shl.rb +42 -0
- data/lib/shl/request.rb +46 -0
- data/lib/shl/response.rb +23 -0
- metadata +80 -0
data/lib/shl.rb
ADDED
@@ -0,0 +1,42 @@
|
|
1
|
+
require 'uri'
|
2
|
+
require 'socket'
|
3
|
+
|
4
|
+
module SHL
|
5
|
+
HTTP_VERSION = "HTTP/1.1"
|
6
|
+
NEWLINE = "\r\n"
|
7
|
+
|
8
|
+
class OrderedHash < Hash
|
9
|
+
def initialize(initial_values)
|
10
|
+
initial_values.each do |key, value|
|
11
|
+
self[key] = value
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
def []=(key, value)
|
16
|
+
@order ||= []
|
17
|
+
@order << key
|
18
|
+
super
|
19
|
+
end
|
20
|
+
|
21
|
+
def each(&block)
|
22
|
+
@order.each do |key|
|
23
|
+
block.call(key, self[key])
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
module AttributeInitializer
|
29
|
+
def initialize(attributes={})
|
30
|
+
attributes.each do |key, value|
|
31
|
+
send("#{key}=", value)
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
require 'shl/request'
|
38
|
+
require 'shl/response'
|
39
|
+
|
40
|
+
def SHL(attributes={})
|
41
|
+
SHL::Request.new(attributes).run
|
42
|
+
end
|
data/lib/shl/request.rb
ADDED
@@ -0,0 +1,46 @@
|
|
1
|
+
module SHL
|
2
|
+
class Request
|
3
|
+
attr_accessor :verb, :url, :headers, :body
|
4
|
+
|
5
|
+
include AttributeInitializer
|
6
|
+
|
7
|
+
def url=(url)
|
8
|
+
@url = URI.parse(url)
|
9
|
+
end
|
10
|
+
|
11
|
+
def path
|
12
|
+
@url.path == '' ? '/' : @url.path
|
13
|
+
end
|
14
|
+
|
15
|
+
def connection
|
16
|
+
@connection ||= TCPSocket.new(@url.host, @url.port)
|
17
|
+
end
|
18
|
+
|
19
|
+
def request_line
|
20
|
+
"#{verb.to_s.upcase} #{path} #{HTTP_VERSION}"
|
21
|
+
end
|
22
|
+
|
23
|
+
def headers
|
24
|
+
@headers||=OrderedHash.new([
|
25
|
+
['Host',@url.host],
|
26
|
+
['Connection','close']
|
27
|
+
])
|
28
|
+
end
|
29
|
+
|
30
|
+
def serialized_headers
|
31
|
+
headers.map do |key, value|
|
32
|
+
"#{key}: #{value}"
|
33
|
+
end.join(NEWLINE)
|
34
|
+
end
|
35
|
+
|
36
|
+
def serialized_body
|
37
|
+
@body.to_s + NEWLINE
|
38
|
+
end
|
39
|
+
|
40
|
+
def run
|
41
|
+
connection.write([request_line,serialized_headers,serialized_body].join(NEWLINE))
|
42
|
+
connection.flush
|
43
|
+
Response.new(:io=>connection)
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
data/lib/shl/response.rb
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
module SHL
|
2
|
+
class Response
|
3
|
+
BUFFER_LENGTH = 4096
|
4
|
+
|
5
|
+
attr_accessor :io
|
6
|
+
|
7
|
+
include AttributeInitializer
|
8
|
+
|
9
|
+
def parsed
|
10
|
+
if @parsed.nil?
|
11
|
+
buffer = ''
|
12
|
+
while(data = io.read(BUFFER_LENGTH))
|
13
|
+
buffer << data
|
14
|
+
end
|
15
|
+
@parsed = buffer.split(NEWLINE*2)
|
16
|
+
end; @parsed
|
17
|
+
end
|
18
|
+
|
19
|
+
def body
|
20
|
+
parsed[1]
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
metadata
ADDED
@@ -0,0 +1,80 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: shl
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 29
|
5
|
+
prerelease:
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 0
|
9
|
+
- 1
|
10
|
+
version: 0.0.1
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Manfred Stienstra
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2012-04-18 00:00:00 Z
|
19
|
+
dependencies:
|
20
|
+
- !ruby/object:Gem::Dependency
|
21
|
+
name: rake
|
22
|
+
prerelease: false
|
23
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
24
|
+
none: false
|
25
|
+
requirements:
|
26
|
+
- - ">="
|
27
|
+
- !ruby/object:Gem::Version
|
28
|
+
hash: 3
|
29
|
+
segments:
|
30
|
+
- 0
|
31
|
+
version: "0"
|
32
|
+
type: :development
|
33
|
+
version_requirements: *id001
|
34
|
+
description: " SHL is a really simple HTTP library.\n"
|
35
|
+
email: manfred@fngtps.com
|
36
|
+
executables: []
|
37
|
+
|
38
|
+
extensions: []
|
39
|
+
|
40
|
+
extra_rdoc_files: []
|
41
|
+
|
42
|
+
files:
|
43
|
+
- lib/shl.rb
|
44
|
+
- lib/shl/request.rb
|
45
|
+
- lib/shl/response.rb
|
46
|
+
homepage:
|
47
|
+
licenses: []
|
48
|
+
|
49
|
+
post_install_message:
|
50
|
+
rdoc_options:
|
51
|
+
- --charset=utf-8
|
52
|
+
require_paths:
|
53
|
+
- lib
|
54
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
55
|
+
none: false
|
56
|
+
requirements:
|
57
|
+
- - ">="
|
58
|
+
- !ruby/object:Gem::Version
|
59
|
+
hash: 3
|
60
|
+
segments:
|
61
|
+
- 0
|
62
|
+
version: "0"
|
63
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
64
|
+
none: false
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
hash: 3
|
69
|
+
segments:
|
70
|
+
- 0
|
71
|
+
version: "0"
|
72
|
+
requirements: []
|
73
|
+
|
74
|
+
rubyforge_project:
|
75
|
+
rubygems_version: 1.8.15
|
76
|
+
signing_key:
|
77
|
+
specification_version: 3
|
78
|
+
summary: SHL is a really simple HTTP library.
|
79
|
+
test_files: []
|
80
|
+
|