【问题标题】:how to parse json url response in rails如何在rails中解析json url响应
【发布时间】:2012-11-23 08:21:44
【问题描述】:

我有一个简单的 Rails 应用程序,它向外部网站发出请求并返回一个带有回调的 json url 响应到我的网站,以通知我的 Rails 应用程序有关响应的信息。 json url 示例为https://voguepay.com/?v_transaction_id=demo-1345109950&type=json,响应正文如下:

{"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"}

我想将此响应转换为 Rails 方法,而不是简单地为我提供进行查询所需的属性。例如,从响应正文中,我需要执行如下操作:

def notify
  response = Json.parse('https://voguepay.com/?v_transaction_id=demo-1345109950&type=json').body
  response.status
  response.date
  response.merchant_id
  response.total
end

上面的代码只是一个示例,用于解释我想要实现的目标。任何帮助都会很棒。

我已经尝试了typhoeusyajl-ruby gems,但是当请求进来时,我所有的身份验证方法都被删除了,并且我不断收到错误消息无法验证 csrf 元令牌。即使我跳过它,当前用户也会自动注销(使用设计身份验证)。使用的代码示例如下:

class NotificationsController < 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}&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] >= @plan.naira_price.to_s)
                current_user.award_user_credits(@plan.hjc.to_i)
              end            
    end
    hydra.queue(request)
    hydra.run
  end
end

再说了,我不想用gem了,我想手动做看看csrf meta token会不会受到影响。因此,任何关于我如何实现这一目标的想法都会很棒。我正在使用导轨 3.2.9。

谢谢!

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-3 json ruby-on-rails-3.2


    【解决方案1】:

    你可以这样做:

    def notify
      response = JSON('https://voguepay.com/?v_transaction_id=demo-1345109950&type=json').body
     response["status"]
     response["date"]
     response["merchant_id"]
     response["total"]
    end    
    

    【讨论】:

    • 这可以正常工作吗?太棒了,我会试试这个。但是我会收到 csfr 令牌错误吗?
    • 检查 stackoverflow.com/questions/5669322/… 以了解如何禁用特定操作的 csrf 保护。这应该可以解决问题
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-27
    • 2013-01-26
    • 2017-04-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多