Class: Funky::Page

Inherits:
GraphRootNode show all
Defined in:
lib/funky/page.rb

Instance Attribute Summary

Attributes inherited from GraphRootNode

#data

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from GraphRootNode

#id, #initialize

Constructor Details

This class inherits a constructor from Funky::GraphRootNode

Class Method Details

.find(page_id) ⇒ Funky::Page

Fetches the data from Facebook Graph API and returns a Funky::Page object. It accepts a page ID.

Examples:

Getting a page object

Funky::Page.find 'FullscreenInc' # or
Funky::Page.find '221406534569729'
# => #<Funky::Page @data={:name=>"Fullscreen", :id=>"221406534569729"}>

Returns:

  • (Funky::Page)

    containing the data fetched by Facebook Graph API.



13
14
15
16
17
18
19
20
# File 'lib/funky/page.rb', line 13

def self.find(page_id)
  page = Funky::Connection::API.fetch("#{page_id}?fields=name,username,location,fan_count,featured_video")
  if page[:name].nil?
    raise ContentNotFound, "Page not found with ID #{page_id}"
  else
    new(page)
  end
end

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

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

Examples:

Getting one page

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

Getting multiple videos

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

Returns:

  • (Array<Funky::Page>)

    multiple instances of Funky::Page objects containing data obtained by Facebook’s APIs.



152
153
154
155
# File 'lib/funky/page.rb', line 152

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

Instance Method Details

#cityString?

Returns the city of the Facebook Page if it is present.

Returns:

  • (String, nil)

    the city of the Facebook Page if it is present

See Also:



99
100
101
# File 'lib/funky/page.rb', line 99

def city
  location[:city]
end

#countryString?

Returns the country of the Facebook Page if it is present.

Returns:

  • (String, nil)

    the country of the Facebook Page if it is present

See Also:



117
118
119
# File 'lib/funky/page.rb', line 117

def country
  location[:country]
end

#fan_countInteger

Returns the number of people who likes the Facebook page.

Returns:

  • (Integer)

    the number of people who likes the Facebook page.



79
80
81
# File 'lib/funky/page.rb', line 79

def fan_count
  data[:fan_count]
end

Returns if the Facebook page has featured_video.

Returns:

  • (Boolean)

    if the Facebook page has featured_video



84
85
86
# File 'lib/funky/page.rb', line 84

def has_featured_video?
  !data[:featured_video].nil?
end

#latitudeFixnum?

Returns the latitude of the Facebook Page if it is present.

Returns:

  • (Fixnum, nil)

    the latitude of the Facebook Page if it is present

See Also:



129
130
131
# File 'lib/funky/page.rb', line 129

def latitude
  location[:latitude]
end

#locationHash

Note:

location is a Hash that contains more specific properties such as city, state, zip, etc.

Returns the location of the Facebook Page if it is present.

Returns:

  • (Hash)

    the location of the Facebook Page if it is present

See Also:



93
94
95
# File 'lib/funky/page.rb', line 93

def location
  data.fetch(:location, {})
end

#longitudeFixnum?

Returns the longitude of the Facebook Page if it is present.

Returns:

  • (Fixnum, nil)

    the longitude of the Facebook Page if it is present

See Also:



135
136
137
# File 'lib/funky/page.rb', line 135

def longitude
  location[:longitude]
end

#nameString

Note:

For example, for www.facebook.com/platform the name is ‘Facebook For Developers’.

Returns the name of the Facebook Page.

Returns:

  • (String)

    the name of the Facebook Page.

See Also:



74
75
76
# File 'lib/funky/page.rb', line 74

def name
  data[:name]
end

#posts(options = {}) ⇒ Array<Funky::Post>

Fetches data from Facebook Graph API and returns an array of Funky::Post objects belong to the caller page.

Examples:

Getting posts under a page

page = Funky::Page.find 'FullscreenInc'
page.posts
# => [#<Funky::Post @data={...}>, #<Funky::Post @data={...}>]

Returns:

  • (Array<Funky::Post>)

    multiple Funky::Post objects containing data fetched by Facebook Graph API.



54
55
56
57
58
59
# File 'lib/funky/page.rb', line 54

def posts(options = {})
  path_query = "#{id}/posts?fields=type,created_time"
  path_query << "&since=#{options[:since]}" if options[:since]
  posts = Funky::Connection::API.fetch_all(path_query)
  posts.map {|post| Post.new(post)}
end

#stateString?

Returns the state of the Facebook Page if it is present.

Returns:

  • (String, nil)

    the state of the Facebook Page if it is present

See Also:



111
112
113
# File 'lib/funky/page.rb', line 111

def state
  location[:state]
end

#streetString?

Returns the street of the Facebook Page if it is present.

Returns:

  • (String, nil)

    the street of the Facebook Page if it is present

See Also:



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

def street
  location[:street]
end

#usernameString

Note:

For example, for www.facebook.com/platform the username is ‘platform’.

Returns the alias of the Facebook Page.

Returns:

  • (String)

    the alias of the Facebook Page.

See Also:



65
66
67
# File 'lib/funky/page.rb', line 65

def username
  data[:username]
end

#videos(options = {}) ⇒ Array<Funky::Video>

Fetches data from Facebook Graph API and returns an array of Funky::Video objects belong to the caller page.

Examples:

Getting videos under a page

page = Funky::Page.find 'FullscreenInc'
page.videos
# => [#<Funky::Video @data={...}>, #<Funky::Video @data={...}>]

Returns:

  • (Array<Funky::Video>)

    multiple Funky::Video objects containing data fetched by Facebook Graph API.



32
33
34
35
36
37
38
39
40
41
42
# File 'lib/funky/page.rb', line 32

def videos(options = {})
  path_query = "#{id}/videos?fields=id,title,description,created_time,length,comments.limit(0).summary(true),likes.limit(0).summary(true),reactions.limit(0).summary(true)"
  videos = []
  if options[:since]
    path_query << "&since=#{options[:since]}"
    videos = Funky::Connection::API.fetch_all(path_query)
  else
    videos = Funky::Connection::API.fetch(path_query, is_array: true)
  end
  videos.map {|video| Video.new(video) }
end

#zipString

Returns the zip code of the Facebook Page if it is present.

Returns:

  • (String)

    the zip code of the Facebook Page if it is present

See Also:



123
124
125
# File 'lib/funky/page.rb', line 123

def zip
  location[:zip]
end