nice_http 1.7.3 → 1.7.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +607 -607
- data/lib/nice_http.rb +428 -414
- data/lib/nice_http/http_methods.rb +526 -526
- data/lib/nice_http/manage_request.rb +237 -237
- data/lib/nice_http/manage_response.rb +279 -280
- data/lib/nice_http/utils.rb +109 -109
- metadata +4 -4
| @@ -1,237 +1,237 @@ | |
| 1 | 
            -
            module NiceHttpManageRequest
         | 
| 2 | 
            -
             | 
| 3 | 
            -
              ######################################################
         | 
| 4 | 
            -
              # private method to manage Request
         | 
| 5 | 
            -
              #   input:
         | 
| 6 | 
            -
              #     3 args: path, data, headers
         | 
| 7 | 
            -
              #     1 arg:  Hash containg at least keys :path and :data
         | 
| 8 | 
            -
              #             In case :data not supplied and :data_examples array supplied, it will be taken the first example as :data.
         | 
| 9 | 
            -
              #   output:
         | 
| 10 | 
            -
              #     path, data, headers
         | 
| 11 | 
            -
              ######################################################
         | 
| 12 | 
            -
              def manage_request(*arguments)
         | 
| 13 | 
            -
                require "json"
         | 
| 14 | 
            -
             | 
| 15 | 
            -
                @prev_request = Hash.new() if @prev_request.nil?
         | 
| 16 | 
            -
                begin
         | 
| 17 | 
            -
                  content_type_included = false
         | 
| 18 | 
            -
                  path = ""
         | 
| 19 | 
            -
                  data = ""
         | 
| 20 | 
            -
             | 
| 21 | 
            -
                  @response = Hash.new()
         | 
| 22 | 
            -
                  headers_t = @headers.dup()
         | 
| 23 | 
            -
                  cookies_to_set_str = ""
         | 
| 24 | 
            -
                  if arguments.size == 3
         | 
| 25 | 
            -
                    path = arguments[0]
         | 
| 26 | 
            -
                  elsif arguments.size == 1 and arguments[0].kind_of?(Hash)
         | 
| 27 | 
            -
                    path = arguments[0][:path]
         | 
| 28 | 
            -
                  elsif arguments.size == 1 and arguments[0].kind_of?(String)
         | 
| 29 | 
            -
                    path = arguments[0].to_s()
         | 
| 30 | 
            -
                  end
         | 
| 31 | 
            -
                  path = (@prepath + path).gsub("//", "/") unless path.nil? or path.start_with?("http:") or path.start_with?("https:")
         | 
