Class: MeetupWinner

Inherits:
Object
  • Object
show all
Defined in:
lib/meetup_winner.rb,
lib/meetup_winner/version.rb

Constant Summary collapse

VERSION =
"0.0.3"

Instance Method Summary collapse

Constructor Details

#initialize(key, group_name) ⇒ MeetupWinner

Returns a new instance of MeetupWinner.



8
9
10
11
12
13
14
15
16
17
18
# File 'lib/meetup_winner.rb', line 8

def initialize(key, group_name)
  begin
    group_name  = CGI::escape(group_name)
    @group_info = HTTParty.get("https://api.meetup.com/find/groups?&sign=true&text=#{group_name}&upcoming_events=true&key=#{key}&only=next_event")
    event_id    = @group_info[0]["next_event"]["id"]
    response    = HTTParty.get("https://api.meetup.com/2/rsvps?&sign=true&event_id=#{event_id}&page=500&key=#{key}")
    @attendance = response["results"]
  rescue
    puts "An error has ocurred, check your key and internet connection"
  end
end

Instance Method Details

#attendanceObject



31
32
33
34
35
36
37
38
# File 'lib/meetup_winner.rb', line 31

def attendance
  @attendance.each do |person|
    chosen_image(person["member_photo"])
    puts person["member"]["name"]
  end
  puts @group_info[0]["next_event"]["name"]
  puts "Number of members: #{count}"
end

#chooseObject



20
21
22
23
24
25
# File 'lib/meetup_winner.rb', line 20

def choose
  winner_position = rand(0..(count - 1))
  winner = @attendance[winner_position]
  chosen_image(winner["member_photo"])
  puts winner["member"]["name"]
end

#chosen_image(photo) ⇒ Object



43
44
45
46
47
48
49
50
51
# File 'lib/meetup_winner.rb', line 43

def chosen_image(photo)
  if photo
    remote_file = nil
    open(photo["photo_link"]) do |f|
      remote_file = Base64.encode64(f.read)
    end
    puts("\x1b]1337;File=;inline=1:" + remote_file + "\a\n")
  end
end

#countObject



27
28
29
# File 'lib/meetup_winner.rb', line 27

def count
  @attendance.count
end