Class: ActivityMapper::ServiceModule

Inherits:
Object
  • Object
show all
Defined in:
lib/activity_mapper/service_module.rb

Constant Summary collapse

COMMON_DIRECTIVES =
['user', 'users', 'person', 'people', 'traveller', 'in', 'profile', 'profiles']

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(profile) ⇒ ServiceModule

Returns a new instance of ServiceModule.



8
9
10
# File 'lib/activity_mapper/service_module.rb', line 8

def initialize(profile)
  @profile = profile
end

Class Method Details

.accepts?(url) ⇒ Boolean

Based on a profile url, decide if I need to be responsible for this

Returns:

  • (Boolean)


43
44
45
46
47
48
49
50
51
52
53
# File 'lib/activity_mapper/service_module.rb', line 43

def self.accepts?(url)  
  u = uri(url)
  return false unless u
  
  self::ACCEPTED_HOSTS.each do |host_re|
    if (u.host =~ host_re)
      return true
    end
  end
  false
end

.all_accepted_hostsObject



12
13
14
15
16
17
18
# File 'lib/activity_mapper/service_module.rb', line 12

def self.all_accepted_hosts
  host_expressions = []
  self.subclasses.each do |service_module_klass|
    host_expressions << eval("#{service_module_klass.to_s}::ACCEPTED_HOSTS")
  end
  host_expressions.flatten!
end

.detect_username(profiles) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/activity_mapper/service_module.rb', line 55

def self.detect_username(profiles)
  usernames = {}
  profiles.each do |me_url|
    username = username_from_url(me_url)
    if username
      usernames[username] ||= 0
      usernames[username] += 1
    end
  end

  usernames = usernames.to_a
  return [] if usernames.blank?

  usernames.sort! { |b,a| a.last <=> b.last }

  # Factor in the shortest username if top 2 matches have the same score
  top_score = usernames[0].last
  if (usernames[1] && usernames[1].last == top_score)
    usernames = [usernames[0], usernames[1]]
    usernames.sort! { |a,b| a.first.size <=> b.first.size }
  end

  usernames.collect(&:first)
end

.klass_for(url) ⇒ Object



20
21
22
23
24
25
26
27
# File 'lib/activity_mapper/service_module.rb', line 20

def self.klass_for(url)
  self.subclasses.each do |service_module_klass|
    if service_module_klass.accepts?(url)
      return service_module_klass
    end
  end
  nil
end

.subclassesObject



30
31
32
33
34
35
36
37
38
39
40
# File 'lib/activity_mapper/service_module.rb', line 30

def self.subclasses
  class_hash = {}
  ObjectSpace.each_object do |obj|
    if Class == obj.class
      if obj.ancestors.include? self
        class_hash[obj] = true
      end
    end
  end
  class_hash.keys.reject { |ch| ch == self }
end

Instance Method Details

#aggregate_activity!(options = {}) ⇒ Object

This is called as often as possible, this is to passively aggregate the latest activity



86
# File 'lib/activity_mapper/service_module.rb', line 86

def aggregate_activity!(options = {}); raise "Method not implemented"; end

#create_or_update_summary!(options = {}) ⇒ Object

Update the long term data (eg top used software, biography)



83
# File 'lib/activity_mapper/service_module.rb', line 83

def create_or_update_summary!(options = {}); raise "Method not implemented"; end

#deep_analysis_on(activity_object) ⇒ Object



109
110
111
112
113
114
115
116
117
118
# File 'lib/activity_mapper/service_module.rb', line 109

def deep_analysis_on(activity_object)
  return unless activity_object.deeply_analyzed_at.blank?
  return unless activity_object.body.size > 100
  unless activity_object.body.blank?
    @extractor = ZemantaExtractor.new
    tags = @extractor.extract_tags(activity_object.body)
    activity_object.tag_list = activity_object.tag_list + tags
    activity_object.save
  end
end

#shallow_analysis_on(activity_object) ⇒ Object

Analyze an individual activity object (eg photo, bookmark, book, software, slide…)



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/activity_mapper/service_module.rb', line 92

def shallow_analysis_on(activity_object)
  return unless activity_object.shallowly_analyzed_at.blank?

  # Extract social connections from body
  #unless activity_object.body.blank?
  #  SocialConnection.generate_from_nanoformats(@profile, activity_object.body)
  #end

  # Extract tags from title
  unless activity_object.title.blank?
    tags = Linguistics::Tagger.keywords_for_caption(activity_object.title)
    activity_object.tag_list = (activity_object.tag_list || []) + tags
    activity_object.shallowly_analyzed_at = Time.now
    activity_object.save
  end
end