Class: ADNCV::Data

Inherits:
Object
  • Object
show all
Defined in:
lib/ADNCV/data.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#all_clientsObject (readonly)

Returns the value of attribute all_clients.



5
6
7
# File 'lib/ADNCV/data.rb', line 5

def all_clients
  @all_clients
end

#all_directedObject (readonly)

Returns the value of attribute all_directed.



5
6
7
# File 'lib/ADNCV/data.rb', line 5

def all_directed
  @all_directed
end

Returns the value of attribute all_links.



5
6
7
# File 'lib/ADNCV/data.rb', line 5

def all_links
  @all_links
end

#all_mentionedObject (readonly)

Returns the value of attribute all_mentioned.



5
6
7
# File 'lib/ADNCV/data.rb', line 5

def all_mentioned
  @all_mentioned
end

#been_repliedObject (readonly)

Returns the value of attribute been_replied.



5
6
7
# File 'lib/ADNCV/data.rb', line 5

def been_replied
  @been_replied
end

#clientsObject (readonly)

Returns the value of attribute clients.



5
6
7
# File 'lib/ADNCV/data.rb', line 5

def clients
  @clients
end

#countObject (readonly)

Returns the value of attribute count.



5
6
7
# File 'lib/ADNCV/data.rb', line 5

def count
  @count
end

#directed_usersObject (readonly)

Returns the value of attribute directed_users.



5
6
7
# File 'lib/ADNCV/data.rb', line 5

def directed_users
  @directed_users
end

#export_pathObject

Returns the value of attribute export_path.



6
7
8
# File 'lib/ADNCV/data.rb', line 6

def export_path
  @export_path
end

#filenameObject (readonly)

Returns the value of attribute filename.



5
6
7
# File 'lib/ADNCV/data.rb', line 5

def filename
  @filename
end

#freqObject (readonly)

Returns the value of attribute freq.



5
6
7
# File 'lib/ADNCV/data.rb', line 5

def freq
  @freq
end

#leadingsObject (readonly)

Returns the value of attribute leadings.



5
6
7
# File 'lib/ADNCV/data.rb', line 5

def leadings
  @leadings
end

#mentionsObject (readonly)

Returns the value of attribute mentions.



5
6
7
# File 'lib/ADNCV/data.rb', line 5

def mentions
  @mentions
end

#mentions_not_directedObject (readonly)

Returns the value of attribute mentions_not_directed.



5
6
7
# File 'lib/ADNCV/data.rb', line 5

def mentions_not_directed
  @mentions_not_directed
end

#mentions_not_repliesObject (readonly)

Returns the value of attribute mentions_not_replies.



5
6
7
# File 'lib/ADNCV/data.rb', line 5

def mentions_not_replies
  @mentions_not_replies
end

#namesObject (readonly)

Returns the value of attribute names.



5
6
7
# File 'lib/ADNCV/data.rb', line 5

def names
  @names
end

#repliesObject (readonly)

Returns the value of attribute replies.



5
6
7
# File 'lib/ADNCV/data.rb', line 5

def replies
  @replies
end

#repostsObject (readonly)

Returns the value of attribute reposts.



5
6
7
# File 'lib/ADNCV/data.rb', line 5

def reposts
  @reposts
end

#sourcesObject (readonly)

Returns the value of attribute sources.



5
6
7
# File 'lib/ADNCV/data.rb', line 5

def sources
  @sources
end

#starsObject (readonly)

Returns the value of attribute stars.



5
6
7
# File 'lib/ADNCV/data.rb', line 5

def stars
  @stars
end

#typeObject (readonly)

Returns the value of attribute type.



5
6
7
# File 'lib/ADNCV/data.rb', line 5

def type
  @type
end

Returns the value of attribute with_links.



5
6
7
# File 'lib/ADNCV/data.rb', line 5

def with_links
  @with_links
end

#without_mentionsObject (readonly)

Returns the value of attribute without_mentions.



5
6
7
# File 'lib/ADNCV/data.rb', line 5

