diff --git a/src/app/models/participant.rb b/src/app/models/participant.rb index ffb4b22a..c7d6737b 100644 --- a/src/app/models/participant.rb +++ b/src/app/models/participant.rb @@ -1,3 +1,6 @@ +require 'nokogiri' +require 'open-uri' + class Participant < ActiveRecord::Base has_many :sessions has_many :attendances @@ -9,6 +12,8 @@ class Participant < ActiveRecord::Base validates_presence_of :name validates_uniqueness_of :email, :case_sensitive => false, :allow_blank => true + before_save :slurp_github_og_info, if: :github_profile_username_changed? + acts_as_authentic do |config| config.crypto_provider = Authlogic::CryptoProviders::BCrypt config.require_password_confirmation = false @@ -47,6 +52,26 @@ def github_profile_url "https://github.com/#{self.github_profile_username}" end + def slurp_github_og_info + logger.debug "slurp github og info" + + return if self.github_profile_username.blank? + + begin + doc = Nokogiri::HTML(open(github_profile_url)) + + nodes = doc.xpath("//head/meta [@property='og:image']") + self.github_og_image = nodes.first.attributes['content'].value + + nodes = doc.xpath("//head/meta [@property='og:url']") + self.github_og_url = nodes.first.attributes['content'].value + + rescue StandardError => err + logger.error err.message + end + + end + end diff --git a/src/spec/models/participant_spec.rb b/src/spec/models/participant_spec.rb index 95a950c5..b6019b3e 100644 --- a/src/spec/models/participant_spec.rb +++ b/src/spec/models/participant_spec.rb @@ -8,6 +8,12 @@ it "can be constructed with a github username" do expect(luke.github_profile_url).to eq "https://github.com/look" end + + it "pulls github og image if the github username is changed" do + luke.github_profile_username = 'lof' + expect(luke).to receive(:slurp_github_og_info) + luke.save + end end describe "timeslot restrictions" do