title: how to parse json url response in rails
body: <p>I've a simple Rails app that makes a request to an external website and returns a json url response with a callback to my site to notify my Rails app about the response. The json url sample is <code>https://voguepay.com/?v_transaction_id=demo-1345109950&amp;type=json</code> and the response body is below:</p>
  
  <pre><code>{"merchant_id":"demo","transaction_id":"demo-1345109950","email":"testuser@buyerdomain.com","total":"10","total_paid_by_buyer":"10.00","total_credited_to_merchant":"9.90","extra_charges_by_merchant":"0.00","merchant_ref":"","memo":"Donation of N10 to Test User","status":"Approved","date":"2012-01-01 11:39:11","referrer":"http:\/\/sellerdomain.com\/buy_now.html","method":"VoguePay","fund_maturity":"2012-01-03"}
  </code></pre>
  
  <p>I'd like to convert this response into a Rails method than can simply give me the attribute I need to make my query. For example, from the response body I need to make an action like: </p>
  
  <pre><code>def notify
    response = Json.parse('https://voguepay.com/?v_transaction_id=demo-1345109950&amp;type=json').body
    response.status
    response.date
    response.merchant_id
    response.total
  end
  </code></pre>
  
  <p>The code above is just a sample to explain what I want to achieve. Any help will be great.</p>
  
  <p>I've tried both <a href="http://rubygems.org/gems/typhoeus" rel="nofollow">typhoeus</a> and <a href="http://rubygems.org/gems/yajl-ruby" rel="nofollow">yajl-ruby</a> gems but when the request comes in, all my authentication methods are removed and I keep getting the error message cannot authenticate csrf meta token. Even if I skip it the current user will be signed out automatically ( with devise authentication). The code sample used is below:</p>
  
  <pre><code>class NotificationsController &lt; ApplicationController
    skip_before_filter :verify_authenticity_token
  
    def notify
        @transaction_id = params[:transaction_id]
        do_notify
    end
  
    private
    def do_notify
      hydra = Typhoeus::Hydra.new
      request = Typhoeus::Request.new("https://voguepay.com/?v_transaction_id=#{@transaction_id}&amp;type=json")
      request.on_complete do |response|
          logger.info("#{response.request.url} in #{response.time} seconds")  #remove in production to avoid huge logs
                transaction = Yajl::Parser.parse(response.body)  #or Yajl::Parser.parse(response.body)
  
                #Now we have the following keys in our transaction hash  you can do whatever
                transaction[:merchant_id]
                transaction[:transaction_id]
                transaction[:email]
                transaction[:total]
                transaction[:merchant_ref]
                transaction[:memo]
                transaction[:status]
                transaction[:date]
                transaction[:referrer]
                transaction[:method]
                @plan = Plan.find_by_id(transaction[:merchant_ref])
                if(transaction[:total] == 0)
                  logger.error "Invalid total for transaction:#{@transaction_id}"
                   #do not subscribe the user or generate invoice, notify user of error n cancel order
                elsif(transaction[:status] != 'Approved')
                  logger.error "Failed transaction for transaction:#{@transaction_id}"
                   #do not subscribe the user or generate invoice, notify user of error n cancel order
                elsif(transaction[:total] &gt;= @plan.naira_price.to_s)
                  current_user.award_user_credits(@plan.hjc.to_i)
                end            
      end
      hydra.queue(request)
      hydra.run
    end
  end
  </code></pre>
  
  <p>Anymore, I don't want to use a gem I want to do it manually to see if the csrf meta token will not be affected. So any idea on how I can achieve this will be great. I am using rails 3.2.9. </p>
  
  <p>Thank you!</p>