I want to solve the NoMethodError that appears when I hit Gurunabi api with Ruby.

Asked 1 years ago, Updated 1 years ago, 95 views

I want to earn information using Gurunabi API in LINEbot, so I write the code in ruby's controller file, but I am having trouble with the following error.I would appreciate your help.

I, [2019-10-03T00:56:21.711041#4]INFO--:[eb3f8fb8-27c3-413a-93cf-c2b167063968] Completed 500 Internal Server Error in 922ms
2019-10-03T00:56:21.711691 + 00:00 app [web.1]:F, [2019-10-03T00:56:21.711621#4]FATAL --:[eb3f8fb8-27c3-413a-93cf-c2b167063968]
2019-10-03T00:56:21.711762 + 00:00 app [web.1]:F, [2019-10-03T00:56:21.711705#4]FATAL --:[eb3f8fb8-27c3-413a-93cf-c2b167063968] NoMethodError(undefined method `sample' for nil: NilClass):
2019-10-03T00:56:21.711817 + 00:00 app [web.1]:F, [2019-10-03T00:56:21.711765#4]FATAL --:[eb3f8fb8-27c3-413a-93cf-c2b167063968]
2019-10-03T00:56:21.711880 + 00:00 app [web.1]:F, [2019-10-03T00:56:21.711829#4]FATAL --: [eb3f8fb8-27c3-413a-93cf-c2b167063968] app/controllers/linbot_controller.rb:48:in`block in callback'
2019-10-03T00:56:21.711881 + 00:00 app [web.1]: [eb3f8fb8-27c3-413a-93cf-c2b167063968] app/controllers/linebot_controller.rb:28:in`each'
2019-10-03T00:56:21.711883 + 00:00 app [web.1]: [eb3f8fb8-27c3-413a-93cf-c2b167063968] app/controllers/linebot_controller.rb:28:in`callback'

linebot_controller.rb

 events.each {|event|
  if event.message ['text']!=nil
    place=event.message ['text']     # Get the text you sent via LINE here
    result=`curl-X GET https://api.gnavi.co.jp/RestSearchAPI/v3/?keyid=78d2d49f3aa8747a6cc03da01cf41bdd&category_s=RSFST08008&category_s=RSFST08009&#{place}`# Hit the API here #{place}
  else
    latitude=event.message ['latitude']
    longitude=event.message ['longitude']

    result=`curl-X GET https://api.gnavi.co.jp/RestSearchAPI/v3/?keyid=78d2d49f3aa8747a6cc03da01cf41bdd&category_s=RSFST08008category_s=RSFST08009&latitude=#{latitude}longitude=#{longitude}`#Here API claps around
  end

  hash_result=JSON.parse result# Parse to hash because response is a string
  shops=hash_result["rest"]#Here's an array of store information
  shop=shops.sample#Select any one

  # store information
  url=shop["url_mobile"]# Send site URL
  shop_name = shop["name"]# Name of the store
  category=shop["category"]# Category
  open_time=shop["opentime"]# Free time
  holiday=shop["holiday"]#Closed
class LinebotController <ApplicationController
  require'line / bot'#gem'line-bot-api'

  # Disable CSRF Token Authentication for Callback Actions
  protect_from_forgery:except=>[:callback]

  def client
    @client||=Line::Bot::Client.new {|config|
      config.channel_secret=ENV["LINE_CHANNEL_SECRET"]
      config.channel_token=ENV["LINE_CHANNEL_TOKEN"]
    }
  end

  def callback
    body=request.body.read

    signature=request.env ['HTTP_X_LINE_SIGNATURE' ]
    unless client.validate_signature(body,signature)
      error400 do 'Bad Request' end
    end

    events=client.parse_events_from(body)

    # In this case gets sent over to has been known event is detected
    #  's Message text : When the reply can be determined character to be designated as
    # Steve Furstenberg. Games can acquire the message delivered by the [ ' text ' ]
    events.each { |event|
      if event.message['text'] != nil
        Developer =   the. me TLD Compromise Impersonation) Reveals the NewMessage [ ' text ' ]   # In this case we acquire a sentence that has been sent in a LINE
        result = `curl -X GET https://api.gnavi.co.jp/RestSearchAPI/v3/?keyid=78d2d49f3aa8747a6cc03da01cf41bdd&category_s=RSFST08008&category_s=RSFST08009&#{place}`#ここでぐるなびAPIを叩く#{place}
      else
        latitude = event.message['latitude']
        longitude = event.message['longitude']
        puts event.message['latitude']
        puts event.message['longitude']
        puts event.message['latitude'].class
        result = `curl -X GET https://api.gnavi.co.jp/RestSearchAPI/v3/?keyid=78d2d49f3aa8747a6cc03da01cf41bdd&category_s=RSFST08008&category_s=RSFST08009&latitude=#{latitude}&longitude=#{longitude}`#ここでぐるなびAPIを叩くlatitude=#{latitude}longitude=#{longitude}
      
      e n d
      hash code of parsing result t   = John would be a perfect fit for Apart   (Ventures said Paul )   a response is to class in shelve) dbhash because it is a perspective a character string #
      shops=hash_result["rest"]#Here's an array of store information
      shop=shops.sample#Select any one
      puts shop

      store information
      url=shop["url_mobile"]# Send site URL
      shop_name = shop["name"]# Name of the store
      category=shop["category"]# Category
      open_time=shop["opentime"]# Free time
      holiday=shop["holiday"]#Closed

      if open_time.class!=String #Two free times and regular holidays are returned with Hash when empty, so when I try to change it to a string, I get an error.Therefore, it depends on the class.
        open_time=""
     end
     if holiday.class!=String
        holiday=""
      end

      response="[store name]"+shop_name+"\n"+"[category]"+category+"\n"+"[business hours and regular holidays]"+open_time+"\n"+holiday+"\n"+url
       Case event#case statement When the value of the case matches when, the sentence in when is executed (like a switch statement)
      when Line::Bot::Event::Message
        case event.type
        when Line::Bot::Event::MessageType::Text,Line::Bot::Event::MessageType::Location
          message = {
            type: 'text',
            text —Response
          }
          client.reply_message(event['replyToken', message)
        end

      end
    } 

    head —ok
  end
end

hash_result=JSON.parse result I can earn the value up to
shops=hash_result["rest"] results in nil with an error.
I looked for syntax errors, but I'm in trouble because I couldn't find them.
I would appreciate it if you could reply.

ruby-on-rails ruby api line

2022-09-30 21:44

1 Answers

result=`curl-X GET https://api.gnavi.co.jp/RestSearchAPI/v3/?keyid=78d2d49f3aa8747a6cc03da01cf41bdd&category_s=RSFST08008&category_s=RSFST08009&#{place}`#Hit API here #{place}

There is a flaw in the external command. Wouldn't it work if I changed & to \\&?


2022-09-30 21:44

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.