0

RESTful JSON API を提供する別の Rails アプリがあります。以下の curl リクエストを介してレスポンスをテストできます (返される最初の項目の例も参照できます)。

より大きな応答から特定のアイテムを抽出する方法を理解できます。これをクライアントにレンダリングするRailsアプリで応答を提供する方法がわかりません。

これを何らかのクラスに保存する必要があると思います。カールは使えますか?私はレールに非常に慣れており、どんな助けにも非常に感謝しています。

ありがとう!

curl "http://api.foobar.com/stuff/stuff_name/catalog_items.json" -H "X-Api-Key: georgesbush"

これは、JSON データの適切なセットを返します。

{
    "data": {
        "catalog_items": [
            {
                "current_price": "9999.0",
                "close_date": "2013-05-14T16:08:00-04:00",
                "open_date": "2013-04-24T11:00:00-04:00",
                "stuff_count": 82,
                "minimum_price": "590000.0",
                "id": 337478,
                "estimated_price": "50000.0",
                "name": "This is a really cool name",
                "current_winner_id": 696969,
                "images": [
                    {
                        "thumb_url": "http://foobar.com/images/93695/thumb.png?1365714300",
                        "detail_url": "http://foobar.com/images/93695/detail.png?1365714300",
                        "position": 1
                    },
                    {
                        "thumb_url": "http://foobar.com/images/95090/thumb.jpg?1366813823",
                        "detail_url": "http://foobar.com/images/95090/detail.jpg?1366813823",
                        "position": 2
                    }
                ]
            }
        ]
    },
    "pagination": {
        "per_page": 1,
        "page": 1,
        "total_pages": 131,
        "total_objects": 131
    }
}
4

1 に答える 1

0

私は ruby​​ 縁石ライブラリを使用しました。うわー、あまりにも簡単です。gem ファイルに縁石を追加し、コントローラー アクションに次のコードを追加しました。

http = Curl.get("http://api.foobar.com/stuff/stuff_name/catalog_items.json?per_page=1&page=1") do|http|
  http.headers['X-Api-Key'] = 'georgebush'
end
foobar =  http.body_str
@foobar = ActiveSupport::JSON.decode(foobar ).symbolize_keys
于 2013-04-28T04:05:36.463 に答える