Check Ruby HTTP response for success - ruby
How does one properly check the response from Net::HTTP::Get (for example) for "success" (i.e., a 2xx return code)? The documentation seems to be sadly silent on this simple question.
I have:
response=Net::HTTP.new( host, port ).request my_get_request # details not important
After a bunch of Googling and near-random typing, I finally determined that this works:
response.class < Net::HTTPSuccess
Is that actually the canonical way to do it?
For Net::HTTP, yes, checking the class of the response object is the way to do it. Using kind_of? (aliased also as is_a?) is a bit clearer (but functionally equivalent to using <):
response.kind_of? Net::HTTPSuccess
Calling value on response will also raise a Net::HTTPError if the status code was not a successful one (what a poorly named method…).
If you can, you may want to consider using a gem instead of Net::HTTP, as they often offer better APIs and performance. Typhoeus and HTTParty are two good ones, among others.
You can take advantage of Ruby's case statement which idiomatically performs class comparisons, thanks to its use of === under the hood.
Here's an example from a JSON client that catches particular errors but otherwise just returns the server's message:
case response
when Net::HTTPSuccess
JSON.parse response.body
when Net::HTTPUnauthorized
{'error' => "#{response.message}: username and password set and correct?"}
when Net::HTTPServerError
{'error' => "#{response.message}: try again later?"}
else
{'error' => response.message}
end
Note above Net::HTTPResponse parent classes (e.g. Net::HTTPServerError) work too.
If all you're looking to grab is the HTTP status code of an external API or website, then try Net::HTTP.get_response.
Net::HTTP.get(url) returns a string. You won't be able to easily parse the header response from it:
url = URI('http://example.com')
string_response = Net::HTTP.get(url)
# => "<!doctype html>\n<html>\n<head>\n <title>Example Domain</title>\n\n <meta charset=\"utf-8\" />\n <meta http-equiv=\"Content-type\" content=\"text/html; charset=utf-8\" />\n <meta name=\"viewport\" content=\"width=device-width, initial-scale=1\" />\n <style type=\"text/css\">\n body {\n background-color: #f0f0f2;\n margin: 0;\n padding: 0;\n font-family: \"Open Sans\", \"Helvetica Neue\", Helvetica, Arial, sans-serif;\n \n }\n div {\n width: 600px;\n margin: 5em auto;\n padding: 50px;\n background-color: #fff;\n border-radius: 1em;\n }\n a:link, a:visited {\n color: #38488f;\n text-decoration: none;\n }\n #media (max-width: 700px) {\n body {\n background-color: #fff;\n }\n div {\n width: auto;\n margin: 0 auto;\n border-radius: 0;\n padding: 1em;\n }\n }\n </style> \n</head>\n\n<body>\n<div>\n <h1>Example Domain</h1>\n <p>This domain is established to be used for illustrative examples in documents. You may use this\n domain in examples without prior coordination or asking for permission.</p>\n <p>More information...</p>\n</div>\n</body>\n</html>\n"
string_response.class
# => String
string_response.kind_of? Net::HTTPSuccess
# => false
status_response = Net::HTTP.get_response(url)
# => #<Net::HTTPOK 200 OK readbody=true>
status_response.class
# => Net::HTTPOK
status_response.kind_of? Net::HTTPSuccess
# => true
Related
How to disable CSRF check in Spring WebService / Spring WS Security application?
I have an application that uses Spring WS and Spring WS security for creating SOAP web services. I am getting the following exception. <!DOCTYPE html> <html lang="en"> <head> <meta http-equiv="content-type" content="text/html; charset=utf-8"><script type="text/javascript">(window.NREUM||(NREUM={})).loader_config={licenseKey:"d89ec68706",applicationID:"74176617"};window.NREUM||(NREUM={}),__nr_require=function(e,n,t){function r(t){if(!n[t]){var i=n[t]={exports:{}};e[t][0].call(i.exports,function(n){var i=e[t][1][n];return r(i||n)},i,i.exports)}return n[t].exports}if("function"==typeof __nr_require)return __nr_require;for(var i=0;i<t.length;i++)r(t[i]);return r}({1:[function(e,n,t){function r(){}function i(e,n,t){return function(){return o(e,[u.now()].concat(f(arguments)),n?null:this,t),n?void 0:this}}var o=e("handle"),a=e(4),f=e(5),c=e("ee").get("tracer"),u=e("loader"),s=NREUM;"undefined"==typeof window.newrelic&&(newrelic=s);var p=["setPageViewName","setCustomAttribute","setErrorHandler","finished","addToTrace","inlineHit","addRelease"],l="api-",d=l+"ixn-";a(p,function(e,n){s[n]=i(l+n,!0,"api")}),s.addPageAction=i(l+"addPageAction",!0),s.setCurrentRouteName=i(l+"routeName",!0),n.exports=newrelic,s.interaction=function(){return(new r).get()};var m=r.prototype={createTracer:function(e,n){var t={},r=this,i="function"==typeof n;return o(d+"tracer",[u.now(),e,t],r),function(){if(c.emit((i?"":"no-")+"fn-start",[u.now(),r,i],t),i)try{return n.apply(this,arguments)}catch(e){throw c.emit("fn-err",[arguments,this,e],t),e}finally{c.emit("fn-end",[u.now()],t)}}}};a("actionText,setName,setAttribute,save,ignore,onEnd,getContext,end,get".split(","),function(e,n){m[n]=i(d+n)}),newrelic.noticeError=function(e,n){"string"==typeof e&&(e=new Error(e)),o("err",[e,u.now(),!1,n])}},{}],2:[function(e,n,t){function r(e,n){var t=e.getEntries();t.forEach(function(e){"first-paint"===e.name?c("timing",["fp",Math.floor(e.startTime)]):"first-contentful-paint"===e.name&&c("timing",["fcp",Math.floor(e.startTime)])})}function i(e,n){var t=e.getEntries();t.length>0&&c("lcp",[t[t.length-1]])}function o(e){if(e instanceof s&&!l){var n,t=Math.round(e.timeStamp);n=t>1e12?Date.now()-t:u.now()-t,l=!0,c("timing",["fi",t,{type:e.type,fid:n}])}}if(!("init"in NREUM&&"page_view_timing"in NREUM.init&&"enabled"in NREUM.init.page_view_timing&&NREUM.init.page_view_timing.enabled===!1)){var a,f,c=e("handle"),u=e("loader"),s=NREUM.o.EV;if("PerformanceObserver"in window&&"function"==typeof window.PerformanceObserver){a=new PerformanceObserver(r),f=new PerformanceObserver(i);try{a.observe({entryTypes:["paint"]}),f.observe({entryTypes:["largest-contentful-paint"]})}catch(p){}}if("addEventListener"in document){var l=!1,d=["click","keydown","mousedown","pointerdown","touchstart"];d.forEach(function(e){document.addEventListener(e,o,!1)})}}},{}],3:[function(e,n,t){function r(e,n){if(!i)return!1;if(e!==i)return!1;if(!n)return!0;if(!o)return!1;for(var t=o.split("."),r=n.split("."),a=0;a<r.length;a++)if(r[a]!==t[a])return!1;return!0}var i=null,o=null,a=/Version\/(\S+)\s+Safari/;if(navigator.userAgent){var f=navigator.userAgent,c=f.match(a);c&&f.indexOf("Chrome")===-1&&f.indexOf("Chromium")===-1&&(i="Safari",o=c[1])}n.exports={agent:i,version:o,match:r}},{}],4:[function(e,n,t){function r(e,n){var t=[],r="",o=0;for(r in e)i.call(e,r)&&(t[o]=n(r,e[r]),o+=1);return t}var i=Object.prototype.hasOwnProperty;n.exports=r},{}],5:[function(e,n,t){function r(e,n,t){n||(n=0),"undefined"==typeof t&&(t=e?e.length:0);for(var r=-1,i=t-n||0,o=Array(i<0?0:i);++r<i;)o[r]=e[n+r];return o}n.exports=r},{}],6:[function(e,n,t){n.exports={exists:"undefined"!=typeof window.performance&&window.performance.timing&&"undefined"!=typeof window.performance.timing.navigationStart}},{}],ee:[function(e,n,t){function r(){}function i(e){function n(e){return e&&e instanceof r?e:e?c(e,f,o):o()}function t(t,r,i,o){if(!l.aborted||o){e&&e(t,r,i);for(var a=n(i),f=v(t),c=f.length,u=0;u<c;u++)f[u].apply(a,r);var p=s[y[t]];return p&&p.push([b,t,r,a]),a}}function d(e,n){h[e]=v(e).concat(n)}function m(e,n){var t=h[e];if(t)for(var r=0;r<t.length;r++)t[r]===n&&t.splice(r,1)}function v(e){return h[e]||[]}function g(e){return p[e]=p[e]||i(t)}function w(e,n){u(e,function(e,t){n=n||"feature",y[t]=n,n in s||(s[n]=[])})}var h={},y={},b={on:d,addEventListener:d,removeEventListener:m,emit:t,get:g,listeners:v,context:n,buffer:w,abort:a,aborted:!1};return b}function o(){return new r}function a(){(s.api||s.feature)&&(l.aborted=!0,s=l.backlog={})}var f="nr#context",c=e("gos"),u=e(4),s={},p={},l=n.exports=i();l.backlog=s},{}],gos:[function(e,n,t){function r(e,n,t){if(i.call(e,n))return e[n];var r=t();if(Object.defineProperty&&Object.keys)try{return Object.defineProperty(e,n,{value:r,writable:!0,enumerable:!1}),r}catch(o){}return e[n]=r,r}var i=Object.prototype.hasOwnProperty;n.exports=r},{}],handle:[function(e,n,t){function r(e,n,t,r){i.buffer([e],r),i.emit(e,n,t)}var i=e("ee").get("handle");n.exports=r,r.ee=i},{}],id:[function(e,n,t){function r(e){var n=typeof e;return!e||"object"!==n&&"function"!==n?-1:e===window?0:a(e,o,function(){return i++})}var i=1,o="nr#id",a=e("gos");n.exports=r},{}],loader:[function(e,n,t){function r(){if(!x++){var e=E.info=NREUM.info,n=d.getElementsByTagName("script")[0];if(setTimeout(s.abort,3e4),!(e&&e.licenseKey&&e.applicationID&&n))return s.abort();u(y,function(n,t){e[n]||(e[n]=t)}),c("mark",["onload",a()+E.offset],null,"api");var t=d.createElement("script");t.src="https://"+e.agent,n.parentNode.insertBefore(t,n)}}function i(){"complete"===d.readyState&&o()}function o(){c("mark",["domContent",a()+E.offset],null,"api")}function a(){return O.exists&&performance.now?Math.round(performance.now()):(f=Math.max((new Date).getTime(),f))-E.offset}var f=(new Date).getTime(),c=e("handle"),u=e(4),s=e("ee"),p=e(3),l=window,d=l.document,m="addEventListener",v="attachEvent",g=l.XMLHttpRequest,w=g&&g.prototype;NREUM.o={ST:setTimeout,SI:l.setImmediate,CT:clearTimeout,XHR:g,REQ:l.Request,EV:l.Event,PR:l.Promise,MO:l.MutationObserver};var h=""+location,y={beacon:"bam.nr-data.net",errorBeacon:"bam.nr-data.net",agent:"js-agent.newrelic.com/nr-1167.min.js"},b=g&&w&&w[m]&&!/CriOS/.test(navigator.userAgent),E=n.exports={offset:f,now:a,origin:h,features:{},xhrWrappable:b,userAgent:p};e(1),e(2),d[m]?(d[m]("DOMContentLoaded",o,!1),l[m]("load",r,!1)):(d[v]("onreadystatechange",i),l[v]("onload",r)),c("mark",["firstbyte",f],null,"api");var x=0,O=e(6)},{}],"wrap-function":[function(e,n,t){function r(e){return!(e&&e instanceof Function&&e.apply&&!e[a])}var i=e("ee"),o=e(5),a="nr#original",f=Object.prototype.hasOwnProperty,c=!1;n.exports=function(e,n){function t(e,n,t,i){function nrWrapper(){var r,a,f,c;try{a=this,r=o(arguments),f="function"==typeof t?t(r,a):t||{}}catch(u){l([u,"",[r,a,i],f])}s(n+"start",[r,a,i],f);try{return c=e.apply(a,r)}catch(p){throw s(n+"err",[r,a,p],f),p}finally{s(n+"end",[r,a,c],f)}}return r(e)?e:(n||(n=""),nrWrapper[a]=e,p(e,nrWrapper),nrWrapper)}function u(e,n,i,o){i||(i="");var a,f,c,u="-"===i.charAt(0);for(c=0;c<n.length;c++)f=n[c],a=e[f],r(a)||(e[f]=t(a,u?f+i:i,o,f))}function s(t,r,i){if(!c||n){var o=c;c=!0;try{e.emit(t,r,i,n)}catch(a){l([a,t,r,i])}c=o}}function p(e,n){if(Object.defineProperty&&Object.keys)try{var t=Object.keys(e);return t.forEach(function(t){Object.defineProperty(n,t,{get:function(){return e[t]},set:function(n){return e[t]=n,n}})}),n}catch(r){l([r])}for(var i in e)f.call(e,i)&&(n[i]=e[i]);return n}function l(n){try{e.emit("internal-error",n)}catch(t){}}return e||(e=i),t.inPlace=u,t.flag=a,t}},{}]},{},["loader"]);</script><script type="text/javascript">window.NREUM||(NREUM={});NREUM.info={"beacon":"bam.nr-data.net","queueTime":0,"licenseKey":"d89ec68706","agent":"","transactionName":"MldVZUJYCkoDABVeWwsdcUReWhBQDQ1OU14EXFBeHlQNXQYPBEBVF1cZUkNLAgMhEBNRYgxXQHxZXQBVBxQARVFLQkVeU1wXSj0VCFJD","applicationID":"74176617","errorBeacon":"bam.nr-data.net","applicationTime":5}</script> <meta name="robots" content="NONE,NOARCHIVE"> <title>403 Forbidden</title> <style type="text/css"> html * { padding:0; margin:0; } body * { padding:10px 20px; } body * * { padding:0; } body { font:small sans-serif; background:#eee; } body>div { border-bottom:1px solid #ddd; } h1 { font-weight:normal; margin-bottom:.4em; } h1 span { font-size:60%; color:#666; font-weight:normal; } #info { background:#f6f6f6; } #info ul { margin: 0.5em 4em; } #info p, #summary p { padding-top:10px; } #summary { background: #ffc; } #explanation { background:#eee; border-bottom: 0px none; } </style> </head> <body> <div id="summary"> <h1>Forbidden <span>(403)</span></h1> <p>CSRF verification failed. Request aborted.</p> <p>You are seeing this message because this site requires a CSRF cookie when submitting forms. This cookie is required for security reasons, to ensure that your browser is not being hijacked by third parties.</p> <p>If you have configured your browser to disable cookies, please re-enable them, at least for this site, or for 'same-origin' requests.</p> </div> <div id="explanation"> <p><small>More information is available with DEBUG=True.</small></p> </div> </body> </html> I'm using spring ws-security to send plain text username-password for authorization. How do I disable CSRF check in this application? This is my security-policy.xml. <xwss:SecurityConfiguration xmlns:xwss="http://java.sun.com/xml/ns/xwss/config"> <xwss:RequireUsernameToken passwordDigestRequired="false" nonceRequired="false" /> </xwss:SecurityConfiguration> Any help is greatly appreciated. Thanks in advance.
Here's how I do it, in pre Spring Security 4: Custom Request Matcher: #Component public class CsrfSecurityRequestMatcher implements RequestMatcher { private RegexRequestMatcher protectedMatcher = new RegexRequestMatcher("(/path_with_csrf/.*)", null); #Override public boolean matches(HttpServletRequest request) { return protectedMatcher.matches(request); } } Security config: <security:csrf request-matcher-ref="csrfSecurityRequestMatcher" />
Prevent js-beautify from adding extra whitespace / empty lines
js-beautify (used under VSCode) annoys me by putting extra lines after comments: My sample.scss /* a fancy comment */ .foo-bars { background: $gray; display: block; width: 26px !important; } ...becomes... /* a fancy comment */ <-- annoying empty line inserted .foo-bars { background: $gray; display: block; <--- (this is fine. I like it being preserved) width: 26px !important; } This is my .jsbeautifyrc (verified to be effective, i.e. by testing with "indent_char": "#") { "indent_char": " ", "preserve_newlines": true, "max_preserve_newlines": 8, "keep-array-indentation": true, "break_chained_methods": false, "newline_between_rules": false, "selector_separator_newline": false, "end_with_newline": false } update: Affects /* block comments */ only, not // line comments.
It seems like that this should have been fixed (js-beautify#609) but somehow didn’t work out as expected as there is still an open issue#531 and a pending pull request regarding this problem. As you mentioned you could use // line comments as a workaround for now.
NameError - uninitialized constant Sass::Engine:
I am getting NameError - uninitialized constant Sass::Engine: when I run my sinatra app with sass gem. installed ruby version 2.3.1 with rbenv and also installed sinatra, sass gem. require 'sinatra' require 'slim' require 'sass' # required sass require 'sinatra/reloader' if development? get '/styles.css' do scss :styles #does not generate styles.css, styles.scss file is in /views folder end get '/' do slim :home end get '/about' do #title = "All About This Website" slim :about end get '/contact' do slim :contact #, :layout => :special end not_found do slim :not_found end get '/fake_error' do status 500 "There's nothing wrong, really :P" end Full error: NameError - uninitialized constant Sass::Engine: /home/tasqyn/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/tilt-2.0.5/lib/tilt/sass.rb:13:in prepare' /home/tasqyn/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/tilt-2.0.5/lib/tilt/template.rb:92:in initialize' /home/tasqyn/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/sinatra-1.4.7/lib/sinatra/base.rb:862:in new' /home/tasqyn/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/sinatra-1.4.7/lib/sinatra/base.rb:862:in block in compile_template' /home/tasqyn/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/tilt-2.0.5/lib/tilt.rb:104:in block in fetch' /home/tasqyn/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/tilt-2.0.5/lib/tilt.rb:103:in fetch' /home/tasqyn/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/tilt-2.0.5/lib/tilt.rb:103:in fetch' /home/tasqyn/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/sinatra-1.4.7/lib/sinatra/base.rb:841:in compile_template' /home/tasqyn/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/sinatra-1.4.7/lib/sinatra/base.rb:822:in render' /home/tasqyn/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/sinatra-1.4.7/lib/sinatra/base.rb:687:in scss' main.rb:7:in `block in ' What I am doing wrong? and here is styles.scss: $red: #903; $black: #444; $white: #fff; $main-font: Helvetica, Arial, sans-serif; body { font-family: $main-font; } h1 { color: $red; font: 32px/1 $main-font; } header h1 { font-size: 40px; line-height: 80px; background: transparent url(/images/logo.png) 0 0 no-repeat; padding-left: 84px; } #mixin tabs ($background: blue, $color: yellow) { ul { list-style: none; margin: 0; padding: 0; background: $background; overflow: hidden; } li { float: left; } a { text-decoration: none; display: block; padding: 8px; background: $background; color: $color; &:hover { background: darken($background, 20%); } } } nav { ##include tabs($background: $black, $color: $white); font-weight: bold; } p { font: 13px/1.4 $main-font; }
Perform below steps : Add any of this gem into your gem file gem 'bootstrap-sass' or gem 'sass-rails' Then install bundle using bundle OR bundle install command
Make sure you have in your gemfile: gem 'sass' gem 'sass-rails' If not, add it. Don't forget to run bundle install.
If you are using the asset-pipeline with sinatra, then this might help. For those using rails this could help. In my scenario, (running rails 3.2.22.2) on one machine my app worked. On another machine I cloned the repo and ran into the uninitialized constant Sass::Engine error. Moving gem sass-rails did not work for me I moved gem 'sass-rails' out of the group :assets do block. This did not work for me. Solution for me: rake assets:clean removes all compiled assets. Next time you run rake rails s, your assets will be recompiled. If not, you can run rake assets:precompile to compile all your assets. Or if you are deploying via capistrano, the deploy.rb will run "deploy:assets:precompile" and compile assets for your production/staging machine. The error seems to occur because sass is not being compiled in the asset pipeline correctly. (Would love to know why this occurs if anyone has the answer)
SASS breaks my selector
I'm having trouble with SASS. Locally I have this selector: #featured-categories{ ul{ li{ width: 33.33%; } } } which works as expected. Deployed (and compressed) however this is compiling to: #featured-categoriesulli{width: 33.33%;} which of course is an invalid selector. The more straight more forward formulation: #featured-categories ul li{ width: 33.33%; } behaves in the same way - i.e. compiles to something munged and broken. The only way I can get this to compile is to add redundant rules between the elements of the selector: #featured-categories{ margin: 0; ul{ margin: 0; li{ width: 33.33%; } } } This works, but is obviously not ideal. Can anyone help? I'm in a ruby 1.9.3 project running sass 3.2.9. Any pointers would be greatly appreciated.
The SCSS that you've provided should work fine. Check it out with SassMeister or http://jsfiddle.net/Kjanu/. It will compile to this: #featured-categories ul li { width: 33.33%; } So you've got something else going wrong in your setup.
Load multiple weight custom font with the webfont loader
When defining a custom font with the webfont loader (repo here), we basically define the families loaded and the related URLs: WebFont.load({ custom: { families : [ "My font" ], urls : [ "assets/css/fonts.css" ] } }); But, it seems the loader don't detect multiple weight defined for the same font in the css file: #font-face { font-family: 'My font'; src: url("../fonts/my-font.eot"); font-weight: normal; font-style: normal; } #font-face { font-family: 'My font'; src: url("../fonts/my-font.eot"); font-weight: bold; font-style: normal; } And so, the loader trigger the active event when the first font is loaded. This can be confirmed if we check the fontactive event who'll only be triggered once: WebFont.load({ fontactive: function( fontname, fontdescription ) { console.log( fontname, fontdescription ); // Only trigger once `My font, n4` } }); So, is there a way tell the webfont loader there's multiple weight to get (a bit like their google webfonts interface)? (A fix can be to use multiple names for each font weight, but that's not the solution I'm searching for here)
I'm one of the developers of the webfontloader. You are correct that the custom module does not support loading multiple variations. Luckily we recently added support for this, so if you upgrade your version of the webfontloader (or use the one on the Google CDN) you'll get support for it. You can use it like: WebFont.load({ custom: { families: ['My Font', 'My Other Font:n4,i4,n7'], urls: ['/fonts.css'] } }); To load the 'n4', 'i4' and 'n7' variations of "My Other Font".