lib/herdic/client.rb in herdic-1.0.2 vs lib/herdic/client.rb in herdic-1.0.3

- old
+ new

@@ -40,11 +40,11 @@ end def run_all if @options['e'] FileUtils.cp @file, Herdic.edit_request_file - system "$EDITOR '%s'" % Herdic.edit_request_file + system "%s '%s'" % [Herdic.editor, Herdic.edit_request_file] @specs = Loader.new Herdic.edit_request_file, @file else @specs = Loader.new @file end @@ -62,31 +62,38 @@ end private def run(spec) setup_spec spec + @printer.title @meta + @printer.request @header, @body + + response = get_response + + body = response.body + + if 'application/json' == response.content_type + body = JSON.parse body + register response, body + end + + @printer.response response, body + end + + private def get_response uri = URI.parse @meta['endpoint'] http = Net::HTTP.new uri.host, uri.port http.use_ssl = @options['use-ssl'] - response = http.start do + http.start do case @meta['method'] when 'GET', 'POST', 'PATCH', 'PUT', 'DELETE' http.send_request @meta['method'], uri.request_uri, @body.to_query, @header else raise "Unsupported method: #{@meta['method']}" end end - - body = response.body - body = JSON.parse body if 'application/json' == response.content_type - - @printer.title @meta - @printer.request @header, @body - @printer.response response, body - - register response, body end private def setup_spec(spec) @meta = spec.slice 'title', 'method', 'endpoint' @register = spec['register'] || {} @@ -96,11 +103,9 @@ @meta['method'].upcase! @header = @header.map { |k, v| [k, v.to_s] }.to_h end private def register(response, body) - return unless 'application/json' == response.content_type - @register.each do |name, path| next if path.empty? val = body