teresa
teresa

Reputation: 366

Rails: Form validation on a API call

Using a form to enter the API key, I wanted to check that the API key returns a valid response before saving the form. Otherwise, it will just return back to the form with a error message (like a common validation error).

I tried looking through stackoverflow: How do I perform validation on an api call while submitting form and produce necessary error message?.

I've tested the validation code and it works but I have it in the controller. I'm new to using API with Rails but I believe form validations should be done in the model...but once I put the code in the model it errors.

This is what I have in the controller:

meetup_apikey = @group.meetup_apikey 
group_urlname = @group.meetup_urlname

url = "https://api.meetup.com/2/events?key=#{meetup_apikey}&sign=true&photo-host=public&group_urlname=#{group_urlname}"

  def test_api_responds_correctly(dataurl)
    response = HTTParty.get(dataurl)
    puts "\nAPI Response success: #{response.success?}\n\n"

    case response.success?
    when true
        puts "-------"
        puts "API Response code: #{response.code}"
        puts "API Response message: #{response.message}"
        puts "API Response body: #{response.body}"

        puts
      else
        flash[:error] = "Invalid API key or group urlname"
      end
  end

  # start the program
  test_api_responds_correctly(url)

If I entered a correct API key and valid group url name. The terminal returns:

API Response success: true

-------

API Response code: 200

API Response message: OK

API Response body: {"results":[{"utc_offset":-25200000,"venue":... }} 

And the user is redirected to a page with the information entered and a Group has been successfully updated message above.

If I entered an invalid API key, the terminal returns:

API Response success: false

And the user is redirected to a page with the information entered. With Group has been successfully updated and Invalid API key or group url name messages above.

This is not intended, I want the form to return a common validation error and stay on the form page until the API response is valid. I've tried to put the code in the model but I just get a undefined method 'test_api_responds_correctly' error.

Any suggestions and insights would help. Thanks!

Upvotes: 0

Views: 762

Answers (1)

Sergey Moiseev
Sergey Moiseev

Reputation: 2963

You need to create custom validation method on your group model like that:

validates :meetup_apikey, :meetup_urlname, presence: true
validate :api_key_valid?

def api_key_valid?
  url = "https://api.meetup.com/2/events?key=#{meetup_apikey}&sign=true&photo-host=public&group_urlname=#{meetup_urlname}"
  if !HTTParty.get(dataurl).success?
    errors.add(:meetup_apikey, "needs to be valid")
  end
end

After that attempt to create group without meetup_apikey and meetup_urlname will lead to default required attribute not present validation error and when they are but invalid, error for meetup_apikey will appear in validation.

Upvotes: 1

Related Questions