Class: Funky::Video

Inherits:
Object
  • Object
show all
Defined in:
lib/funky/video.rb

Constant Summary collapse

@@html_page =
HTML::Page.new
@@html_parser =
HTML::Parser.new

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data) ⇒ Video

Returns a new instance of Video.



13
14
15
# File 'lib/funky/video.rb', line 13

def initialize(data)
  @data = data
end

Instance Attribute Details

#counters ⇒ Object

Returns the value of attribute counters.



8
9
10
# File 'lib/funky/video.rb', line 8

def counters
  @counters
end

#data ⇒ Object (readonly)

Returns the value of attribute data.



7
8
9
# File 'lib/funky/video.rb', line 7

def data
  @data
end

Class Method Details

.fetch_and_parse_data(ids) ⇒ Object



111
112
113
114
115
116
117
118
119
120
121
# File 'lib/funky/video.rb', line 111

def self.fetch_and_parse_data(ids)
  if ids.is_a?(Array) && ids.size > 1
    response = Connection::API.batch_request(ids: ids, fields: fields)
  else
    id = ids.is_a?(Array) ? ids.first : ids
    response = Connection::API.request(id: id, fields: fields)
  end
  parse response
rescue ContentNotFound
  []
end

.fields ⇒ Object



142
143
144
145
146
147
148
149
# File 'lib/funky/video.rb', line 142

def self.fields
  [
    'created_time',
    'description',
    'length',
    'picture'
  ]
end

.find(video_id) ⇒ Funky::Video

Fetches the data from Facebook's HTML and instantiates the data into a single Funky::Video object. It can accept one only video ID.

Examples:

Getting a video

Funky::Video.find('10153834590672139') # => #<Funky::Video>

Returns:

  • (Funky::Video) —

    the data scraped from Facebook's HTML and encapsulated into a Funky::Video object.



89
90
91
92
# File 'lib/funky/video.rb', line 89

def self.find(video_id)
  counters = @@html_parser.parse html: @@html_page.get(video_id: video_id)
  new counters.merge(id: video_id)
end

.find_by_url!(url) ⇒ Funky::Video

Similar to #find, but it finds the video by url instead of video id. Fetches the data from Facebook's HTML and instantiates the data into a single Funky::Video object. It can accept one only video url.

Examples:

Getting a video by url

url = 'https://www.facebook.com/video.php?v=203203106739575'
Funky::Video.find_by_url!(url) # => #<Funky::Video>

Returns:

  • (Funky::Video) —

    the data scraped from Facebook's HTML and encapsulated into a Funky::Video object.



104
105
106
107
# File 'lib/funky/video.rb', line 104

def self.find_by_url!(url)
  url = URL.new(url)
  find(url.video_id)
end

.instantiate_collection(items) ⇒ Object



138
139
140
# File 'lib/funky/video.rb', line 138

def self.instantiate_collection(items)
  items.collect { |item| new item }
end

.parse(response) ⇒ Object



123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/funky/video.rb', line 123

def self.parse(response)
  if response.code == '200'
    body = JSON.parse response.body, symbolize_names: true
    if body.is_a? Array
      body.select{|item| item[:code] == 200}.collect do |item|
        JSON.parse(item[:body], symbolize_names: true)
      end.compact
    else
      [body]
    end
  else
    raise ContentNotFound, "Error #{response.code}: #{response.body}"
  end
end

.where(id:) ⇒ Array<Funky::Video>

Fetches the data from Facebook's APIs and instantiates the data into an Array of Funky::Video objects. It can accept one video ID or an array of multiple video IDs.

Examples:

Getting one video

id = '10153834590672139'
Funky::Video.where(id: id) # => [#<Funky::Video>]

Getting multiple videos

ids = ['10154439119663508', '10153834590672139']
Funky::Video.where(id: ids) # => [#<Funky::Video>, #<Funky::Video>]

Returns:

  • (Array<Funky::Video>) —

    multiple instances of Funky::Video objects containing data obtained by Facebook's APIs.



76
77
78
79
# File 'lib/funky/video.rb', line 76

def self.where(id:)
  return nil unless id
  instantiate_collection(fetch_and_parse_data Array(id))
end

Instance Method Details

#comment_count ⇒ Integer

Returns the total number of comments for the video.

Returns:

  • (Integer) —

    the total number of comments for the video.



49
50
51
# File 'lib/funky/video.rb', line 49

def comment_count
  data[:comment_count]
end

#created_time ⇒ DateTime

Returns the created time of the video.

Returns:

  • (DateTime) —

    the created time of the video.



23
24
25
26
# File 'lib/funky/video.rb', line 23

def created_time
  datetime = data[:created_time]
  DateTime.parse datetime if datetime
end

#description ⇒ String

Returns the description of the video.

Returns:

  • (String) —

    the description of the video.



29
30
31
# File 'lib/funky/video.rb', line 29

def description
  data[:description]
end

#id ⇒ String

Returns the video ID.

Returns:

  • (String) —

    the video ID.



18
19
20
# File 'lib/funky/video.rb', line 18

def id
  data[:id]
end

#length ⇒ Float

Returns the length (duration) of the video.

Returns:

  • (Float) —

    the length (duration) of the video.



34
35
36
# File 'lib/funky/video.rb', line 34

def length
  data[:length]
end

#like_count ⇒ Integer

Returns the total number of likes for the video.

Returns:

  • (Integer) —

    the total number of likes for the video.



44
45
46
# File 'lib/funky/video.rb', line 44

def like_count
  data[:like_count]
end

#picture ⇒ String

Returns the picture URL of the video.

Returns:

  • (String) —

    the picture URL of the video.



39
40
41
# File 'lib/funky/video.rb', line 39

def picture
  data[:picture]
end

#share_count ⇒ Integer

Returns the total number of shares for the video.

Returns:

  • (Integer) —

    the total number of shares for the video.



54
55
56
# File 'lib/funky/video.rb', line 54

def share_count
  data[:share_count]
end

#view_count ⇒ Integer

Returns the total number of views for the video.

Returns:

  • (Integer) —

    the total number of views for the video.



59
60
61
# File 'lib/funky/video.rb', line 59

def view_count
  data[:view_count]
end