Class: TentD::Model::Follower

Inherits:
Object
  • Object
show all
Includes:
DataMapper::Resource, Permissible, RandomPublicId, Serializable, UserScoped
Defined in:
lib/tentd/model/follower.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from UserScoped

included

Methods included from RandomPublicId

included

Methods included from Permissible

#assign_permissions, included, #permissions_json

Class Method Details

.create_follower(data, authorized_scopes = []) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
# File 'lib/tentd/model/follower.rb', line 38

def self.create_follower(data, authorized_scopes = [])
  if authorized_scopes.include?(:write_followers) && authorized_scopes.include?(:write_secrets)
    follower = create(data.slice(:entity, :groups, :public, :profile, :licenses, :notification_path, :mac_key_id, :mac_key, :mac_algorithm, :mac_timestamp_delta))
  else
    follower = create(data.slice('entity', 'licenses', 'profile', 'notification_path'))
  end
  (data.types || ['all']).each do |type_url|
    follower.notification_subscriptions.create(:type => type_url)
  end
  follower
end

.public_attributesObject



71
72
73
# File 'lib/tentd/model/follower.rb', line 71

def self.public_attributes
  [:entity]
end

.update_follower(id, data, authorized_scopes = []) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/tentd/model/follower.rb', line 50

def self.update_follower(id, data, authorized_scopes = [])
  follower = first(:id => id)
  return unless follower
  whitelist = ['licenses']
  if authorized_scopes.include?(:write_followers)
    whitelist.concat(['entity', 'profile', 'public', 'groups'])

    if authorized_scopes.include?(:write_secrets)
      whitelist.concat(['mac_key_id', 'mac_key', 'mac_algorithm', 'mac_timestamp_delta'])
    end
  end
  follower.update(data.slice(*whitelist))
  if data['types']
    follower.notification_subscriptions.destroy
    data['types'].each do |type_url|
      follower.notification_subscriptions.create(:type => type_url)
    end
  end
  follower
end

Instance Method Details

#as_json(options = {}) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/tentd/model/follower.rb', line 91

def as_json(options = {})
  attributes = super

  attributes.merge!(:profile => profile) if options[:app]

  if options[:app] || options[:self]
    types = notification_subscriptions.all.map { |s| s.type.uri }
    attributes.merge!(:licenses => licenses, :types => types)
  end

  attributes
end

#auth_detailsObject



87
88
89
# File 'lib/tentd/model/follower.rb', line 87

def auth_details
  attributes.slice(:mac_key_id, :mac_key, :mac_algorithm)
end

#core_profileObject



79
80
81
# File 'lib/tentd/model/follower.rb', line 79

def core_profile
  API::CoreProfileData.new(profile)
end

#notification_serversObject



83
84
85
# File 'lib/tentd/model/follower.rb', line 83

def notification_servers
  core_profile.servers
end

#permissible_foreign_keyObject



75
76
77
# File 'lib/tentd/model/follower.rb', line 75

def permissible_foreign_key
  :follower_access_id
end