cm-httpclient 1.0.0 → 1.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/httpclient.gemspec +3 -3
- data/lib/{httpclient.rb → cm-httpclient.rb} +1 -1
- data/lib/httpclient/httpclient.rb +105 -107
- metadata +2 -2
data/httpclient.gemspec
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
|
3
3
|
Gem::Specification.new do |s|
|
4
4
|
s.name = %q{httpclient}
|
5
|
-
s.version = "1.0.
|
5
|
+
s.version = "1.0.1"
|
6
6
|
|
7
7
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
8
8
|
s.authors = ["pedro gutierrez"]
|
@@ -13,7 +13,7 @@ Gem::Specification.new do |s|
|
|
13
13
|
s.files = %w[
|
14
14
|
README.md
|
15
15
|
httpclient.gemspec
|
16
|
-
lib/httpclient.rb
|
16
|
+
lib/cm-httpclient.rb
|
17
17
|
lib/httpclient/multipart.rb
|
18
18
|
lib/httpclient/httpclient.rb
|
19
19
|
]
|
@@ -24,7 +24,7 @@ Gem::Specification.new do |s|
|
|
24
24
|
s.rdoc_options = ["--inline-source", "--charset=UTF-8"]
|
25
25
|
s.require_paths = ["lib"]
|
26
26
|
s.rubyforge_project = %q{cm-httpclient}
|
27
|
-
s.rubygems_version = %q{1.0.
|
27
|
+
s.rubygems_version = %q{1.0.1}
|
28
28
|
s.summary = %q{a simple httpclient}
|
29
29
|
|
30
30
|
#if s.respond_to? :specification_version then
|
@@ -1,7 +1,6 @@
|
|
1
1
|
require 'net/https'
|
2
2
|
require 'uri'
|
3
3
|
require 'rexml/document'
|
4
|
-
require 'helpers/multipart'
|
5
4
|
|
6
5
|
class Hash
|
7
6
|
def to_s
|
@@ -45,49 +44,48 @@ class String
|
|
45
44
|
|
46
45
|
end
|
47
46
|
|
48
|
-
module
|
47
|
+
module CodeMutiny
|
48
|
+
|
49
|
+
module HttpClient
|
49
50
|
|
50
|
-
|
51
|
-
|
52
|
-
|
51
|
+
def host( host )
|
52
|
+
@@host = host
|
53
|
+
end
|
53
54
|
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
end
|
55
|
+
def connect( &block )
|
56
|
+
uri = URI.parse( @@host )
|
57
|
+
h = Net::HTTP.new( uri.host, uri.port )
|
58
|
+
if( uri.scheme == 'https' )
|
59
|
+
h.use_ssl = uri.scheme
|
60
|
+
h.verify_mode = OpenSSL::SSL::VERIFY_NONE
|
61
|
+
end
|
62
|
+
headers = {}
|
63
|
+
headers.merge!( @auth ) if @auth
|
64
|
+
headers.desym!
|
65
|
+
block.call( h, headers )
|
66
|
+
end
|
67
67
|
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
68
|
+
def debug( &block )
|
69
|
+
@debug = true
|
70
|
+
instance_eval( &block )
|
71
|
+
@debug = false
|
72
|
+
end
|
73
73
|
|
74
|
+
def debug_request( http_method, path, headers, form = nil )
|
75
|
+
if @debug
|
76
|
+
puts ""
|
77
|
+
puts "Request"
|
78
|
+
puts "==========================="
|
79
|
+
puts "method: #{http_method}"
|
80
|
+
puts "path: #{path}"
|
81
|
+
puts "headers: #{headers}"
|
82
|
+
puts "form: #{form}" if form
|
83
|
+
puts ""
|
84
|
+
end
|
85
|
+
end
|
74
86
|
|
75
|
-
|
76
|
-
|
77
|
-
if @debug
|
78
|
-
puts ""
|
79
|
-
puts "Request"
|
80
|
-
puts "==========================="
|
81
|
-
puts "method: #{http_method}"
|
82
|
-
puts "path: #{path}"
|
83
|
-
puts "headers: #{headers}"
|
84
|
-
puts "form: #{form}" if form
|
85
|
-
puts ""
|
86
|
-
end
|
87
|
-
end
|
88
|
-
|
89
|
-
def debug_response( code, headers, body )
|
90
|
-
if @debug
|
87
|
+
def debug_response( code, headers, body )
|
88
|
+
if @debug
|
91
89
|
puts ""
|
92
90
|
puts "Response"
|
93
91
|
puts "==========================="
|
@@ -95,87 +93,87 @@ module HttpClient
|
|
95
93
|
puts "headers: #{headers}"
|
96
94
|
puts "body: #{body}"
|
97
95
|
puts ""
|
98
|
-
|
99
|
-
|
96
|
+
end
|
97
|
+
end
|
100
98
|
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
99
|
+
def retrieve( path, &block )
|
100
|
+
connect do |http, headers|
|
101
|
+
debug_request( :get, path, headers )
|
102
|
+
res = http.get path, headers
|
103
|
+
handle_response( res, block )
|
104
|
+
end
|
105
|
+
end
|
108
106
|
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
107
|
+
def remove( path, &block )
|
108
|
+
connect do |http, headers|
|
109
|
+
debug_request( :delete, path, headers )
|
110
|
+
res = http.delete path, headers
|
111
|
+
handle_response( res, block )
|
112
|
+
end
|
113
|
+
end
|
116
114
|
|
117
115
|
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
116
|
+
def create( path, form = nil, &block )
|
117
|
+
connect do |http, headers|
|
118
|
+
data = nil
|
119
|
+
headers[ 'Content-Length' ] = '0'
|
122
120
|
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
121
|
+
if form
|
122
|
+
data, extra_headers = Multipart::Post.prepare_query( form )
|
123
|
+
headers.merge!( extra_headers )
|
124
|
+
headers[ 'Content-Length' ] = data.length.to_s
|
125
|
+
end
|
128
126
|
|
129
|
-
|
127
|
+
debug_request( :post, path, headers, form )
|
130
128
|
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
129
|
+
res = http.post path, data, headers
|
130
|
+
handle_response( res, block )
|
131
|
+
end
|
132
|
+
end
|
135
133
|
|
136
|
-
|
134
|
+
alias :update :create
|
137
135
|
|
138
136
|
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
end
|
156
|
-
end
|
137
|
+
def handle_response( res, block = nil )
|
138
|
+
code = res.code.to_i
|
139
|
+
headers = res.to_hash
|
140
|
+
body = res.body
|
141
|
+
debug_response( code, headers, body )
|
142
|
+
if res[ 'Content-Type' ] == 'text/xml'
|
143
|
+
body = REXML::Document.new( body )
|
144
|
+
end
|
145
|
+
if block
|
146
|
+
block.call( code, body, headers )
|
147
|
+
else
|
148
|
+
@obtained_code = code
|
149
|
+
@obtained_body = body
|
150
|
+
@obtained_headers = headers
|
151
|
+
end
|
152
|
+
end
|
157
153
|
|
158
|
-
|
154
|
+
attr_reader :obtained_code, :obtained_body, :obtained_headers
|
159
155
|
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
156
|
+
def admin( key, &block )
|
157
|
+
if key == nil
|
158
|
+
anonymous( &block )
|
159
|
+
else
|
160
|
+
@auth = { :cmuser => key }
|
161
|
+
instance_eval( &block )
|
162
|
+
end
|
163
|
+
end
|
168
164
|
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
165
|
+
def user( username, password = nil, &block )
|
166
|
+
@auth = { :cmuser => username }
|
167
|
+
@auth[ :cmpasswd ] = password if password
|
168
|
+
instance_eval( &block )
|
169
|
+
end
|
174
170
|
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
171
|
+
def anonymous( &block )
|
172
|
+
@auth = { :cmuser => 'anonymous' }
|
173
|
+
instance_eval( &block )
|
174
|
+
end
|
179
175
|
|
180
|
-
end
|
181
176
|
|
177
|
+
end
|
178
|
+
|
179
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: cm-httpclient
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- pedro gutierrez
|
@@ -24,7 +24,7 @@ extra_rdoc_files: []
|
|
24
24
|
files:
|
25
25
|
- README.md
|
26
26
|
- httpclient.gemspec
|
27
|
-
- lib/httpclient.rb
|
27
|
+
- lib/cm-httpclient.rb
|
28
28
|
- lib/httpclient/multipart.rb
|
29
29
|
- lib/httpclient/httpclient.rb
|
30
30
|
has_rdoc: true
|