Skip to content

Commit

Permalink
Addes github og image avatar scraping when the user has a github user…
Browse files Browse the repository at this point in the history
…name
  • Loading branch information
caseyhelbling committed Apr 18, 2016
1 parent 887feb0 commit c3071d6
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
25 changes: 25 additions & 0 deletions src/app/models/participant.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
require 'nokogiri'
require 'open-uri'

class Participant < ActiveRecord::Base
has_many :sessions
has_many :attendances
Expand All @@ -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
Expand Down Expand Up @@ -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


6 changes: 6 additions & 0 deletions src/spec/models/participant_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit c3071d6

Please sign in to comment.