Module: Facy::Converter

Included in:
Facy
Defined in:
lib/facy/converter.rb

Instance Method Summary collapse

Instance Method Details

#graph2item(graph_item) ⇒ Object

convert facebook graph return to Item#class



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
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
65
66
67
68
# File 'lib/facy/converter.rb', line 6

def graph2item(graph_item)
  id = graph_item["id"]
  if id =~ /^notif_(.+)$/
    item = Item.new({
      id: id,
      info: :notification,
      data: {
        user: graph_item["from"]["name"],
        content: graph_item["title"],
      },
      date: graph_item["created_time"],
      link: graph_item["link"]
    })
  else
    content = 
      case graph_item["type"]
      when "status"
        if graph_item["message"].nil?
          graph_item["story"]
        else
          graph_item["message"]
        end
      when "photo"
        "share a photo: #{graph_item['message'] || graph_item['link']}"
      when "checkin"
        "checkin"
      when "video"
        "share a video: #{graph_item['message']}"
      when "link"
        "#{graph_item["message"]} #{graph_item["link"]} #{graph_item["name"]}"
      end
    
    link = 
      (graph_item["actions"] && graph_item["actions"].first["link"]) ||
      graph_item["link"]
    
    likes = graph_item["likes"] && graph_item["likes"]["data"] || []
    comments = graph_item["comments"] && graph_item["comments"]["data"] || []

    like_count = likes.size
    comment_count = comments.size

    item = Item.new({
      id: graph_item["id"],
      info: :feed,
      data: {
        type: graph_item["type"],
        user: graph_item["from"]["name"],
        content: content,
        picture: graph_item["picture"],
        link: link,
        like_count: like_count,
        likes: likes, 
        comment_count: comment_count,
        comments: comments,
        date: DateTime.parse(graph_item["created_time"]),
      },
      date: graph_item["created_time"],
    })
  end
  item.raw = graph_item
  return item
end