Module: CurbFu::Request::Base
Instance Method Summary
collapse
Methods included from Common
#build_url, #timeout, #timeout=
Instance Method Details
#build(url_params, query_params = {}) ⇒ Object
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
# File 'lib/curb-fu/request/base.rb', line 6
def build(url_params, query_params = {})
curb = Curl::Easy.new(build_url(url_params, query_params))
=
unless url_params.is_a?(String)
curb.userpwd = "#{url_params[:username]}:#{url_params[:password]}" if url_params[:username]
if url_params[:authtype]
curb.http_auth_types = url_params[:authtype]
elsif url_params[:username]
curb.http_auth_types = CurbFu::Authentication::BASIC
end
= .merge(url_params[:headers]) unless url_params[:headers].nil?
["Expect"] = '' unless url_params[:headers] && url_params[:headers]["Expect"]
end
curb. =
curb.timeout = @timeout
curb
end
|
#get(url, params = {}) ⇒ Object
41
42
43
44
45
|
# File 'lib/curb-fu/request/base.rb', line 41
def get(url, params = {})
curb = self.build(url, params)
curb.http_get
CurbFu::Response::Base.from_curb_response(curb)
end
|
Headers to be used for every request Returns: hash of header names and values
37
38
39
|
# File 'lib/curb-fu/request/base.rb', line 37
def
@global_headers ||= {}
end
|
Set headers to be used for every request
31
32
33
|
# File 'lib/curb-fu/request/base.rb', line 31
def ()
@global_headers =
end
|
#post(url, params = {}) ⇒ Object
53
54
55
56
57
58
59
60
|
# File 'lib/curb-fu/request/base.rb', line 53
def post(url, params = {})
fields = create_post_fields(params)
fields = [fields] if fields.is_a?(String)
curb = self.build(url)
curb.http_post(*fields)
CurbFu::Response::Base.from_curb_response(curb)
end
|
#post_file(url, params = {}, filez = {}) ⇒ Object
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
|
# File 'lib/curb-fu/request/base.rb', line 62
def post_file(url, params = {}, filez = {})
fields = create_post_fields(params)
fields += create_file_fields(filez)
curb = self.build(url)
curb.multipart_form_post = true
begin
curb.http_post(*fields)
rescue Curl::Err::InvalidPostFieldError => e
field_list = (params.merge(filez)).inject([]) { |list, (name, value)| list << "#{name} => #{value.to_s[0..49].inspect}"; list }
raise e, "There was an attempt to post invalid fields. The fields were:\n#{field_list.join("\n")}"
end
CurbFu::Response::Base.from_curb_response(curb)
end
|
#put(url, params = {}) ⇒ Object
47
48
49
50
51
|
# File 'lib/curb-fu/request/base.rb', line 47
def put(url, params = {})
curb = self.build(url, params)
curb.http_put("")
CurbFu::Response::Base.from_curb_response(curb)
end
|