Ask A Question

Notifications

You’re not receiving notifications from this thread.

How do I add HTTParty to ActiveJob ?

Arnas Klasauskas asked in General

I'm currently trying to make a GET request with ActiveJob. It looks like everything is working, but somehow the GET request isn't saving in my database.

This is my TypeformJob:

  def perform(current_user)
    response = HTTParty.get("https://api.typeform.com/forms/XXXXX/responses?page_size=25&query=#{current_user.hash_id}",
      headers: { "Authorization" => "Bearer #{@token}"})
    if response.code.to_i == 200
      items = response.parsed_response["items"]
      items.each do |item|
        @order = current_user.orders.find_or_create_by(landing_id: item["landing_id"]) do |order|
          order.landing_id = item["landing_id"]
          order.order_number = item["hidden"]["order_number"]
          order.moduls = item["hidden"]["moduls"]
          order.project = item["hidden"]["project"]
          order.category = item["hidden"]["nickname"]
          order.price = item["hidden"]["price"]
          order.total = item["hidden"]["total"]
          order.summary = item["answers"][0]["text"]
          order.payment = item["answers"][1]["choice"]["label"]
          order.number = item["answers"][2]["number"]
          order.day = item["answers"][3]["date"]
          order.contact = item["answers"][4]["choice"]["label"]
        end
      end
    else
      p response
    end
  end

My OrdersController:

  def get_requests
    TypeformJob.perform_later(current_user)
  end

The console responses with this:

[ActiveJob] [TypeformJob] [2560dd37-cc4c-4cc3-95e9-5787b65e1960]   Order Load (0.4ms)  SELECT  "orders".* FROM "orders" WHERE "orders"."user_id" = $1 AND "orders"."landing_id" = $2 LIMIT $3  [["user_id", 4], ["landing_id", "16c950ff40b24dc41300a5ceb6cb4e7e"], ["LIMIT", 1]]
[ActiveJob] [TypeformJob] [2560dd37-cc4c-4cc3-95e9-5787b65e1960]   ↳ app/jobs/typeform_job.rb:12
[ActiveJob] [TypeformJob] [2560dd37-cc4c-4cc3-95e9-5787b65e1960]    (0.5ms)  BEGIN
[ActiveJob] [TypeformJob] [2560dd37-cc4c-4cc3-95e9-5787b65e1960]   ↳ app/jobs/typeform_job.rb:12
[ActiveJob] [TypeformJob] [2560dd37-cc4c-4cc3-95e9-5787b65e1960]    (0.2ms)  ROLLBACK
[ActiveJob] [TypeformJob] [2560dd37-cc4c-4cc3-95e9-5787b65e1960]   ↳ app/jobs/typeform_job.rb:12
[ActiveJob] [TypeformJob] [2560dd37-cc4c-4cc3-95e9-5787b65e1960] Performed TypeformJob (Job ID: 2560dd37-cc4c-4cc3-95e9-5787b65e1960) from Async(default) in 1007.91ms

Any thoughts on this? I'm not sure where the ROLLBACK does come from

Reply

Hey Arnas!

Anytime you see a ROLLBACK in your logs, that means your validations failed.

In development, you print out what errors there are if it fails for debugging pretty easily.

If this is happening in production, you could change to find_or_create_by! so it throws an exception instead instead of silently failing. Then at least you can debug it through your error tracking.

Reply
Join the discussion
Create an account Log in

Want to stay up-to-date with Ruby on Rails?

Join 86,946+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.