Class: Okayu::LiveController

Inherits:
Object
  • Object
show all
Defined in:
lib/controllers/live_controller.rb

Instance Method Summary collapse

Constructor Details

#initialize(live_id) ⇒ LiveController

Returns a new instance of LiveController.



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/controllers/live_controller.rb', line 3

def initialize live_id
  @live_id = live_id
  if @client = Client.open(live_id)
    @live_now = true
    if @program = Program.find(:first, :conditions => {:live_id => @live_id})
      @community = @program.community
    else
      @program = Program.new
      @program.live_id = @live_id
      @program.base_time = @client.base_time
      unless @community = Community.find(:first, :conditions => {:community_id => @client.community})
        @community = Community.new
        @community.community_id = @client.community
        @community.save
      end
      @program.community = @community
      @program.save
    end
  else
    @live_now = false
    if @program = Program.find(:first, :conditions => {:live_id => @live_id})
      @community = @program.community
    else
      @community = nil
    end
  end
end

Instance Method Details

#base_timeObject



40
41
42
# File 'lib/controllers/live_controller.rb', line 40

def base_time
  @program.base_time
end

#closeObject



81
82
83
84
85
# File 'lib/controllers/live_controller.rb', line 81

def close
  if @live_now && @client
    @client.close
  end
end

#comment(number) ⇒ Object



52
53
54
# File 'lib/controllers/live_controller.rb', line 52

def comment number
  @program.comments.reload.find :first, :conditions => {:number => number}
end

#commentsObject



44
45
46
# File 'lib/controllers/live_controller.rb', line 44

def comments
  @program.comments
end

#live_now?Boolean

Returns:

  • (Boolean)


35
36
37
# File 'lib/controllers/live_controller.rb', line 35

def live_now?
  @live_now
end

#new_commentsObject



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/controllers/live_controller.rb', line 56

def new_comments
  if @live_now
    @client.comments.map{|comment_hash|
      if Comment.find :first, :conditions => {:program_id => @program.id, :number => comment_hash[:number]}
        nil
      else
        comment = Comment.new(comment_hash)
        comment.community = @community
        comment.program = @program
        if comment_hash[:user_id]
          if visitor = Visitor.find(:first, :conditions => {:user_id => comment_hash[:user_id]})
            comment.visitor = visitor
          else
            comment.create_visitor(:user_id => comment_hash[:user_id])
          end
        end
        comment.save
        comment
      end
    }.compact
  else
    []
  end
end

#reload_commentsObject



48
49
50
# File 'lib/controllers/live_controller.rb', line 48

def reload_comments
  @program.comments.reload
end

#valid?Boolean

Returns:

  • (Boolean)


31
32
33
# File 'lib/controllers/live_controller.rb', line 31

def valid?
  !!@program
end