Class: TentD::Model::AppAuthorization

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

Class Method Summary collapse

Instance Method Summary collapse

Methods included from RandomPublicId

included

Class Method Details

.create_from_params(data) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
# File 'lib/tentd/model/app_authorization.rb', line 54

def self.create_from_params(data)
  authorization = create(data)

  if data[:notification_url]
    data[:post_types].each do |type|
      authorization.notification_subscriptions.create(:type => type)
    end
  end

  authorization
end

.follow_url(entity) ⇒ Object



66
67
68
69
70
71
72
73
# File 'lib/tentd/model/app_authorization.rb', line 66

def self.follow_url(entity)
  app_auth = all(:follow_url.not => nil).find { |a| a.scopes.map(&:to_sym).include?(:follow_ui) }
  return unless app_auth
  uri = URI(app_auth.follow_url)
  query = "entity=#{URI.encode_www_form_component(entity)}"
  uri.query ? uri.query += "&#{query}" : uri.query = query
  uri.to_s
end

.public_attributesObject



50
51
52
# File 'lib/tentd/model/app_authorization.rb', line 50

def self.public_attributes
  [:post_types, :profile_info_types, :scopes, :notification_url]
end

Instance Method Details

#as_json(options = {}) ⇒ Object



102
103
104
105
106
107
108
109
110
# File 'lib/tentd/model/app_authorization.rb', line 102

def as_json(options = {})
  attributes = super

  if options[:authorization_token]
    attributes[:token_code] = token_code
  end

  attributes
end

#auth_detailsObject



38
39
40
# File 'lib/tentd/model/app_authorization.rb', line 38

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

#notification_pathObject



46
47
48
# File 'lib/tentd/model/app_authorization.rb', line 46

def notification_path
  notification_url
end

#notification_serversObject



42
43
44
# File 'lib/tentd/model/app_authorization.rb', line 42

def notification_servers
  nil
end

#token_exchange!Object



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

def token_exchange!
  update(:token_code => SecureRandom.hex(16))
  {
    :access_token => mac_key_id,
    :mac_key => mac_key,
    :mac_algorithm => mac_algorithm,
    :token_type => 'mac'
  }
end

#update_from_params(data) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/tentd/model/app_authorization.rb', line 75

def update_from_params(data)
  _post_types = post_types

  saved = update(data.slice(:post_types, :profile_info_types, :scopes, :notification_url))

  if saved && data[:post_types] && data[:post_types] != _post_types
    notification_subscriptions.all(:type_base.not => post_types.map { |t| TentType.new(t).base }).destroy

    data[:post_types].map { |t| TentType.new(t) }.each do |type|
      next if notification_subscriptions.first(:type_base => type.base)
      notification_subscriptions.create(:type_base => type.base, :type_version => type.version, :type_view => type.view)
    end
  end

  saved
end