class RemotePartial::ResourceManager
Attributes
criteria[R]
output_modifier[R]
url[R]
Public Class Methods
connection_settings(uri)
click to toggle source
# File lib/remote_partial/resource_manager.rb, line 42 def self.connection_settings(uri) [uri.host, uri.port] + proxy_settings end
get_page(url)
click to toggle source
# File lib/remote_partial/resource_manager.rb, line 9 def self.get_page(url) Nokogiri::HTML(get_raw(url)) end
get_raw(url)
click to toggle source
# File lib/remote_partial/resource_manager.rb, line 13 def self.get_raw(url) response = get_response(url) case response.code.to_i when ok_response_codes return response.body when redirect_response_codes get_raw(URI.parse(response['location'])) else raise response.inspect end rescue => exception # Do main exception raising outside of case statement so that SocketErrors are also handled raise RemotePartialRetrivalError.new(url, exception) end
get_response(url)
click to toggle source
# File lib/remote_partial/resource_manager.rb, line 28 def self.get_response(url) uri = URI.parse(url) http = Net::HTTP.new(*connection_settings(uri)) if uri.port == 443 http.use_ssl = true http.verify_mode = OpenSSL::SSL::VERIFY_NONE end request = Net::HTTP::Get.new(uri.request_uri) http.request(request) end
new(url, criteria = nil, &output_modifier)
click to toggle source
# File lib/remote_partial/resource_manager.rb, line 46 def initialize(url, criteria = nil, &output_modifier) @url = url @criteria = criteria @output_modifier = output_modifier end
Private Class Methods
ok_response_codes()
click to toggle source
# File lib/remote_partial/resource_manager.rb, line 94 def self.ok_response_codes 200..299 end
proxy_settings()
click to toggle source
# File lib/remote_partial/resource_manager.rb, line 102 def self.proxy_settings return [] unless ENV['http_proxy'] proxy = URI.parse(ENV['http_proxy']) [proxy.host, proxy.port, proxy.user, proxy.password] end
redirect_response_codes()
click to toggle source
# File lib/remote_partial/resource_manager.rb, line 98 def self.redirect_response_codes 300..399 end
Public Instance Methods
html()
click to toggle source
# File lib/remote_partial/resource_manager.rb, line 58 def html text = criteria ? get_part_of_page : get_whole_page output_modifier ? output_modifier.call(text) : text end
output_to(path)
click to toggle source
# File lib/remote_partial/resource_manager.rb, line 52 def output_to(path) @path = path ensure_output_folder_exists File.write(path, html) end
Private Instance Methods
encoding()
click to toggle source
# File lib/remote_partial/resource_manager.rb, line 74 def encoding "UTF-8" end
ensure_output_folder_exists()
click to toggle source
# File lib/remote_partial/resource_manager.rb, line 86 def ensure_output_folder_exists FileUtils.mkdir_p(output_folder) unless output_folder_exists? end
get_part_of_page()
click to toggle source
# File lib/remote_partial/resource_manager.rb, line 78 def get_part_of_page self.class.get_page(@url).search(criteria).to_s end
get_whole_page()
click to toggle source
# File lib/remote_partial/resource_manager.rb, line 65 def get_whole_page self.class.get_raw(@url).force_encoding(encoding).gsub(windows_bom_text, "") end
output_folder()
click to toggle source
# File lib/remote_partial/resource_manager.rb, line 82 def output_folder File.dirname(@path) end
output_folder_exists?()
click to toggle source
# File lib/remote_partial/resource_manager.rb, line 90 def output_folder_exists? Dir.exists?(output_folder) end
windows_bom_text()
click to toggle source
Windows editors add a BOM to the start of text files, and this needs to be removed
# File lib/remote_partial/resource_manager.rb, line 70 def windows_bom_text "\xEF\xBB\xBF".force_encoding(encoding) end