| 32 | 
            -
                  @cookies.each { |cookie_path, cookies_hash|
         | 
| 33 | 
            -
                    cookie_path = "" if cookie_path == "/"
         | 
| 34 | 
            -
                    path_to_check = path
         | 
| 35 | 
            -
                    if path == "/" or path[-1] != "/"
         | 
| 36 | 
            -
                      path_to_check += "/"
         | 
| 37 | 
            -
                    end
         | 
| 38 | 
            -
                    if path_to_check.scan(/^#{cookie_path}\//).size > 0
         | 
| 39 | 
            -
                      cookies_hash.each { |key, value|
         | 
| 40 | 
            -
                        cookies_to_set_str += "#{key}=#{value}; "
         | 
| 41 | 
            -
                      }
         | 
| 42 | 
            -
                    end
         | 
| 43 | 
            -
                  }
         | 
| 44 | 
            -
                  headers_t["Cookie"] = cookies_to_set_str
         | 
| 45 | 
            -
             | 
| 46 | 
            -
                  method_s = caller[0].to_s().scan(/:in `(.*)'/).join
         | 
| 47 | 
            -
             | 
| 48 | 
            -
                  if arguments.size == 3
         | 
| 49 | 
            -
                    data = arguments[1]
         | 
| 50 | 
            -
                    if arguments[2].kind_of?(Hash)
         | 
| 51 | 
            -
                      headers_t.merge!(arguments[2])
         | 
| 52 | 
            -
                    end
         | 
| 53 | 
            -
                  elsif arguments.size == 1 and arguments[0].kind_of?(Hash)
         | 
| 54 | 
            -
                    if arguments[0][:data].nil?
         | 
| 55 | 
            -
                      if arguments[0].keys.include?(:data)
         | 
| 56 | 
            -
                        data = ""
         | 
| 57 | 
            -
                      elsif arguments[0].keys.include?(:data_examples) and
         | 
| 58 | 
            -
                            arguments[0][:data_examples].kind_of?(Array)
         | 
| 59 | 
            -
                        data = arguments[0][:data_examples][0] #the first example by default
         | 
| 60 | 
            -
                      else
         | 
| 61 | 
            -
                        data = ""
         | 
| 62 | 
            -
                      end
         | 
| 63 | 
            -
                    else
         | 
| 64 | 
            -
                      data = arguments[0][:data]
         | 
| 65 | 
            -
                    end
         | 
| 66 | 
            -
                    if arguments[0].include?(:headers)
         | 
| 67 | 
            -
                      headers_t.merge!(arguments[0][:headers])
         | 
| 68 | 
            -
                    end
         | 
| 69 | 
            -
             | 
| 70 | 
            -
                    if headers_t["Content-Type"].to_s() == "" and headers_t["content-type"].to_s() == "" and
         | 
| 71 | 
            -
                       headers_t[:"content-type"].to_s() == "" and headers_t[:"Content-Type"].to_s() == ""
         | 
| 72 | 
            -
                      content_type_included = false
         | 
| 73 | 
            -
                    elsif headers_t["content-type"].to_s() != ""
         | 
| 74 | 
            -
                      content_type_included = true
         | 
| 75 | 
            -
                      headers_t["Content-Type"] = headers_t["content-type"]
         | 
| 76 | 
            -
                    elsif headers_t[:"content-type"].to_s() != ""
         | 
| 77 | 
            -
                      content_type_included = true
         | 
| 78 | 
            -
                      headers_t["Content-Type"] = headers_t[:"content-type"]
         | 
| 79 | 
            -
                      headers_t.delete(:"content-type")
         | 
| 80 | 
            -
                    elsif headers_t[:"Content-Type"].to_s() != ""
         | 
| 81 | 
            -
                      content_type_included = true
         | 
| 82 | 
            -
                      headers_t["Content-Type"] = headers_t[:"Content-Type"]
         | 
| 83 | 
            -
                      headers_t.delete(:"Content-Type")
         | 
| 84 | 
            -
                    elsif headers_t["Content-Type"].to_s() != ""
         | 
| 85 | 
            -
                      content_type_included = true
         | 
| 86 | 
            -
                    end
         | 
| 87 | 
            -
                    if !content_type_included and data.kind_of?(Hash)
         | 
| 88 | 
            -
                      headers_t["Content-Type"] = "application/json"
         | 
| 89 | 
            -
                      content_type_included = true
         | 
| 90 | 
            -
                    end
         | 
| 91 | 
            -
                    # to be backwards compatible since before was :values
         | 
| 92 | 
            -
                    if arguments[0].include?(:values) and !arguments[0].include?(:values_for)
         | 
| 93 | 
            -
                      arguments[0][:values_for] = arguments[0][:values]
         | 
| 94 | 
            -
                    end
         | 
| 95 | 
            -
             | 
| 96 | 
            -
                    if @values_for.size > 0
         | 
| 97 | 
            -
                      if arguments[0][:values_for].nil?
         | 
| 98 | 
            -
                        arguments[0][:values_for] = @values_for.dup
         | 
| 99 | 
            -
                      else
         | 
| 100 | 
            -
                        arguments[0][:values_for] = @values_for.merge(arguments[0][:values_for])
         | 
| 101 | 
            -
                      end
         | 
| 102 | 
            -
                    end
         | 
| 103 | 
            -
             | 
| 104 | 
            -
                    if content_type_included and (!headers_t["Content-Type"][/text\/xml/].nil? or
         | 
| 105 | 
            -
                                                  !headers_t["Content-Type"]["application/soap+xml"].nil? or
         | 
| 106 | 
            -
                                                  !headers_t["Content-Type"][/application\/jxml/].nil?)
         | 
| 107 | 
            -
                      if arguments[0].include?(:values_for)
         | 
| 108 | 
            -
                        arguments[0][:values_for].each { |key, value|
         | 
| 109 | 
            -
                          data = NiceHttpUtils.set_value_xml_tag(key.to_s(), data, value.to_s(), true)
         | 
| 110 | 
            -
                        }
         | 
| 111 | 
            -
                      end
         | 
| 112 | 
            -
                    elsif content_type_included and !headers_t["Content-Type"][/application\/json/].nil? and data.to_s() != ""
         | 
| 113 | 
            -
                      require "json"
         | 
| 114 | 
            -
                      if data.kind_of?(String)
         | 
| 115 | 
            -
                        if arguments[0].include?(:values_for)
         | 
| 116 | 
            -
                          arguments[0][:values_for].each { |key, value|
         | 
| 117 | 
            -
                            data.gsub!(/"(#{key})":\s*"([^"]*)"/, '"\1": "' + value + '"')  # "key":"value"
         | 
| 118 | 
            -
                            data.gsub!(/(#{key}):\s*"([^"]*)"/, '\1: "' + value + '"')  # key:"value"
         | 
| 119 | 
            -
                            data.gsub!(/(#{key}):\s*'([^']*)'/, '\1: \'' + value + "'")  # key:'value'
         | 
| 120 | 
            -
                            data.gsub!(/"(#{key})":\s*(\w+)/, '"\1": ' + value)  # "key":456
         | 
| 121 | 
            -
                            data.gsub!(/(#{key}):\s*(\w+)/, '\1: ' + value)  # key:456
         | 
| 122 | 
            -
                          }
         | 
| 123 | 
            -
                        end
         | 
| 124 | 
            -
                      elsif data.kind_of?(Hash)
         | 
| 125 | 
            -
                        if arguments[0].include?(:values_for)
         | 
| 126 | 
            -
                          data = data.set_values(arguments[0][:values_for])
         | 
| 127 | 
            -
                        end
         | 
| 128 | 
            -
                        data = data.to_json()
         | 
| 129 | 
            -
                      elsif data.kind_of?(Array)
         | 
| 130 | 
            -
                        data_arr = Array.new()
         | 
| 131 | 
            -
                        data.each_with_index { |row, indx|
         | 
| 132 | 
            -
                          unless row.kind_of?(Hash)
         | 
| 133 | 
            -
                            @logger.fatal("Wrong format on request application/json, be sure is a Hash, Array of Hashes or JSON string")
         | 
| 134 | 
            -
                            return :error, :error, :error
         | 
| 135 | 
            -
                          end
         | 
| 136 | 
            -
                          if arguments[0].include?(:values_for)
         | 
| 137 | 
            -
                            if arguments[0][:values_for].is_a?(Array)
         | 
| 138 | 
            -
                              data_n = row.set_values(arguments[0][:values_for][indx])
         | 
| 139 | 
            -
                            else
         | 
| 140 | 
            -
                              data_n = row.set_values(arguments[0][:values_for])
         | 
| 141 | 
            -
                            end
         | 
| 142 | 
            -
                          else
         | 
| 143 | 
            -
                            data_n = row
         | 
| 144 | 
            -
                          end
         | 
| 145 | 
            -
                          data_arr.push(data_n)
         | 
| 146 | 
            -
                        }
         | 
| 147 | 
            -
                        data = data_arr.to_json()
         | 
| 148 | 
            -
                      else
         | 
| 149 | 
            -
                        @logger.fatal("Wrong format on request application/json, be sure is a Hash, Array of Hashes or JSON string")
         | 
| 150 | 
            -
                        return :error, :error, :error
         | 
| 151 | 
            -
                      end
         | 
| 152 | 
            -
                    elsif content_type_included and arguments[0].include?(:values_for)
         | 
| 153 | 
            -
                      if arguments[0][:values_for].kind_of?(Hash) and arguments[0][:values_for].keys.size > 0
         | 
| 154 | 
            -
                        if !headers_t.nil? and headers_t.kind_of?(Hash) and headers_t["Content-Type"] != "application/x-www-form-urlencoded" and headers_t["content-type"] != "application/x-www-form-urlencoded"
         | 
| 155 | 
            -
                          @logger.warn(":values_for key given without a valid content-type or data for request. No values modified on the request")
         | 
| 156 | 
            -
                        end
         | 
| 157 | 
            -
                      end
         | 
| 158 | 
            -
                    end
         | 
| 159 | 
            -
                  elsif arguments.size == 1 and arguments[0].kind_of?(String)
         | 
| 160 | 
            -
                    #path=arguments[0].to_s()
         | 
| 161 | 
            -
                    data = ""
         | 
| 162 | 
            -
                  else
         | 
| 163 | 
            -
                    @logger.fatal("Invalid number of arguments or wrong arguments in #{method_s}")
         | 
| 164 | 
            -
                    return :error, :error, :error
         | 
| 165 | 
            -
                  end
         | 
| 166 | 
            -
                  if headers_t.keys.include?("Content-Type") and !headers_t["Content-Type"]["multipart/form-data"].nil? and headers_t["Content-Type"] != ["multipart/form-data"] #only for the case raw multipart request
         | 
| 167 | 
            -
                    encoding = "UTF-8"
         | 
| 168 | 
            -
                    data_s = ""
         | 
| 169 | 
            -
                  else
         | 
| 170 | 
            -
                    encoding = data.to_s().scan(/encoding='(.*)'/i).join
         | 
| 171 | 
            -
                    if encoding.to_s() == ""
         | 
| 172 | 
            -
                      encoding = data.to_s().scan(/charset='(.*)'/i).join
         | 
| 173 | 
            -
                    end
         | 
| 174 | 
            -
                    if encoding.to_s() == "" and headers_t.include?("Content-Type")
         | 
| 175 | 
            -
                      encoding = headers_t["Content-Type"].scan(/charset='?(.*)'?/i).join
         | 
| 176 | 
            -
                      if encoding.to_s() == ""
         | 
| 177 | 
            -
                        encoding = headers_t["Content-Type"].scan(/encoding='?(.*)'?/i).join
         | 
| 178 | 
            -
                      end
         | 
| 179 | 
            -
                    end
         | 
| 180 | 
            -
             | 
| 181 | 
            -
                    begin
         | 
| 182 | 
            -
                      data_s = JSON.pretty_generate(JSON.parse(data))
         | 
| 183 | 
            -
                    rescue
         | 
| 184 | 
            -
                      data_s = data
         | 
| 185 | 
            -
                    end
         | 
| 186 | 
            -
                    data_s = data_s.to_s().gsub("<", "<")
         | 
| 187 | 
            -
                  end
         | 
| 188 | 
            -
                  if headers_t.keys.include?("Accept-Encoding")
         | 
| 189 | 
            -
                    headers_t["Accept-Encoding"].gsub!("gzip", "") #removed so the response is in plain text
         | 
| 190 | 
            -
                  end
         | 
| 191 | 
            -
             | 
| 192 | 
            -
                  headers_ts = ""
         | 
| 193 | 
            -
                  headers_t.each { |key, val| headers_ts += key.to_s + ":" + val.to_s() + ", " }
         | 
| 194 | 
            -
                  message = "\n\n#{"- " * 25}\n"
         | 
| 195 | 
            -
                  if arguments.size == 1 and arguments[0].kind_of?(Hash) and arguments[0].key?(:name)
         | 
| 196 | 
            -
                    message += "#{method_s.upcase} Request: #{arguments[0][:name]}"
         | 
| 197 | 
            -
                  else
         | 
| 198 | 
            -
                    message += "#{method_s.upcase} Request"
         | 
| 199 | 
            -
                  end
         | 
| 200 | 
            -
                  message += "\n path: " + path.to_s() + "\n"
         | 
| 201 | 
            -
                  if @debug or @prev_request[:path] != path or @prev_request[:headers] != headers_t or @prev_request[:data] != data
         | 
| 202 | 
            -
                    message += " headers: {" + headers_ts.to_s() + "}\n"
         | 
| 203 | 
            -
                    message += " data: " + data_s.to_s() + "\n"
         | 
| 204 | 
            -
                    message = @message_server + "\n" + message
         | 
| 205 | 
            -
                  else
         | 
| 206 | 
            -
                    message += " Same#{" headers" if headers_t != {}}#{" and" if headers_t != {} and data.to_s != ""}#{" data" if data.to_s != ""} as in the previous request."
         | 
| 207 | 
            -
                  end
         | 
| 208 | 
            -
                  if path.to_s().scan(/^https?:\/\//).size > 0 and path.to_s().scan(/^https?:\/\/#{@host}/).size == 0
         | 
| 209 | 
            -
                    # the path is for another server than the current
         | 
| 210 | 
            -
                    # todo: identify if it is better to log the request, or if it is done later
         | 
| 211 | 
            -
                  else
         | 
| 212 | 
            -
                    self.class.last_request = message
         | 
| 213 | 
            -
                    @logger.info(message)
         | 
| 214 | 
            -
                  end
         | 
| 215 | 
            -
             | 
| 216 | 
            -
                  if data.to_s() != "" and encoding.to_s().upcase != "UTF-8" and encoding != ""
         | 
| 217 | 
            -
                    data = data.to_s().encode(encoding, "UTF-8")
         | 
| 218 | 
            -
                  end
         | 
| 219 | 
            -
                  headers_t.each do |k,v|
         | 
| 220 | 
            -
                    # for lambdas
         | 
| 221 | 
            -
                    headers_t[k] = v.call if v.is_a?(Proc)
         | 
| 222 | 
            -
                  end
         | 
| 223 | 
            -
                  @prev_request[:path] = path
         | 
| 224 | 
            -
                  @prev_request[:data] = data
         | 
| 225 | 
            -
                  @prev_request[:headers] = headers_t
         | 
| 226 | 
            -
                  @prev_request[:method] = method_s.upcase
         | 
| 227 | 
            -
                  if arguments.size == 1 and arguments[0].kind_of?(Hash) and arguments[0].key?(:name)
         | 
| 228 | 
            -
                    @prev_request[:name] = arguments[0][:name]
         | 
| 229 | 
            -
                  end
         | 
| 230 | 
            -
                  return path, data, headers_t
         | 
| 231 | 
            -
                rescue Exception => stack
         | 
| 232 | 
            -
                  @logger.fatal(stack)
         | 
| 233 | 
            -
                  @logger.fatal("manage_request Error on method #{method_s} . path:#{path.to_s()}. data:#{data.to_s()}. headers:#{headers_t.to_s()}")
         | 
| 234 | 
            -
                  return :error
         | 
| 235 | 
            -
                end
         | 
| 236 | 
            -
              end
         | 
| 237 | 
            -
            end
         | 
| 1 | 
            +
            module NiceHttpManageRequest
         | 
| 2 | 
            +
             | 
| 3 | 
            +
              ######################################################
         | 
| 4 | 
            +
              # private method to manage Request
         | 
| 5 | 
            +
              #   input:
         | 
| 6 | 
            +
              #     3 args: path, data, headers
         | 
| 7 | 
            +
              #     1 arg:  Hash containg at least keys :path and :data
         | 
| 8 | 
            +
              #             In case :data not supplied and :data_examples array supplied, it will be taken the first example as :data.
         | 
| 9 | 
            +
              #   output:
         | 
| 10 | 
            +
              #     path, data, headers
         | 
| 11 | 
            +
              ######################################################
         | 
| 12 | 
            +
              def manage_request(*arguments)
         | 
| 13 | 
            +
                require "json"
         | 
| 14 | 
            +
             | 
| 15 | 
            +
                @prev_request = Hash.new() if @prev_request.nil?
         | 
| 16 | 
            +
                begin
         | 
| 17 | 
            +
                  content_type_included = false
         | 
| 18 | 
            +
                  path = ""
         | 
| 19 | 
            +
                  data = ""
         | 
| 20 | 
            +
             | 
| 21 | 
            +
                  @response = Hash.new()
         | 
| 22 | 
            +
                  headers_t = @headers.dup()
         | 
| 23 | 
            +
                  cookies_to_set_str = ""
         | 
| 24 | 
            +
                  if arguments.size == 3
         | 
| 25 | 
            +
                    path = arguments[0]
         | 
| 26 | 
            +
                  elsif arguments.size == 1 and arguments[0].kind_of?(Hash)
         | 
| 27 | 
            +
                    path = arguments[0][:path]
         | 
| 28 | 
            +
                  elsif arguments.size == 1 and arguments[0].kind_of?(String)
         | 
| 29 | 
            +
                    path = arguments[0].to_s()
         | 
| 30 | 
            +
                  end
         | 
| 31 | 
            +
                  path = (@prepath + path).gsub("//", "/") unless path.nil? or path.start_with?("http:") or path.start_with?("https:")
         | 
| 32 | 
            +
                  @cookies.each { |cookie_path, cookies_hash|
         | 
| 33 | 
            +
                    cookie_path = "" if cookie_path == "/"
         | 
| 34 | 
            +
                    path_to_check = path
         | 
| 35 | 
            +
                    if path == "/" or path[-1] != "/"
         | 
| 36 | 
            +
                      path_to_check += "/"
         | 
| 37 | 
            +
                    end
         | 
| 38 | 
            +
                    if path_to_check.scan(/^#{cookie_path}\//).size > 0
         | 
| 39 | 
            +
                      cookies_hash.each { |key, value|
         | 
| 40 | 
            +
                        cookies_to_set_str += "#{key}=#{value}; "
         | 
| 41 | 
            +
                      }
         | 
| 42 | 
            +
                    end
         | 
| 43 | 
            +
                  }
         | 
| 44 | 
            +
                  headers_t["Cookie"] = cookies_to_set_str
         | 
| 45 | 
            +
             | 
| 46 | 
            +
                  method_s = caller[0].to_s().scan(/:in `(.*)'/).join
         | 
| 47 | 
            +
             | 
| 48 | 
            +
                  if arguments.size == 3
         | 
| 49 | 
            +
                    data = arguments[1]
         | 
| 50 | 
            +
                    if arguments[2].kind_of?(Hash)
         | 
| 51 | 
            +
                      headers_t.merge!(arguments[2])
         | 
| 52 | 
            +
                    end
         | 
| 53 | 
            +
                  elsif arguments.size == 1 and arguments[0].kind_of?(Hash)
         | 
| 54 | 
            +
                    if arguments[0][:data].nil?
         | 
| 55 | 
            +
                      if arguments[0].keys.include?(:data)
         | 
| 56 | 
            +
                        data = ""
         | 
| 57 | 
            +
                      elsif arguments[0].keys.include?(:data_examples) and
         | 
| 58 | 
            +
                            arguments[0][:data_examples].kind_of?(Array)
         | 
| 59 | 
            +
                        data = arguments[0][:data_examples][0] #the first example by default
         | 
| 60 | 
            +
                      else
         | 
| 61 | 
            +
                        data = ""
         | 
| 62 | 
            +
                      end
         | 
| 63 | 
            +
                    else
         | 
| 64 | 
            +
                      data = arguments[0][:data]
         | 
| 65 | 
            +
                    end
         | 
| 66 | 
            +
                    if arguments[0].include?(:headers)
         | 
| 67 | 
            +
                      headers_t.merge!(arguments[0][:headers])
         | 
| 68 | 
            +
                    end
         | 
| 69 | 
            +
             | 
| 70 | 
            +
                    if headers_t["Content-Type"].to_s() == "" and headers_t["content-type"].to_s() == "" and
         | 
| 71 | 
            +
                       headers_t[:"content-type"].to_s() == "" and headers_t[:"Content-Type"].to_s() == ""
         | 
| 72 | 
            +
                      content_type_included = false
         | 
| 73 | 
            +
                    elsif headers_t["content-type"].to_s() != ""
         | 
| 74 | 
            +
                      content_type_included = true
         | 
| 75 | 
            +
                      headers_t["Content-Type"] = headers_t["content-type"]
         | 
| 76 | 
            +
                    elsif headers_t[:"content-type"].to_s() != ""
         | 
| 77 | 
            +
                      content_type_included = true
         | 
| 78 | 
            +
                      headers_t["Content-Type"] = headers_t[:"content-type"]
         | 
| 79 | 
            +
                      headers_t.delete(:"content-type")
         | 
| 80 | 
            +
                    elsif headers_t[:"Content-Type"].to_s() != ""
         | 
| 81 | 
            +
                      content_type_included = true
         | 
| 82 | 
            +
                      headers_t["Content-Type"] = headers_t[:"Content-Type"]
         | 
| 83 | 
            +
                      headers_t.delete(:"Content-Type")
         | 
| 84 | 
            +
                    elsif headers_t["Content-Type"].to_s() != ""
         | 
| 85 | 
            +
                      content_type_included = true
         | 
| 86 | 
            +
                    end
         | 
| 87 | 
            +
                    if !content_type_included and data.kind_of?(Hash)
         | 
| 88 | 
            +
                      headers_t["Content-Type"] = "application/json"
         | 
| 89 | 
            +
                      content_type_included = true
         | 
| 90 | 
            +
                    end
         | 
| 91 | 
            +
                    # to be backwards compatible since before was :values
         | 
| 92 | 
            +
                    if arguments[0].include?(:values) and !arguments[0].include?(:values_for)
         | 
| 93 | 
            +
                      arguments[0][:values_for] = arguments[0][:values]
         | 
| 94 | 
            +
                    end
         | 
| 95 | 
            +
             | 
| 96 | 
            +
                    if @values_for.size > 0
         | 
| 97 | 
            +
                      if arguments[0][:values_for].nil?
         | 
| 98 | 
            +
                        arguments[0][:values_for] = @values_for.dup
         | 
| 99 | 
            +
                      else
         | 
| 100 | 
            +
                        arguments[0][:values_for] = @values_for.merge(arguments[0][:values_for])
         | 
| 101 | 
            +
                      end
         | 
| 102 | 
            +
                    end
         | 
| 103 | 
            +
             | 
| 104 | 
            +
                    if content_type_included and (!headers_t["Content-Type"][/text\/xml/].nil? or
         | 
| 105 | 
            +
                                                  !headers_t["Content-Type"]["application/soap+xml"].nil? or
         | 
| 106 | 
            +
                                                  !headers_t["Content-Type"][/application\/jxml/].nil?)
         | 
| 107 | 
            +
                      if arguments[0].include?(:values_for)
         | 
| 108 | 
            +
                        arguments[0][:values_for].each { |key, value|
         | 
| 109 | 
            +
                          data = NiceHttpUtils.set_value_xml_tag(key.to_s(), data, value.to_s(), true)
         | 
| 110 | 
            +
                        }
         | 
| 111 | 
            +
                      end
         | 
| 112 | 
            +
                    elsif content_type_included and !headers_t["Content-Type"][/application\/json/].nil? and data.to_s() != ""
         | 
| 113 | 
            +
                      require "json"
         | 
| 114 | 
            +
                      if data.kind_of?(String)
         | 
| 115 | 
            +
                        if arguments[0].include?(:values_for)
         | 
| 116 | 
            +
                          arguments[0][:values_for].each { |key, value|
         | 
| 117 | 
            +
                            data.gsub!(/"(#{key})":\s*"([^"]*)"/, '"\1": "' + value + '"')  # "key":"value"
         | 
| 118 | 
            +
                            data.gsub!(/(#{key}):\s*"([^"]*)"/, '\1: "' + value + '"')  # key:"value"
         | 
| 119 | 
            +
                            data.gsub!(/(#{key}):\s*'([^']*)'/, '\1: \'' + value + "'")  # key:'value'
         | 
| 120 | 
            +
                            data.gsub!(/"(#{key})":\s*(\w+)/, '"\1": ' + value)  # "key":456
         | 
| 121 | 
            +
                            data.gsub!(/(#{key}):\s*(\w+)/, '\1: ' + value)  # key:456
         | 
| 122 | 
            +
                          }
         | 
| 123 | 
            +
                        end
         | 
| 124 | 
            +
                      elsif data.kind_of?(Hash)
         | 
| 125 | 
            +
                        if arguments[0].include?(:values_for)
         | 
| 126 | 
            +
                          data = data.set_values(arguments[0][:values_for])
         | 
| 127 | 
            +
                        end
         | 
| 128 | 
            +
                        data = data.to_json()
         | 
| 129 | 
            +
                      elsif data.kind_of?(Array)
         | 
| 130 | 
            +
                        data_arr = Array.new()
         | 
| 131 | 
            +
                        data.each_with_index { |row, indx|
         | 
| 132 | 
            +
                          unless row.kind_of?(Hash)
         | 
| 133 | 
            +
                            @logger.fatal("Wrong format on request application/json, be sure is a Hash, Array of Hashes or JSON string")
         | 
| 134 | 
            +
                            return :error, :error, :error
         | 
| 135 | 
            +
                          end
         | 
| 136 | 
            +
                          if arguments[0].include?(:values_for)
         | 
| 137 | 
            +
                            if arguments[0][:values_for].is_a?(Array)
         | 
| 138 | 
            +
                              data_n = row.set_values(arguments[0][:values_for][indx])
         | 
| 139 | 
            +
                            else
         | 
| 140 | 
            +
                              data_n = row.set_values(arguments[0][:values_for])
         | 
| 141 | 
            +
                            end
         | 
| 142 | 
            +
                          else
         | 
| 143 | 
            +
                            data_n = row
         | 
| 144 | 
            +
                          end
         | 
| 145 | 
            +
                          data_arr.push(data_n)
         | 
| 146 | 
            +
                        }
         | 
| 147 | 
            +
                        data = data_arr.to_json()
         | 
| 148 | 
            +
                      else
         | 
| 149 | 
            +
                        @logger.fatal("Wrong format on request application/json, be sure is a Hash, Array of Hashes or JSON string")
         | 
| 150 | 
            +
                        return :error, :error, :error
         | 
| 151 | 
            +
                      end
         | 
| 152 | 
            +
                    elsif content_type_included and arguments[0].include?(:values_for)
         | 
| 153 | 
            +
                      if arguments[0][:values_for].kind_of?(Hash) and arguments[0][:values_for].keys.size > 0
         | 
| 154 | 
            +
                        if !headers_t.nil? and headers_t.kind_of?(Hash) and headers_t["Content-Type"] != "application/x-www-form-urlencoded" and headers_t["content-type"] != "application/x-www-form-urlencoded"
         | 
| 155 | 
            +
                          @logger.warn(":values_for key given without a valid content-type or data for request. No values modified on the request")
         | 
| 156 | 
            +
                        end
         | 
| 157 | 
            +
                      end
         | 
| 158 | 
            +
                    end
         | 
| 159 | 
            +
                  elsif arguments.size == 1 and arguments[0].kind_of?(String)
         | 
| 160 | 
            +
                    #path=arguments[0].to_s()
         | 
| 161 | 
            +
                    data = ""
         | 
| 162 | 
            +
                  else
         | 
| 163 | 
            +
                    @logger.fatal("Invalid number of arguments or wrong arguments in #{method_s}")
         | 
| 164 | 
            +
                    return :error, :error, :error
         | 
| 165 | 
            +
                  end
         | 
| 166 | 
            +
                  if headers_t.keys.include?("Content-Type") and !headers_t["Content-Type"]["multipart/form-data"].nil? and headers_t["Content-Type"] != ["multipart/form-data"] #only for the case raw multipart request
         | 
| 167 | 
            +
                    encoding = "UTF-8"
         | 
| 168 | 
            +
                    data_s = ""
         | 
| 169 | 
            +
                  else
         | 
| 170 | 
            +
                    encoding = data.to_s().scan(/encoding='(.*)'/i).join
         | 
| 171 | 
            +
                    if encoding.to_s() == ""
         | 
| 172 | 
            +
                      encoding = data.to_s().scan(/charset='(.*)'/i).join
         | 
| 173 | 
            +
                    end
         | 
| 174 | 
            +
                    if encoding.to_s() == "" and headers_t.include?("Content-Type")
         | 
| 175 | 
            +
                      encoding = headers_t["Content-Type"].scan(/charset='?(.*)'?/i).join
         | 
| 176 | 
            +
                      if encoding.to_s() == ""
         | 
| 177 | 
            +
                        encoding = headers_t["Content-Type"].scan(/encoding='?(.*)'?/i).join
         | 
| 178 | 
            +
                      end
         | 
| 179 | 
            +
                    end
         | 
| 180 | 
            +
             | 
| 181 | 
            +
                    begin
         | 
| 182 | 
            +
                      data_s = JSON.pretty_generate(JSON.parse(data))
         | 
| 183 | 
            +
                    rescue
         | 
| 184 | 
            +
                      data_s = data
         | 
| 185 | 
            +
                    end
         | 
| 186 | 
            +
                    data_s = data_s.to_s().gsub("<", "<")
         | 
| 187 | 
            +
                  end
         | 
| 188 | 
            +
                  if headers_t.keys.include?("Accept-Encoding")
         | 
| 189 | 
            +
                    headers_t["Accept-Encoding"].gsub!("gzip", "") #removed so the response is in plain text
         | 
| 190 | 
            +
                  end
         | 
| 191 | 
            +
             | 
| 192 | 
            +
                  headers_ts = ""
         | 
| 193 | 
            +
                  headers_t.each { |key, val| headers_ts += key.to_s + ":" + val.to_s() + ", " }
         | 
| 194 | 
            +
                  message = "\n\n#{"- " * 25}\n"
         | 
| 195 | 
            +
                  if arguments.size == 1 and arguments[0].kind_of?(Hash) and arguments[0].key?(:name)
         | 
| 196 | 
            +
                    message += "#{method_s.upcase} Request: #{arguments[0][:name]}"
         | 
| 197 | 
            +
                  else
         | 
| 198 | 
            +
                    message += "#{method_s.upcase} Request"
         | 
| 199 | 
            +
                  end
         | 
| 200 | 
            +
                  message += "\n path: " + path.to_s() + "\n"
         | 
| 201 | 
            +
                  if @debug or @prev_request[:path] != path or @prev_request[:headers] != headers_t or @prev_request[:data] != data
         | 
| 202 | 
            +
                    message += " headers: {" + headers_ts.to_s() + "}\n"
         | 
| 203 | 
            +
                    message += " data: " + data_s.to_s() + "\n"
         | 
| 204 | 
            +
                    message = @message_server + "\n" + message
         | 
| 205 | 
            +
                  else
         | 
| 206 | 
            +
                    message += " Same#{" headers" if headers_t != {}}#{" and" if headers_t != {} and data.to_s != ""}#{" data" if data.to_s != ""} as in the previous request."
         | 
| 207 | 
            +
                  end
         | 
| 208 | 
            +
                  if path.to_s().scan(/^https?:\/\//).size > 0 and path.to_s().scan(/^https?:\/\/#{@host}/).size == 0
         | 
| 209 | 
            +
                    # the path is for another server than the current
         | 
| 210 | 
            +
                    # todo: identify if it is better to log the request, or if it is done later
         | 
| 211 | 
            +
                  else
         | 
| 212 | 
            +
                    self.class.last_request = message
         | 
| 213 | 
            +
                    @logger.info(message)
         | 
| 214 | 
            +
                  end
         | 
| 215 | 
            +
             | 
| 216 | 
            +
                  if data.to_s() != "" and encoding.to_s().upcase != "UTF-8" and encoding != ""
         | 
| 217 | 
            +
                    data = data.to_s().encode(encoding, "UTF-8")
         | 
| 218 | 
            +
                  end
         | 
| 219 | 
            +
                  headers_t.each do |k, v|
         | 
| 220 | 
            +
                    # for lambdas
         | 
| 221 | 
            +
                    headers_t[k] = v.call if v.is_a?(Proc)
         | 
| 222 | 
            +
                  end
         | 
| 223 | 
            +
                  @prev_request[:path] = path
         | 
| 224 | 
            +
                  @prev_request[:data] = data
         | 
| 225 | 
            +
                  @prev_request[:headers] = headers_t
         | 
| 226 | 
            +
                  @prev_request[:method] = method_s.upcase
         | 
| 227 | 
            +
                  if arguments.size == 1 and arguments[0].kind_of?(Hash) and arguments[0].key?(:name)
         | 
| 228 | 
            +
                    @prev_request[:name] = arguments[0][:name]
         | 
| 229 | 
            +
                  end
         | 
| 230 | 
            +
                  return path, data, headers_t
         | 
| 231 | 
            +
                rescue Exception => stack
         | 
| 232 | 
            +
                  @logger.fatal(stack)
         | 
| 233 | 
            +
                  @logger.fatal("manage_request Error on method #{method_s} . path:#{path.to_s()}. data:#{data.to_s()}. headers:#{headers_t.to_s()}")
         | 
| 234 | 
            +
                  return :error
         | 
| 235 | 
            +
                end
         | 
| 236 | 
            +
              end
         | 
| 237 | 
            +
            end
         |