diff --git a/plugin/url_handler/github_commit_handler.rb b/plugin/url_handler/github_commit_handler.rb new file mode 100644 index 0000000..dd8748f --- /dev/null +++ b/plugin/url_handler/github_commit_handler.rb @@ -0,0 +1,51 @@ +module UrlHandler + class GithubCommitHandler + include Common + + REGEXP = %r{https?://(?:www\.)?github\.com/([^/]+?)/([^/]+)/commit/(\w+)} + + attr_accessor :url, :username, :repository, :sha + + def self.match?(string) + REGEXP.match(string) + end + + def initialize(url) + self.url = url + end + + def username + @username ||= REGEXP.match(url)[1] + end + + def repository + @repository ||= REGEXP.match(url)[2] + end + + def sha + @sha ||= REGEXP.match(url)[3] + end + + def api_url + @api_url ||= "https://api.github.com/repos/#{username}/#{repository}/git/commits/#{sha}" + end + + def api_data + @api_data ||= get_json_data(api_url) + end + + def message + api_data['message'].split("\n\n").first + end + + def author + api_data['author']['name'] + end + + def info + "github commit: \2#{username}/#{repository}\2 \2#{sha[0,7]}\2 - #{message}" + end + end +end + + diff --git a/plugin/url_handler_cinch_plugin.rb b/plugin/url_handler_cinch_plugin.rb index 697ba56..4196386 100644 --- a/plugin/url_handler_cinch_plugin.rb +++ b/plugin/url_handler_cinch_plugin.rb @@ -4,6 +4,7 @@ require_relative 'url_handler/common' require_relative 'url_handler/github_issue_handler' require_relative 'url_handler/github_pull_request_handler' +require_relative 'url_handler/github_commit_handler' require_relative 'url_handler/github_repo_handler' require_relative 'url_handler/gist_handler' require_relative 'url_handler/twitter_user_handler' diff --git a/spec/fixtures/vcr_cassettes/url_handler_github_commit.yml b/spec/fixtures/vcr_cassettes/url_handler_github_commit.yml new file mode 100644 index 0000000..c7e8b13 --- /dev/null +++ b/spec/fixtures/vcr_cassettes/url_handler_github_commit.yml @@ -0,0 +1,86 @@ +--- +http_interactions: +- request: + method: get + uri: https://api.github.com/repos/TampaRuby/turbot/git/commits/94ab61b10ff94220ca1f7a3fff46b55bb92b9e20 + body: + encoding: US-ASCII + string: '' + headers: + Accept-Encoding: + - gzip,deflate,identity + Accept: + - '*/*' + User-Agent: + - Mechanize/2.5.1 Ruby/2.0.0p247 (http://github.com/tenderlove/mechanize/) + Accept-Charset: + - ISO-8859-1,utf-8;q=0.7,*;q=0.7 + Accept-Language: + - en-us,en;q=0.5 + Host: + - api.github.com + Connection: + - keep-alive + Keep-Alive: + - 300 + response: + status: + code: 200 + message: OK + headers: + Server: + - GitHub.com + Date: + - Tue, 24 Sep 2013 14:43:59 GMT + Content-Type: + - application/json; charset=utf-8 + Transfer-Encoding: + - chunked + Status: + - 200 OK + X-Ratelimit-Limit: + - '60' + X-Ratelimit-Remaining: + - '57' + X-Ratelimit-Reset: + - '1380036788' + Cache-Control: + - public, max-age=60, s-maxage=60 + Last-Modified: + - Wed, 19 Jun 2013 13:35:12 GMT + Etag: + - '"dc8212d2e664510b63cb21d2744a1d44"' + Vary: + - Accept + - Accept-Encoding + X-Github-Media-Type: + - github.beta + X-Content-Type-Options: + - nosniff + Access-Control-Allow-Credentials: + - 'true' + Access-Control-Expose-Headers: + - ETag, Link, X-RateLimit-Limit, X-RateLimit-Remaining, X-RateLimit-Reset, X-OAuth-Scopes, + X-Accepted-OAuth-Scopes + Access-Control-Allow-Origin: + - '*' + X-Github-Request-Id: + - B8B112A2:6827:11870C5:5241A52F + Content-Encoding: + - gzip + body: + encoding: ASCII-8BIT + string: !binary |- + H4sIAAAAAAAAA7WTSW/bMBCF/wrBAj05FheRWk7tqWnRAoWRXpIUwVAaWkqs + pSR1KAz/91JwBKToxQt6JWfm43tvuKe+AVrSIgWjueHM2iIVglXAbQbSWptq + o5QxhTAFCkZXdHK72NCEMPoySWBs19s2NJNZV0OXOBwHn9xBN8JmMr+TMDkz + hCRWJPG6a4NPzkA1ods9/c17w/qHcgEBptAMjpZ72kOHUdeXoYfQQB+FYgft + LPX59Wj9DNWLH3r+ocNZbCypIcxNgnF5w/QNL+64LKUqubinhxU9PijgfwME + h5G/X0LMs1RxXTBt6jTngqEQGlSmpAXNC6FTlWnN4IoQZ2CM8FRQ9KBD72E7 + u/QN3RbJOO12xOGvCX0g72RGrBs64l69TUZwHp+OKc/R+8f+sf9Y1+TH5iuZ + L9t+S+zgyKc23E6GfPY+TiLvyfd57uY410eFsRb74Gn5sNgjmMVKpEylGpWq + pNBMW8gRTWaE5oxxbZi6yp5lA89AXbjjJxMOq0U/r5jlIJXgAiWzuYw/Ma+K + 3AgGitfGZjYuToVXrMei/wzUhfpPJhx+Hv4AJVWyfuUEAAA= + http_version: + recorded_at: Tue, 24 Sep 2013 14:43:59 GMT +recorded_with: VCR 2.4.0 diff --git a/spec/plugin/url_handler_spec.rb b/spec/plugin/url_handler_spec.rb index a35ff1b..98b22eb 100644 --- a/spec/plugin/url_handler_spec.rb +++ b/spec/plugin/url_handler_spec.rb @@ -120,6 +120,17 @@ end end + context "when a github commit url is given" do + let(:url) {'https://github.com/TampaRuby/turbot/commit/94ab61b10ff94220ca1f7a3fff46b55bb92b9e20'} + let(:message) {double('message', :raw => url)} + let(:reply_text) {"github commit: \x02TampaRuby/turbot\x02 \x0294ab61b\x02 - Merge pull request #37 from rjackson/parse_github_urls"} + + it "should reply." do + message.should_receive(:reply).with(reply_text) + VCR.use_cassette('url_handler_github_commit') { plugin.listen(message) } + end + end + context "when an image url is given" do let(:url) {'http://farm9.staticflickr.com/8013/7619514930_4a1ed85893_z.jpg'} let(:message) {double('message', :raw => url)}