Reputation: 1671
Looking for some help with the Meetup.com API I'm trying to add to my Rails app. My purpose is just to have a view of local meetups when you open the app.
Here is my code so far:
Routes.rb (at the bottom before the last 'end'):
root 'meetup#get_meetup'
meetup_controller.rb
class MeetupController < ApplicationController
def get_meetup
if params[:city] && params[:name]
@Event = Event.new(params[:city], params[:name])
else
@event = Event.new("90034", "networking")
end
end
end
meetup.rb:
class Event
attr_reader :city, :nam
def initialize(city, name)
url = "http://api.meetup.com/2/events?key#{ENV["meetup_api_key"]}&group_urlname=Girl-Develop-It-Austin&sign=true.json"
response = HTTParty.get(url)
@event = response["event"]
@city = city
@name = name
end
end
I tried to create a new event search using the params for the city and name (aka the event categories) in the rails console.
rails console: Event.new("Austin", "networking")
and it gave me the error NameError: uninitialized constant Event
**I'm still new to using third party API's especially with Rails so I haven't found too much information online about how to correctly get them to work. If someone could please give me some insight into why this isn't correct...or if theres a great article/website to help me with these please let me know!!
Upvotes: 0
Views: 263
Reputation: 183
You need to restart your rails console using reload!
also you've mentioned above meetup.rb file which includes
class MeetUp #you need to change class name
attr_reader :city, :name
def initialize(city, name)
url = "http://api.meetup.com/2/events?key#ENV["meetup_api_key"]}&group_urlname=Girl-Develop-It-Austin&sign=true.json"
response = HTTParty.get(url)
@event = response["event"]
@city = city
@name = name
end
end
If your filename is meet_up.rb the class name must MeetUp
not Event
Please change your class name or else you can change filename as event.rb
You can not define model or inside class as different name I hope my answer will help you. Thanks
Class name should be file_name_except_extension.classify
. Therefore it would be 'meet_up'.classify
i.e. MeetUp
and not Event
Upvotes: 0
Reputation: 6311
Restart the server after copy pasting, personally never tried to use meetup api.
controller
Meetup.new(city, name)
/config/application.rb
config.eager_load_paths << Rails.root.join('lib')
/lib/meetup.rb
require 'rest-client'
require 'net/http'
require 'json'
class Meetup
attr_reader :city, :name
def initialize(city, name)
resp = RestClient.get("http://api.meetup.com/2/events?key#{ENV['meetup_api_key']}&group_urlname=Girl-Develop-It-Austin&sign=true.json")
#or
resp = RestClient.get("http://api.meetup.com/2/events",
{ key: ENV['meetup_api_key'],
group_urlname: "Girl-Develop-It-Austin",
sign: true
})
@event = JSON.parse(resp, { symbolize_names: true })[:event]
@city = city
@name = name
end
end
Upvotes: 0
Reputation: 3998
Try to move your code into service object
Create a folder services inside app folder. Then move your Event class to service folder. Also in initialize method, a good practice is you initialize only the variable. For parsing creating another method.And lastly, you have done a spelling mistake which I have corrected
class Event
include HTTParty
attr_reader :city, :name ##you have made a spelling mistake
def initialize(city, name)
@city = city
@name = name
end
def parsing
url = "http://api.meetup.com/2/events?key#
{ENV["meetup_api_key"]}&group_urlname=Girl-Develop-It-
Austin&sign=true.json"
response = HTTParty.get(url)
@event = response["event"]
end
end
Upvotes: 0