Class: Event

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/get_freaky/event.rb

Constant Summary collapse

BASE_URI =
"http://confreaks.tv/api/v1"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params = {}) ⇒ Event

TODO: I’m pretty sure that at this number of arguments it should just take a hash



8
9
10
11
12
13
14
# File 'lib/get_freaky/event.rb', line 8

def initialize(params = {})
  self.short_code = params[:short_code]
  self.conference_name = params[:conference_name]
  self.conference_id = params[:conference_id]
  self.video_count = params[:video_count]
  self.date = params[:date]
end

Instance Attribute Details

#conference_idObject

Returns the value of attribute conference_id.



5
6
7
# File 'lib/get_freaky/event.rb', line 5

def conference_id
  @conference_id
end

#conference_nameObject

Returns the value of attribute conference_name.



5
6
7
# File 'lib/get_freaky/event.rb', line 5

def conference_name
  @conference_name
end

#dateObject

Returns the value of attribute date.



5
6
7
# File 'lib/get_freaky/event.rb', line 5

def date
  @date
end

#short_codeObject

Returns the value of attribute short_code.



5
6
7
# File 'lib/get_freaky/event.rb', line 5

def short_code
  @short_code
end

#video_countObject

Returns the value of attribute video_count.



5
6
7
# File 'lib/get_freaky/event.rb', line 5

def video_count
  @video_count
end

Class Method Details

.find(short_code) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/get_freaky/event.rb', line 25

def self.find(short_code)
  response = get("/events/#{short_code}.json")
  if response["status"] == 404
    NullEvent.new("No event was found with that name")
  elsif response.success?
    self.new(
      {
        :short_code      => response["short_code"],
        :conference_name => response["conference"]["name"],
        :conference_id   => response["conference"]["id"],
        :video_count     => response["video_count"],
        :date            => fix_date(response["start_at"])
      }
    )
  else
    # This raises the net/http response that was raised
    raise response.response
  end
end

Instance Method Details

#download_all_videos!Object



71
72
73
# File 'lib/get_freaky/event.rb', line 71

def download_all_videos!
  videos.each { |video| video.download }
end

#find_conferenceObject



45
46
47
# File 'lib/get_freaky/event.rb', line 45

def find_conference
  Conference.find(self.conference_name)
end

#nameObject

Alias short_code as name; “short_code” is consistent with the api, but “name” is easier to remember



17
18
19
# File 'lib/get_freaky/event.rb', line 17

def name
  short_code
end

#to_sObject



21
22
23
# File 'lib/get_freaky/event.rb', line 21

def to_s
  %Q{\nConference: #{conference_name}\nNumber of Videos: #{video_count}}
end

#valid?Boolean

Returns:

  • (Boolean)


75
76
77
# File 'lib/get_freaky/event.rb', line 75

def valid?
  true
end

#video_listObject

TODO: This implementation isn’t really ideal. By calling the videos method you’re hitting the api again. Ideally you want to just hit the api once and store that information away since you know that hitting the API takes more resources (and time frankly) then just storing the names in memory which is all you really care about until you need to actually get the information for that particular video Probably should do something like store the list of video names as strings as soon as you create the object and then keep that array for future use This seems slightly better but I’m still not sure it’s ideal



67
68
69
# File 'lib/get_freaky/event.rb', line 67

def video_list
  videos.map { |video| video.title }
end

#videosObject



49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/get_freaky/event.rb', line 49

def videos
  response = HTTParty.get("#{BASE_URI}/events/#{short_code}/videos.json")
  videos = []
  if response.success?
    response.each do |video|
      videos << Video.find(short_code, video["title"])
    end
    return videos
  else
    # This raises the net/http response that was raised
    raise response.response
  end
end