Sha256: ba459292965722739500acd415b7e4be7166b866b3f831f686cc4eae0f9d6cfc

Contents?: true

Size: 1.34 KB

Versions: 1

Compression:

Stored size: 1.34 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
    end

    def ping
      head :ok
    end

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

    def member
      authorship = Authorship.find_by_uid(body.member.id)
      unless authorship
        Authorship.create_from_data(body.member)
      end
      head :ok
    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

1 entries across 1 versions & 1 rubygems

Version Path
writefully-0.4.0 app/controllers/writefully/hooks_controller.rb