Class: BadgesEngine::Assertion

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/badges_engine/assertion.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.associate_user_class(user_class) ⇒ Object



25
26
27
# File 'app/models/badges_engine/assertion.rb', line 25

def associate_user_class(user_class)
  belongs_to :user, :class_name=>user_class.to_s, :foreign_key=>'user_id'
end

Instance Method Details

#bakeObject



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 'app/models/badges_engine/assertion.rb', line 44

def bake
  uri = URI.parse(BadgesEngine::Configuration.baker_url)
  http = Net::HTTP.new(uri.host, uri.port)
  path = "#{uri.path}?assertion=#{self.baking_callback_url}"
  headers = {'Content-Type'=>'application/json','Accept'=>'application/json'}
  logger.debug("request: #{uri.host}#{path}")
  response = http.get(path, headers)
  
  unless response.kind_of?(Net::HTTPSuccess)
    raise "Baking badge failed: Response was not a success:\n\t'#{response.inspect}'"
  end

  if !response || response.body.blank?
    raise "Baking badge failed: Response was blank:\n\t'#{response.inspect}'"
  end
  
  badges_response = ActiveSupport::JSON.decode(response.body)
  logger.debug("response: #{badges_response.inspect}")
  
  if badges_response['status'] == 'success'
    self.update_attribute(:is_baked, true)
  else
    raise "Baking badge failed:\n\tStatus:'#{badges_response['status']}'\n\tError:'#{badges_response['error']}'\n\tReason:'#{badges_response['reason']}'"
  end
end

#baking_callback_urlObject



39
40
41
42
# File 'app/models/badges_engine/assertion.rb', line 39

def baking_callback_url
  origin_uri = URI.parse(BadgesEngine::Configuration.issuer.origin)
  secret_assertion_url(:id=>self.id, :token=>self.token, :host=>origin_uri.host)
end

#open_badges_as_jsonObject



70
71
72
73
74
75
76
77
# File 'app/models/badges_engine/assertion.rb', line 70

def open_badges_as_json
  as_json(  :only=>[:evidence, :expires, :issued_on],
            :methods=>[:recipient],
            :include=>{ :badge=>{
                        :only=>[:version, :name, :image, :description, :criteria],
                        :methods=>:issuer} }
          )
end

#recipientObject



30
31
32
# File 'app/models/badges_engine/assertion.rb', line 30

def recipient
  self.user.try(:email)
end

#update_assertion?Boolean

Returns:

  • (Boolean)


35
36
37
# File 'app/models/badges_engine/assertion.rb', line 35

def update_assertion?
  persisted? && !is_baked?
end