class Fontina::Fetchers::HTTP
Public Instance Methods
fetch(location)
click to toggle source
Calls superclass method
# File lib/fontina/fetchers/http.rb, line 8 def fetch(location) return super unless location.is_a? URI::HTTP response = connection.get(location) Result[response.body, get_filename(response), get_mime_type(response)] end
Private Instance Methods
connection()
click to toggle source
# File lib/fontina/fetchers/http.rb, line 18 def connection @connection ||= Faraday.new do |c| c.use FaradayMiddleware::FollowRedirects, limit: 2 c.use Faraday::Response::RaiseError c.adapter Faraday.default_adapter end end
get_attachment_filename(response)
click to toggle source
# File lib/fontina/fetchers/http.rb, line 30 def get_attachment_filename(response) disposition = response.headers[:content_disposition] and match = disposition.match(/^attachment; *filename=(?<q>"?)(?<filename>(\w| |\.|\\")*)\k<q>$/) and match[:filename].gsub('\"', '"') end
get_filename(response)
click to toggle source
# File lib/fontina/fetchers/http.rb, line 26 def get_filename(response) get_attachment_filename(response) || File.basename(URI.decode response.env.url.path) end
get_mime_type(response)
click to toggle source
# File lib/fontina/fetchers/http.rb, line 36 def get_mime_type(response) type = response.headers[:content_type] and match = type.match(/^(?<type>[\w-]+\/[\w-]+)(;.*)?$/) and match[:type] end