Class: ConsoleUtils::RequestUtils::Remo::Curl

Inherits:
Object
  • Object
show all
Defined in:
lib/console_utils/request_utils/remo.rb

Constant Summary collapse

OUT_FORMAT =
"\n%{http_code}\n%{time_total}\n%{size_download}".freeze
KEY_MAP =
{ ">" => :request, "<" => :response }

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(request_method, url) ⇒ Curl

Returns a new instance of Curl


73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/console_utils/request_utils/remo.rb', line 73

def initialize(request_method, url)
  cmd = %W(#{ConsoleUtils.curl_bin} --silent -v --write-out #{OUT_FORMAT} -X #{request_method} #{url})
  puts "# #{cmd.shelljoin.inspect}" unless ConsoleUtils.curl_silence

  @response = {}
  @request  = {}
  @payload  = []

  Open3.popen3(Shellwords.join(cmd)) do |stdin, stdout, stderr, thr|
    # stdin.close
    { stderr: stderr, stdin: stdout }.each do |key, io|
      Thread.new do
        while line = io.gets
          key == :stderr ? process_stderr(line) : @payload << line
        end
      end
    end
    thr.join
  end
end

Instance Attribute Details

#payloadObject (readonly)

Returns the value of attribute payload


71
72
73
# File 'lib/console_utils/request_utils/remo.rb', line 71

def payload
  @payload
end

#requestObject (readonly)

Returns the value of attribute request


71
72
73
# File 'lib/console_utils/request_utils/remo.rb', line 71

def request
  @request
end

#responseObject (readonly)

Returns the value of attribute response


71
72
73
# File 'lib/console_utils/request_utils/remo.rb', line 71

def response
  @response
end

Class Method Details

.call(*args) {|result.to_h, result.payload| ... } ⇒ Object

Yields:


66
67
68
69
# File 'lib/console_utils/request_utils/remo.rb', line 66

def self.call(*args)
  result = new(*args)
  yield(result.to_h, result.payload)
end

Instance Method Details

#process_stderr(line) ⇒ Object


96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/console_utils/request_utils/remo.rb', line 96

def process_stderr(line)
  # warn(line)
  if type = KEY_MAP[line.chr]
    line = line[2, line.size-1].strip

    return if line.size == 0

    case type
    when :request; set_request(line)
    when :response; set_response(line)
    end
  end
end

#set_request(line) ⇒ Object


110
111
112
113
114
115
116
117
118
# File 'lib/console_utils/request_utils/remo.rb', line 110

def set_request(line)
  # warn("Request: #{line}")
  if !@request.key?(:http_version) && line =~ /^(GET|POST|PUT|PATCH|HEAD|OPTION|DELETE) (.+?) HTTP\/(.+)$/
    @request.merge!(method: $1, url: $2, http_version: $3)
  else
    header, value = line.split(": ", 2)
    @request[header] = value
  end
end

#set_response(line) ⇒ Object


120
121
122
123
124
125
126
127
128
# File 'lib/console_utils/request_utils/remo.rb', line 120

def set_response(line)
  # warn("Response: #{line}")
  if !@response.key?(:http_version) && line =~ /^HTTP\/(.+) (\d+?) (.+)$/
    @response.merge!(http_version: $1, http_code: $2, http_status: $3)
  else
    header, value = line.split(": ", 2)
    @response[header] = value
  end
end

#to_hObject


130
131
132
# File 'lib/console_utils/request_utils/remo.rb', line 130

def to_h
  { response: @response, request: @request }
end