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



42
43
44
# File 'app/models/mingle/facebook.rb', line 42

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

Class Method Details

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

Yields:



38
39
40
# File 'app/models/mingle/facebook.rb', line 38

def configure
  yield config
end

.create_post_from_data(data, hashtags) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'app/models/mingle/facebook.rb', line 46

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 = Mingle::Facebook::Post.ordered.last.try(:created_at)) ⇒ 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
# File 'app/models/mingle/facebook.rb', line 11

def fetch(hashtags = Mingle::Hashtag.all, since = Mingle::Facebook::Post.ordered.last.try(:created_at))
  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



33
34
35
36
# File 'app/models/mingle/facebook.rb', line 33

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



25
26
27
28
29
30
# File 'app/models/mingle/facebook.rb', line 25

def posts_through_search hashtag, since
  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