Class: Vid::Facebook::Video

Inherits:
Base::Video show all
Defined in:
lib/vid/facebook/video.rb

Instance Attribute Summary collapse

Attributes inherited from Base::Video

#view_count

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) ⇒ Video

Returns a new instance of Video.



7
8
9
10
11
12
13
14
# File 'lib/vid/facebook/video.rb', line 7

def initialize(attributes = {})
  @id = attributes['id']
  @published_at = attributes['created_time']
  @title = attributes['description']
  @comment_count = attributes['comments']['summary']['total_count']
  @like_count = attributes['likes']['summary']['total_count']
  @view_count = attributes[:view_count]
end

Instance Attribute Details

#comment_countObject (readonly)

Returns the value of attribute comment_count.



6
7
8
# File 'lib/vid/facebook/video.rb', line 6

def comment_count
  @comment_count
end

#idObject (readonly)

Returns the value of attribute id.



6
7
8
# File 'lib/vid/facebook/video.rb', line 6

def id
  @id
end

#like_countObject (readonly)

Returns the value of attribute like_count.



6
7
8
# File 'lib/vid/facebook/video.rb', line 6

def like_count
  @like_count
end

#published_atObject (readonly)

Returns the value of attribute published_at.



6
7
8
# File 'lib/vid/facebook/video.rb', line 6

def published_at
  @published_at
end

#titleObject (readonly)

Returns the value of attribute title.



6
7
8
# File 'lib/vid/facebook/video.rb', line 6

def title
  @title
end

Class Method Details

.find(id) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/vid/facebook/video.rb', line 16

def self.find(id)
  uri = URI::HTTPS.build host: 'graph.facebook.com', path: "/v2.6/#{id}", query: "access_token=#{ENV['FB_APP_ID']}%7C#{ENV['FB_APP_SECRET']}&fields=id,permalink_url,created_time,description,title,from,comments.limit(0).summary(true),likes.limit(0).summary(true)"
  http_request = Net::HTTP::Get.new(uri.request_uri)

  net_http_options = [uri.host, uri.port, use_ssl: true]
  response = Net::HTTP.start('graph.facebook.com', 443, use_ssl: true) do |http|
    http.request http_request
  end

  attributes = JSON response.body

  uri = URI::HTTPS.build host: 'www.facebook.com', path: "/video.php", query: "v=#{id}"
  http_request = Net::HTTP::Get.new(uri.request_uri)

  net_http_options = [uri.host, uri.port, use_ssl: true]
  response = Net::HTTP.start('www.facebook.com', 443, use_ssl: true) do |http|
    http.request http_request
  end

  view_count = %r{<div></div><span class="fcg">(?<views>.*) Views</span>}.match(response.body)[:views].tr(',','').to_i
  new attributes.merge(view_count: view_count)
end

.where(clauses) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/vid/facebook/video.rb', line 39

def self.where(clauses)
  uri = URI::HTTPS.build host: 'graph.facebook.com', path: "/", query: "include_headers=false&access_token=#{ENV['FB_APP_ID']}%7C#{ENV['FB_APP_SECRET']}"
  http_request = Net::HTTP::Post.new(uri.request_uri)
  batch = clauses[:id].map do |id|
    {"method":"GET", "relative_url": "/v2.6/#{id}?fields=id,permalink_url,created_time,description,title,from,comments.limit(0).summary(true),likes.limit(0).summary(true)"}
  end
  http_request.set_form_data batch: batch.to_json

  net_http_options = [uri.host, uri.port, use_ssl: true]
  response = Net::HTTP.start('graph.facebook.com', 443, use_ssl: true) do |http|
    http.request http_request
  end

  JSON(response.body).map do |attributes|
    uri = URI::HTTPS.build host: 'www.facebook.com', path: "/video.php", query: "v=#{JSON(attributes['body'])['id']}"
    http_request = Net::HTTP::Get.new(uri.request_uri)

    net_http_options = [uri.host, uri.port, use_ssl: true]
    response = Net::HTTP.start('www.facebook.com', 443, use_ssl: true) do |http|
      http.request http_request
    end

    view_count = %r{<div></div><span class="fcg">(?<views>.*) Views</span>}.match(response.body)[:views].tr(',','').to_i
    new JSON(attributes['body']).merge(view_count: view_count)
  end
end