Sha256: 3355671e398cf3a3f0ccacde406a94ba1d468c59728953e4458a51c345cf7b29

Contents?: true

Size: 1.31 KB

Versions: 21

Compression:

Stored size: 1.31 KB

Contents

require_dependency "writefully/application_controller"

module Writefully
  class HooksController < ApplicationController
    respond_to :json
    before_filter :check_signature

    HMAC_DIGEST = OpenSSL::Digest::Digest.new('sha1')

    class InvalidSignature < StandardError; end

    def create
      self.__send__ request.headers["X-Github-Event"].to_sym
      head :ok
    end

    def ping
    end

    def push
      Writefully.add_job :handyman, { task: :synchronize, 
                                      site_slug: body.repository.name } if branch_match?
    end

    def member
      authorship = Authorship.find_by_uid(body.member.id)
      unless authorship
        Authorship.create_from_data(body.member)
      end
    end

  protected

    def branch_match?
      Site.where(slug: body.repository.name).first.branch == body.ref.split('/').last
    end

    def body
      @_body ||= Hashie::Mash.new(JSON.parse(request.body.read))
    end

    def check_signature
      unless signature == request.headers["X-Hub-Signature"]
        raise InvalidSignature, "Invalid signature" 
      end
    end

    def signature
      'sha1=' + OpenSSL::HMAC.hexdigest(HMAC_DIGEST, 
                                        Writefully.options[:hook_secret], 
                                        request.body.read)
    end
  end
end

Version data entries

21 entries across 21 versions & 1 rubygems

Version Path
writefully-0.7.1 app/controllers/writefully/hooks_controller.rb
writefully-0.6.12 app/controllers/writefully/hooks_controller.rb
writefully-0.6.11 app/controllers/writefully/hooks_controller.rb
writefully-0.6.10 app/controllers/writefully/hooks_controller.rb
writefully-0.6.9 app/controllers/writefully/hooks_controller.rb
writefully-0.6.7 app/controllers/writefully/hooks_controller.rb
writefully-0.6.6 app/controllers/writefully/hooks_controller.rb
writefully-0.6.5 app/controllers/writefully/hooks_controller.rb
writefully-0.6.4 app/controllers/writefully/hooks_controller.rb
writefully-0.6.3 app/controllers/writefully/hooks_controller.rb
writefully-0.6.2 app/controllers/writefully/hooks_controller.rb
writefully-0.5.1 app/controllers/writefully/hooks_controller.rb
writefully-0.5.0 app/controllers/writefully/hooks_controller.rb
writefully-0.4.10 app/controllers/writefully/hooks_controller.rb
writefully-0.4.8 app/controllers/writefully/hooks_controller.rb
writefully-0.4.7 app/controllers/writefully/hooks_controller.rb
writefully-0.4.6 app/controllers/writefully/hooks_controller.rb
writefully-0.4.5 app/controllers/writefully/hooks_controller.rb
writefully-0.4.4 app/controllers/writefully/hooks_controller.rb
writefully-0.4.2 app/controllers/writefully/hooks_controller.rb