Module: Mingle::Facebook

Defined in:
app/models/mingle/facebook.rb,
app/models/mingle/facebook/configuration.rb

Defined Under Namespace

Classes: Configuration, Fetch, Post

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.configObject



54
55
56
# File 'app/models/mingle/facebook.rb', line 54

def config
  @config ||= Mingle::Facebook::Configuration.new
end

Class Method Details

.configure {|config| ... } ⇒ Object

Yields:



50
51
52
# File 'app/models/mingle/facebook.rb', line 50

def configure
  yield config
end

.create_post_from_data(data, hashtags) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'app/models/mingle/facebook.rb', line 58

def create_post_from_data data, hashtags
  post = Post.find_or_initialize_by post_id: data.identifier

  post.attributes = {
    caption: data.caption,
    created_at: data.created_time,
    description: data.description,
    link: data.link,
    message: data.message,
    name: data.name,
    picture: data.picture,
    type: data.type,
    user_id: data.from.identifier,
    user_name: data.from.name
  }

  hashtags.each do |hashtag|
    if matches? post, hashtag.tag_name
      post.hashtags << hashtag unless post.hashtags.include? hashtag
    end
  end

  post.save!
  post
end

.fetch(hashtags = Mingle::Hashtag.all, since = nil) ⇒ Object

Fetch posts since given date/timestamp, last post’s creation date or beginning of time



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'app/models/mingle/facebook.rb', line 11

def fetch(hashtags = Mingle::Hashtag.all, since = nil)
  unless since
    if Post.any?
      since = Post.ordered.last.created_at
    else
      since = Mingle.config.since
    end
  end

  hashtags = Array(hashtags)

  posts = []
  hashtags.each do |hashtag|
    posts += posts_through_search hashtag, since
  end

  posts.collect do |data|
    create_post_from_data(data, hashtags)
  end
end

.posts_by_page(page_id, since) ⇒ Object

Retrieves posts for given page



45
46
47
48
# File 'app/models/mingle/facebook.rb', line 45

def posts_by_page page_id, since
  page = FbGraph::Page.new(page_id, access_token: config.access_token)
  page.posts(limit: 100, since: since.to_i, return_ssl_resources: 1)
end

.posts_through_search(hashtag, since) ⇒ Object

Retrieves posts through given search query



33
34
35
36
37
38
39
40
41
42
# File 'app/models/mingle/facebook.rb', line 33

def posts_through_search hashtag, since
  warn "DEPRECATION WARNING: Facebook has deprecated searching for posts. It is no " +
       "longer available for new applications and will cease to work for existing " +
       "applications in April 2015."

  posts = FbGraph::Post.search(hashtag.tag_name_with_hash, since: since.to_i, return_ssl_resources: 1, access_token: config.access_token)

  # Facebook search is fairly unpredictable, so ensure the query is actually mentioned
  posts.select { |post| matches? post, hashtag.tag_name }
end

.table_name_prefixObject



6
7
8
# File 'app/models/mingle/facebook.rb', line 6

def table_name_prefix
  "#{Mingle.table_name_prefix}facebook_"
end