Skip to content
This repository has been archived by the owner on Oct 13, 2020. It is now read-only.

Commit

Permalink
code from URL: fix GitHub integration; support HTTPS
Browse files Browse the repository at this point in the history
  • Loading branch information
mislav committed Feb 12, 2013
1 parent 69a2cff commit aa3633d
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 13 deletions.
3 changes: 3 additions & 0 deletions Gemfile
Expand Up @@ -13,6 +13,9 @@ gem 'dm-postgres-adapter'
gem 'dm-migrations'
gem 'dm-timestamps'

gem 'faraday', '~> 0.8.5'
gem 'faraday_middleware'

group :development do
gem 'rake'
gem 'rspec'
Expand Down
7 changes: 7 additions & 0 deletions Gemfile.lock
Expand Up @@ -18,7 +18,12 @@ GEM
dm-core (~> 1.2.0)
do_postgres (0.10.12)
data_objects (= 0.10.12)
faraday (0.8.5)
multipart-post (~> 1.1)
faraday_middleware (0.9.0)
faraday (>= 0.7.4, < 0.9)
haml (3.0.16)
multipart-post (1.1.5)
mustache (0.11.2)
nokogiri (1.4.3.1)
oniguruma (1.1.0)
Expand Down Expand Up @@ -51,6 +56,8 @@ DEPENDENCIES
dm-migrations
dm-postgres-adapter
dm-timestamps
faraday (~> 0.8.5)
faraday_middleware
haml
mustache
nokogiri
Expand Down
28 changes: 15 additions & 13 deletions code.rb
Expand Up @@ -3,15 +3,14 @@
require 'processor'
require 'digest/md5'
require 'pp'
require 'net/http'
# TODO: SSL support
# require 'net/https'
require 'faraday_middleware'
require 'dm-migrations'
require 'dm-timestamps'

Net::HTTPResponse.class_eval do
Faraday::Response.class_eval do
def html?
content_type == 'text/html' or content_type == 'application/xhtml+xml'
type = self['content-type'].to_s.split(';', 2).first
type == 'text/html' or type == 'application/xhtml+xml'
end
end

Expand All @@ -37,33 +36,36 @@ def extract_code_from_html(doc)
end

def get_http_response(url)
response = Net::HTTP.get_response URI.parse(url)
response.error! unless Net::HTTPSuccess === response
response
Faraday.new { |conn|
conn.response :follow_redirects
conn.response :raise_error
# conn.response :logger
conn.adapter Faraday.default_adapter
}.get(url)
end

def get_html_document(url)
response = get_http_response(url)
response.error! unless response.html?
raise "not HTML" unless response.html?
Nokogiri::HTML response.body
end

def get_raw_body(url)
response = get_http_response(url)
response.error! if response.html?
raise "was HTML, expected raw" if response.html?
response.body
end

def get_raw_url(url)
case url
when %r{^(https?://pastie.org)/pastes/(\w+)$}
url = "#{$1}/#{$2}.txt"
when %r{^(http://github.com/[^/]+/[^/]+)/blob/(.+)$}
when %r{^(https?://github.com/[^/]+/[^/]+)/blob/(.+)$}
url = "#{$1}/raw/#{$2}"
when %r{^(https?://gist.github.com)/\w+$}
when %r{^(https?://gist.github.com)/[\w.-]+/\w+$}
prefix = $1
doc = get_html_document(url)
if raw_link = doc.at('.file[id$=".rb"] .actions a[href^="/raw/"]')
if raw_link = doc.search('a[href$=".rb"]').find {|a| a['href'].include?('/raw/') }
raw_url = raw_link['href']
raw_url = prefix + raw_url unless raw_url.index('http') == 0
raw_url
Expand Down

0 comments on commit aa3633d

Please sign in to comment.