Class: Curl::Spawn::ArgsBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/curl/spawn/args_builder.rb

Instance Method Summary collapse

Constructor Details

#initializeArgsBuilder

Returns a new instance of ArgsBuilder.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/curl/spawn/args_builder.rb', line 8

def initialize
  @user = nil
  @password = nil
  @scheme = 'http'
  @ssl_no_verify = false
  @host = 'localhost'
  @port = 80
  @path = '/'
  @verb = 'GET'
  @queries = {}
  @headers = {}
  @data = nil

  @show_headers = false
  @verbose = false
end

Instance Method Details

#build!Object



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/curl/spawn/args_builder.rb', line 108

def build!
  args = ::Curl::Spawn::Args.new

  args.argv.push('curl')
  args.argv.push('-k') if @ssl_no_verify
  args.argv.push('--user') unless @user.nil?
  args.argv.push("#{@user}#{":#{@password}" unless @password.nil?}")

  query_str = ''
  @queries.each do |k, v|
    if query_str == ''
      query_str << '?'
    else
      query_str << '&'
    end
    query_str << "#{ERB::Util.url_encode(k)}=#{ERB::Util.url_encode(v)}"
  end

  args.argv.push('-X')
  args.argv.push(@verb.upcase)

  args.argv.push("#{@scheme}://#{@host}:#{@port}#{@path}#{query_str}")

  @headers.each do |k, v|
    args.argv.push("-H")
    args.argv.push("'#{k}: #{v}'")
  end

  if @data
    args.argv.push('--data-binary')
    args.argv.push('@-')
    args.opt[:in] = @data
  end

  if @show_headers
    args.argv.push('-i')
  end

  if @verbose
    args.argv.push('-v')
  end

  args.argv = args.argv.reject { |arg| arg.nil? || arg.empty? }.map { |arg| arg.to_s }

  args
end

#data(d) ⇒ Object Also known as: data=, content, content=



90
91
92
# File 'lib/curl/spawn/args_builder.rb', line 90

def data(d)
  @data =d
end

#dump_headers(v = true) ⇒ Object Also known as: dump_headers=, show_headers, show_headers=



97
98
99
# File 'lib/curl/spawn/args_builder.rb', line 97

def dump_headers(v=true)
  @show_headers = v
end

#header(h) ⇒ Object Also known as: headers



85
86
87
# File 'lib/curl/spawn/args_builder.rb', line 85

def header(h)
  @headers.merge!(h)
end

#host(h) ⇒ Object Also known as: host=



59
60
61
# File 'lib/curl/spawn/args_builder.rb', line 59

def host(h)
  @host = h
end

#httpObject



46
47
48
# File 'lib/curl/spawn/args_builder.rb', line 46

def http
  @scheme = 'http'
end

#httpsObject



50
51
52
# File 'lib/curl/spawn/args_builder.rb', line 50

def https
  @scheme = 'https'
end

#password(p) ⇒ Object Also known as: password=



36
37
38
# File 'lib/curl/spawn/args_builder.rb', line 36

def password(p)
  @password = p
end

#path(p) ⇒ Object Also known as: path=



69
70
71
72
# File 'lib/curl/spawn/args_builder.rb', line 69

def path(p)
  p = "/#{p}" unless p.start_with?('/')
  @path = p
end

#port(p) ⇒ Object Also known as: port=



64
65
66
# File 'lib/curl/spawn/args_builder.rb', line 64

def port(p)
  @port = p
end

#query(q) ⇒ Object Also known as: queries



80
81
82
# File 'lib/curl/spawn/args_builder.rb', line 80

def query(q)
  @queries.merge!(q)
end

#scheme(h) ⇒ Object Also known as: scheme=



41
42
43
# File 'lib/curl/spawn/args_builder.rb', line 41

def scheme(h)
  @scheme = h
end

#ssl_no_verify(val = true) ⇒ Object Also known as: ssl_no_verify=



54
55
56
# File 'lib/curl/spawn/args_builder.rb', line 54

def ssl_no_verify(val = true)
  @ssl_no_verify = val
end

#url_encode(str) ⇒ Object



25
26
27
# File 'lib/curl/spawn/args_builder.rb', line 25

def url_encode(str)
  Curl.url_encode(str)
end

#user(user) ⇒ Object Also known as: user=, username, username=



29
30
31
# File 'lib/curl/spawn/args_builder.rb', line 29

def user(user)
  @user = user
end

#verb(v) ⇒ Object Also known as: verb=



75
76
77
# File 'lib/curl/spawn/args_builder.rb', line 75

def verb(v)
  @verb = v
end

#verboseObject



104
105
106
# File 'lib/curl/spawn/args_builder.rb', line 104

def verbose
  @verbose = true
end