LoginSignup
0
0

More than 3 years have passed since last update.

(質問です)rubyのapiの叩き方について

Last updated at Posted at 2019-10-02

rubyでapiを叩きたい

現在プログラミング歴1ヶ月でlinebotを作成しながら勉強しています。

ぐるなびAPIをlinebotに使用したい

ぐるなびAPIをlinebotに使用する所でつまづいており、皆様にご質問があります。

下記エラーが解決出来ません
NoMethodError (undefined method `sample' for nil:NilClass):

該当コードは

linebot_controller.rb
events.each { |event|
      if event.message['text'] != nil
        place = event.message['text'] #ここでLINEで送った文章を取得
        result = `curl -X GET https://api.gnavi.co.jp/RestSearchAPI/v3/?keyid=ffb92f0f997a628153ecfa407099fe9b&category_s=RSFST08008&category_s=RSFST08009&#{place}`
      else
        latitude = event.message['latitude']
        longitude = event.message['longitude']

        result = `curl -X GET https://api.gnavi.co.jp/RestSearchAPI/v3/?keyid=ffb92f0f997a628153ecfa407099fe9b&category_s=RSFST08008category_s=RSFST08009&latitude=#{latitude}longitude=#{longitude}`#ここでぐるなびAPIを叩く
      end

      hash_result = JSON.parse result  #レスポンスが文字列なのでhashにパースする
      shops = hash_result["rest"] #ここでお店情報が入った配列となる
      shop = shops.sample #任意のものを一個選ぶ

hash_result = JSON.parse result 部分まではしっかり値が入っているのですが、
shops = hash_result["rest"]   ←この部分がnilで返ってきてしまいエラーを吐き出してしまいます。
文法のミス等自分なりにapiを叩く記述を参考にしながら行なったのですが解決出来ません。
皆様のお力をお貸しいただけると幸いです。

以下参考記事
https://qiita.com/NoharaMasato/items/6fb1ac277c965905e019

0
0
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0