0

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!!

Asmita
  • 183
  • 2
  • 14
DianaBG
  • 1,511
  • 3
  • 10
  • 13

3 Answers3

0

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
7urkm3n
  • 6,054
  • 4
  • 29
  • 46
  • Thank you for your response! Just a question, but why would the meetup.rb be in the lib folder? I had placed it in my models folder as a new model. Also, I am now getting this error "NameError: undefined local variable or method `meetup' for main:Object" I think another issue is I'm a bit unsure of what to name event or meetup. I've changed my variables to "meetup" as well as the file names...but that may be an issue. – DianaBG Oct 13 '17 at 17:41
  • I'm trying to follow a few guidelines as well as the documentation but its hard to know exactly what to do as a beginner using API's. – DianaBG Oct 13 '17 at 17:42
  • Also where exactly am i placing that config line in application.rb. It gave me an error when updating my db. (config.eager_load_paths << Rails.root.join('lib'))? – DianaBG Oct 13 '17 at 17:54
  • @DianaBG what rails version do u use ? ill update my post – 7urkm3n Oct 13 '17 at 18:39
  • Thank you! I have version 2.3.1 @7urkm3n – DianaBG Nov 19 '17 at 15:16
0

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
Aniket Tiwari
  • 3,561
  • 4
  • 21
  • 61
0

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

swag
  • 13
  • 3
Asmita
  • 183
  • 2
  • 14
  • Thank you for your comment! I've edited that as well as changed some other issues with my code and have gotten the new error "NameError: undefined local variable or method `meetup' for main:Object". – DianaBG Oct 13 '17 at 17:43
  • If it is your ActiveRecord(Model) you need to remove earlier created migration/model from the app/model and db/migration directory manually after that you can create another migration to create meetup.rb – Asmita Oct 14 '17 at 04:01