def without_mentions
  @without_mentions
end

Instance Method Details

#export(options) ⇒ Object



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/ADNCV/data.rb', line 103

def export(options)
  root = if options["path"]
    options["path"]
  else
    Dir.home
  end

  @export_path = "#{root}/adncv_export.json"

  export = {
    meta: {
      created_at: Time.now,
      from_file: @filename,
      with_version: VERSION
    },
    data: {
      posts: {
        total: @count,
        data: [{
          without_mentions: @without_mentions,
          directed: @leadings,
          directed_to_unique_users: @directed_users.size,
          with_mentions_not_directed: @mentions_not_directed,
          with_mentions_are_replies: @replies,
          with_mentions_are_not_replies: @mentions_not_replies,
          with_links: @with_links,
          have_been_reposted: @reposts,
          have_been_starred: @stars,
          have_been_replied: @been_replied,
          posts_per_month: @freq
        }]
      },
      users: {
        total: @all_mentioned.size,
        data: @all_mentioned.reverse
      },
      clients: {
        total: @all_clients.size,
        data: @all_clients.reverse
      },
      links: {
        total: @with_links,
        data: @all_links
      }
    }
  }

  File.write(@export_path, JSON.pretty_generate(export))
end

#extract(file, options = {}) ⇒ Object

end



12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/ADNCV/data.rb', line 12

def extract(file, options = {})
  @filename = file
  @decoded = JSON.parse(File.read(file))
  
  if options["messages"]
    @type = :messages
    extract_messages(file)
  else
    @type = :posts
    extract_posts(file)
  end
end

#extract_messages(file) ⇒ Object



25
26
27
28
29
30
31
32
33
34
# File 'lib/ADNCV/data.rb', line 25

def extract_messages(file)
  messages = Hash.new(0)
  @decoded.each do |message|
    messages[message["channel_id"]] += 1
  end
  puts JSON.pretty_generate(messages)

  #
  exit
end

#extract_posts(file) ⇒ Object



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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/ADNCV/data.rb', line 36

def extract_posts(file)
  @count = @decoded.size
  links = []
  @mentions = 0
  @leadings = 0
  @replies = 0
  @with_links = 0
  @reposts = 0
  @stars = 0
  @been_replied = 0
  mentioned = Hash.new(0) 
  directed = Hash.new(0)
  is_reply = Hash.new(0)
  @clients = Hash.new(0)
  @freq = Hash.new(0)

  @decoded.each do |post|
    @clients[post["source"]["name"]] += 1
    m = post["entities"]["mentions"]
    l = post["entities"]["links"]
    unless m.empty?
      @mentions += 1
      first = m[0]["name"]
      if m[0]["is_leading"] == true
        @leadings += 1
        directed[first] += 1
        unless post["reply_to"].nil?
          @replies += 1
          is_reply[first] += 1
        end
      end
      m.each do |obj|
        mentioned[obj["name"]] += 1
      end
    end
    unless l.empty?
      @with_links += 1
      l.each do |link|
        links << link['url']
      end
    end
    unless post["num_reposts"].nil?
      @reposts += post["num_reposts"]
    end
    unless post["num_stars"].nil?
      @stars += post["num_stars"]
    end
    unless post["num_replies"].nil?
      @been_replied += post["num_replies"]
    end
    dd = Date.parse(post["created_at"])
    @freq[[dd.year, dd.month]] += 1
  end

  @all_directed = directed.sort_by {|k,v| v}
  @all_clients = @clients.sort_by {|k,v| v}
  @all_mentioned = mentioned.sort_by {|k,v| v}.uniq
  @all_links = links.uniq.sort
  @names = @all_mentioned.map {|k,v| "@#{k} (#{v})"}
  @sources = @all_clients.map {|k,v| "#{k} (#{v})"}
  @directed_users = @all_directed.uniq.map {|k,v| "@#{k} (#{v})"}

  @without_mentions = count - @mentions
  @mentions_not_directed = @mentions - @leadings
  @mentions_not_replies = @mentions - @replies
end