Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

code from URL: fix GitHub integration; support HTTPS

  • Loading branch information...
commit aa3633dfe5fa05f1913139f0687e08ec7578547a 1 parent 69a2cff
@mislav authored
Showing with 25 additions and 13 deletions.
  1. +3 −0  Gemfile
  2. +7 −0 Gemfile.lock
  3. +15 −13 code.rb
View
3  Gemfile
@@ -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'
View
7 Gemfile.lock
@@ -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)
@@ -51,6 +56,8 @@ DEPENDENCIES
dm-migrations
dm-postgres-adapter
dm-timestamps
+ faraday (~> 0.8.5)
+ faraday_middleware
haml
mustache
nokogiri
View
28 code.rb
@@ -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
@@ -37,20 +36,23 @@ 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
@@ -58,12 +60,12 @@ 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
Please sign in to comment.
Something went wrong with that request. Please try again.