Module: Itrigga::NetHelper
- Defined in:
- lib/itrigga/net_helper.rb
Class Method Summary collapse
- .discover_favicon_url(root_url, time_out = 5) ⇒ Object
- .discover_icon_hrefs(url) ⇒ Object
- .do_get(url, time_out = 5, retries_on_timeout = 5, max_redirects = 3) ⇒ Object
- .establish_session_if_needed(opts) ⇒ Object
-
.format_url_shortener_api_url(raw_url, config_hash) ⇒ Object
parses the :url template for the url shortener service.
- .get(options = {}) ⇒ Object
- .get_with_auth(opts) ⇒ Object
- .get_with_timeout(url, time_out) ⇒ Object
- .handle_response(url, resp, retries_on_timeout = 5, max_redirects = 3) ⇒ Object
- .ip_of(server) ⇒ Object
-
.parse_with_hpricot(html) ⇒ Object
Have difficulty mocking/stubbing the top-level Hpricot call This wrapper makes it testable.
- .query_string(h, opts = {:encode_values=>false, :skip_empty=>false}) ⇒ Object
- .raw_get(opts) ⇒ Object
-
.shorten_url(opts = {}) ⇒ Object
uses the url shortener service defined for this site by default will use bit.ly.
-
.transfer_file_scp(opts = {}) ⇒ Object
Transfer a file using scp between servers.
- .url_encode(s) ⇒ Object
Class Method Details
.discover_favicon_url(root_url, time_out = 5) ⇒ Object
127 128 129 130 131 132 133 134 |
# File 'lib/itrigga/net_helper.rb', line 127 def discover_favicon_url(root_url, time_out = 5) favicon_urls = ['favicon.ico'] + discover_icon_hrefs(root_url) favicon_urls.compact.each do |url| abs_url = URI.join(root_url, url).to_s return abs_url if get_with_timeout( abs_url, time_out ).is_a?(Net::HTTPSuccess) end nil end |
.discover_icon_hrefs(url) ⇒ Object
136 137 138 139 140 |
# File 'lib/itrigga/net_helper.rb', line 136 def discover_icon_hrefs(url) pd = open(url) { |f| parse_with_hpricot(f) } links = pd.search("//link[@rel='shortcut icon']") + pd.search("//link[@rel='icon']") links.map{ |link| link.attributes['href'] }.compact.to_a end |
.do_get(url, time_out = 5, retries_on_timeout = 5, max_redirects = 3) ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/itrigga/net_helper.rb', line 13 def do_get(url, time_out=5, retries_on_timeout=5, max_redirects = 3) retrycount = 0 resp = nil begin resp = get_with_timeout( url, time_out ) handle_response( url, resp, retries_on_timeout, max_redirects ) rescue Timeout::Error if(retrycount.to_i < retries_on_timeout.to_i) retrycount+=1 retry else raise IOError.new( "HTTP request timed out #{retrycount} times" ) end end end |
.establish_session_if_needed(opts) ⇒ Object
109 110 111 112 113 |
# File 'lib/itrigga/net_helper.rb', line 109 def establish_session_if_needed(opts) opts[:parsed_url] ||= URI.parse(opts[:url]) opts[:http_session] ||= Net::HTTP.new(opts[:parsed_url].host, opts[:parsed_url].port) opts[:http_session].use_ssl = true if opts[:parsed_url].scheme == 'https' end |
.format_url_shortener_api_url(raw_url, config_hash) ⇒ Object
parses the :url template for the url shortener service
182 183 184 185 |
# File 'lib/itrigga/net_helper.rb', line 182 def format_url_shortener_api_url(raw_url, config_hash) escaped_url = CGI::escape(raw_url) config_hash[:url].gsub("{{username}}",config_hash[:username] || "").gsub("{{api_key}}",config_hash[:api_key] || "").gsub("{{raw_url}}",escaped_url) end |
.get(options = {}) ⇒ Object
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/itrigga/net_helper.rb', line 54 def get( = {} ) opts = {:timeout=>5, :retries_on_timeout=>5, :max_redirects => 3, :headers=>{} }.merge() raise ArgumentError.new(":url is required" ) unless opts[:url] if (opts[:username] || opts[:headers]).to_s.empty? do_get(opts[:url], opts[:timeout], opts[:retries_on_timeout], opts[:max_redirects]) else retrycount = 0 resp = begin timeout( opts[:timeout] ) do raw_get(opts) end rescue TimeoutError if(retrycount < opts[:retries_on_timeout]) retrycount+=1 retry else raise IOError.new( "HTTP request timed out #{retrycount} times" ) end end resp end end |
.get_with_auth(opts) ⇒ Object
101 102 103 104 105 106 107 |
# File 'lib/itrigga/net_helper.rb', line 101 def get_with_auth( opts ) establish_session_if_needed(opts) req = Net::HTTP::Get.new(opts[:parsed_url].path) req.basic_auth( opts[:username], opts[:password] ) if opts[:username] resp = opts[:http_session].request(req, opts[:headers]) end |
.get_with_timeout(url, time_out) ⇒ Object
32 33 34 35 36 37 |
# File 'lib/itrigga/net_helper.rb', line 32 def get_with_timeout( url, time_out) resp = timeout(time_out) do Net::HTTP.get_response(URI.parse(url)) end resp end |
.handle_response(url, resp, retries_on_timeout = 5, max_redirects = 3) ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/itrigga/net_helper.rb', line 40 def handle_response( url, resp, retries_on_timeout=5, max_redirects = 3 ) if resp.is_a? Net::HTTPSuccess then resp.body elsif resp.is_a? Net::HTTPRedirection if max_redirects > 0 do_get( URI.parse(url).merge(resp['location']).to_s, retries_on_timeout, max_redirects - 1 ) else raise IOError.new("too many redirects!") end else resp.error! end end |
.ip_of(server) ⇒ Object
148 149 150 |
# File 'lib/itrigga/net_helper.rb', line 148 def ip_of(server) IPSocket::getaddress(server) end |
.parse_with_hpricot(html) ⇒ Object
Have difficulty mocking/stubbing the top-level Hpricot call This wrapper makes it testable
144 145 146 |
# File 'lib/itrigga/net_helper.rb', line 144 def parse_with_hpricot(html) Hpricot.parse(html) end |
.query_string(h, opts = {:encode_values=>false, :skip_empty=>false}) ⇒ Object
115 116 117 118 119 120 121 |
# File 'lib/itrigga/net_helper.rb', line 115 def query_string( h, opts={:encode_values=>false, :skip_empty=>false} ) params = [] h.each{ |k,v| params << "#{k.to_s}=#{ opts[:encode_values] ? url_encode(v) : v }" unless v.to_s.empty? && opts[:skip_empty] } params.sort.join('&') end |
.raw_get(opts) ⇒ Object
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/itrigga/net_helper.rb', line 79 def raw_get(opts) resp = nil establish_session_if_needed(opts) if opts[:username] resp = get_with_auth(opts) retries = 0 while resp.is_a? Net::HTTPRedirection do retries += 1 raise IOError.new( "HTTP request timed out #{retries} times" ) if retries > (opts[:max_redirects] || 3) resp = get_with_auth(opts.merge(:parsed_url=>URI.parse(resp['location']))) end resp.body else response = opts[:http_session].request_get(opts[:parsed_url].path, opts[:headers]) response.body end end |
.shorten_url(opts = {}) ⇒ Object
uses the url shortener service defined for this site by default will use bit.ly
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 |
# File 'lib/itrigga/net_helper.rb', line 154 def shorten_url(opts={}) opts[:attempts] ||= 0 return opts[:url] if opts[:attempts] > 3 api_url = self.format_url_shortener_api_url(opts[:url], opts[:config]) begin response = NetHelper.get :url => api_url rescue Exception => e sleep 10 unless defined?(RAILS_ENV) && RAILS_ENV == "test" return shorten_url(:url=>opts[:url], :attempts=>opts[:attempts]+1) end data = JSON.parse(response).recursive_symbolize_keys! if data[:status_code] == 200 return data[:data][:url] else # theres been a problem, try again if we have tries left sleep 10 unless defined?(RAILS_ENV) && RAILS_ENV == "test" return shorten_url(:url=>opts[:url], :attempts=>opts[:attempts]+1) end end |
.transfer_file_scp(opts = {}) ⇒ Object
Transfer a file using scp between servers
Options: :host - the target server name. Hash. Must have keys:
> :port - the port on which to connect
> :ssh_key_path - the absolute path to the ssh key file which it will use to connect
> :user - the user to connect as
> :host - the hostname to connect to
:target_path - the absolute path on the target server to put the file :file - the file to transfer. Must be an absolute path to the file
199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 |
# File 'lib/itrigga/net_helper.rb', line 199 def transfer_file_scp(opts = {}) raise ArgumentError.new("File '#{opts[:file]}' does not exist!") unless opts[:file] && File.exist?(opts[:file]) raise ArgumentError.new("host is required") unless opts[:host] raise ArgumentError.new("No target_path defined") unless opts[:target_path] #host = TRIGGA_CONFIG.hosts.detect{|host| host[:display_name].to_s == opts[:target].to_s} # now that everything checks out make sure the destination dir path exists. If not then create it command = "ssh -p#{opts[:host][:port]} -i #{opts[:host][:ssh_key_path]} #{opts[:host][:user]}@#{opts[:host][:host]} 'mkdir -p #{File.dirname(opts[:target_path])}'" `#{command}` # now transfer the file across command = "scp -P#{opts[:host][:port]} -i #{opts[:host][:ssh_key_path]} #{opts[:file]} #{opts[:host][:user]}@#{opts[:host][:host]}:#{opts[:target_path]}" `#{command}` end |
.url_encode(s) ⇒ Object
123 124 125 |
# File 'lib/itrigga/net_helper.rb', line 123 def url_encode( s ) URI.escape( s.to_s, Regexp.new("[^#{URI::PATTERN::UNRESERVED}]") ) end |