4

Here my HTTParty code

      response = HTTParty.post(api_url,body: form_data,timeout: 5)
      rescue Timeout::Error
      ## create dummy response with 500 error code
      response = HTTParty::Response.new()
      ensure
       response

all I'm trying to do is ensure If the HTTParty is unable to connect the given website create a dummy response body objec

But when I try to create a dummy Response object like this

     ## response = HTTParty::Response.new(Rack::Request.new(api_url),Rack::Response.new('TimeOut Error',500),'TimeOutError')

but this does not work because my response object does not respond_to to_hash

Can anyone suggest a better way to accomplish the same

Ratatouille
  • 1,372
  • 5
  • 23
  • 50
  • What are you hoping for? You could respond with a JSON object that can be converted to a `Hash` using `JSON.parse` but your post is a little confusing – engineersmnky Aug 01 '14 at 19:45
  • All I want is to create a dummy HTTParty Response object if the HTTParty response get time out. – Ratatouille Aug 04 '14 at 14:31

1 Answers1

13

In case anybody comes looking after 4 years, one could try the following:

httparty_req = HTTParty::Request.new Net::HTTP::Get, '/'
nethttp_resp = Net::HTTPInternalServerError.new('1.1', 500, 'Internal Server Error')
response = HTTParty::Response.new(httparty_req, nethttp_resp, lambda {''}, body: '')
thunder
  • 544
  • 5
  • 8
  • 8 years later I now ant to know how to do it without out an error and a successful response. – Whitecat Mar 08 '22 at 15:17
  • `httparty_req = HTTParty::Request.new Net::HTTP::Get, '/' nethttp_resp = Net::HTTPOK.new(1.0, "200", "OK")` `response = HTTParty::Response.new(httparty_req, nethttp_resp, lambda {''}, body: '')` – thunder Jul 02 '22 at 17:34