---
title: "Five ways to make HTTP requests from Ruby"
publication: "Ruby Weekly"
issue: 542
published_at: "2021-03-04"
canonical_url: "https://rubyweekly.com/issues/542"
---

Plus creating games with Gosu, and how to use Kernel#caller_locations. |        

#542 — March 4, 2021 

[Read on the Web](https://rubyweekly.com/issues/542)    

💡 We've had some fantastic _Tip of the Week_ feedback, so we're carrying on :-) If you have any suggestions of things Jemma could cover, you can tweet her [@jemmaissroff](https://twitter.com/jemmaissroff) — she's also keeping a Twitter thread of [quick Ruby tips](https://twitter.com/JemmaIssroff/status/1366398823124369408) if you want even more.
__
_Peter Cooper, editor_  

![](https://res.cloudinary.com/cpress/image/upload/e_grayscale,w_150,h_170,c_pad,g_south/xrupigpv8kyjqotfdgy8.jpg)    

Ruby Weekly 

[![\](https://res.cloudinary.com/cpress/image/upload/w_1280,e_sharpen:60/da97i2ei9lbfhjqijehr.jpg)](https://upgrow.shopify.io/)  

[Upgrow: A Sustainable Architecture for Rails Apps?](https://upgrow.shopify.io/) — Shopify draws on its experience as a Rails shop to create this guide with patterns (and anti-patterns) for creating large, long-lived Rails apps. Do you agree with their choices? 

Shopify   

[Standard 1.0: A Ruby Style Guide, Linter and Formatter](https://github.com/testdouble/standard) — As [StandardJS](https://standardjs.com/) is to JavaScript, Standard is to Ruby. It’s been around a while, but 1.0 is always a fun milestone, and I keep hearing about more people and teams using this to catch issues and have a standard formatting for codebases. 

test double   [![\](https://copm.s3.amazonaws.com/f7b32350.jpg)](https://www.founderquestpodcast.com/episodes) 

[Are You Listening to FounderQuest Yet?](https://www.founderquestpodcast.com/episodes) — It’s a weekly podcast about three Ruby developers bootstrapping a software business on their own terms...and murderbots, ninjas, garlic scapes, and (checks notes) something called Big Mouth Billy Bass. Don’t miss out. 

FounderQuest sponsor   

[Five Ways to Make HTTP Requests in Ruby](https://www.twilio.com/blog/5-ways-make-http-requests-ruby) — A comparison of several approaches including net/http, httparty, HTTP (the gem), HTTPX, and Faraday. I’m going to admit right now the [http gem](https://github.com/httprb/http) is my go-to. 

Valeriane Vernance   

[Active Record Queries in Views: When It's Bad, When It's Fine](https://www.codewithjason.com/active-record-queries-views-bad-fine/) — Keeping concerns separate where you can is generally a best practice, but Jason doesn’t think it’s a cast iron rule. _(Many JavaScript developers are mixing CSS in with JS nowadays, so it’s a free for all as far as I’m concerned… ;-))_ 

Jason Swett     

Quick Bits 

- [JRuby 9.2.16.0](https://www.jruby.org/2021/03/03/jruby-9-2-16-0.html) has been released.
- [RubyGems 3.2.13](https://blog.rubygems.org/2021/03/03/3.2.13-released.html) has been released with support for non-GNU Linux platforms.  

💻 Jobs  

[Senior Software Engineering Consultant - [100% Remote\]](https://testdouble.recruiterbox.com/jobs/fk0u68n/) — Co-founded by Justin Searls, Test Double is an engineering consultancy on a mission to improve the way the world builds software. Work on challenging projects with a collaborative, passionate team. 100% employee owned, and awesome perks. 

TestDouble   

[Backend Engineer (SF or Vancouver BC)](https://www.chime.com/job-openings/?gh_jid=4225355002&utm_source=RUBYWEEKLY) — Chime is the largest and fastest-growing U.S. player in the challenger-banking space. We’re hiring backend engineers. Join us 😎 

Chime   

[Find Your Next Job Through Hired](https://hired.com/join/?utm_source=newsletter&utm_medium=sponsor&utm_campaign=%28b2c%29%28l-all%29%28r-all%29%28cooper-rubyweekly%29&utm_content=find-your-next-job) — Create a profile on Hired to connect with hiring managers at growing startups and Fortune 500 companies. It's free for job-seekers. 

Hired  

📘 Articles & Tutorials  

▶[Some Rails Best Practices in 20 Minutes](https://www.youtube.com/watch?v=vw7PlBvLq9k) — A well recorded screencast that covers a variety of ideas. 

Jack Kinsella   

[Building Static Ruby Gems Containers using Docker & Amazon Linux](https://dev.to/aws-heroes/building-static-ruby-gems-for-lambda-zip-or-containers-using-docker-amazon-linux-3g0f) — With the ability to deploy containers to Lambda, is there a way to create precompiled gems for some dependencies to ease and speed up deployment? (Spoiler: Yes) 

Ken Collins   

[Testing Multiple Sessions in the Same Test with Capybara](https://boringrails.com/tips/capybara-multiple-user-sessions) — You can simulate switching between users using Capybara’s `using_session` helper. 

Matt Swanson   

[Build a Ruby App with CockroachDB & ActiveRecord](https://www.cockroachlabs.com/docs/v20.2/build-a-ruby-app-with-cockroachdb-activerecord/?utm_campaign=cooperpress-2021-Q1&utm_source=ruby-weekly&utm_medium=sponsor&utm_content=ruby-weekly-build-ruby-app-tutorial) — This tutorial shows how to build a Ruby app with CockroachDB & [ActiveRecord](http://guides.rubyonrails.org/active_record_basics.html). CRDB provides an ActiveRecord adapter as a[RubyGem](https://rubygems.org/gems/activerecord-cockroachdb-adapter). 

CockroachDB sponsor   

[The Ruby Unbundled Series: Creating Games with Gosu](https://blog.engineyard.com/create-ruby-games-using-gosu) — [Gosu](https://www.libgosu.org/) wraps OpenGL capabilities which makes it easy to make simple games like those presented here. 

Darren Broemmer   

[Testing ActiveRecord Concerns](https://alexbarret.com/blog/2021/testing-model-concerns/) — It’s a good idea to test your concerns outside of the models that use them, but there are some, um, concerns about how to do that. 

Alex Barret   

[Build a Text-to-Speech Application with Hanami (and Vonage)](https://learn.vonage.com/blog/2021/02/02/build-a-text-to-speech-application-with-hanami/?utm_source=reddit&utm_medium=organic&utm_campaign=social_media) — Bear in mind this uses the commercial Vonage Voice API (formerly Nexmo). 

Ben Greenberg (Vonage)   

[Rails 6.1 Adds `ActiveRecord::FinderMethods` '`sole`' and '`find_sole_by`'](https://bigbinary.com/blog/rails-6-1-adds-active-record-finder-methods) — For when you truly want one and _only one_ result. 

Akanksha Jain   

▶[Sharing Cookies with Subdomains in Rails](https://gorails.com/episodes/rails-subdomain-cookies) — You might do this if you want to authenticate a user across all subdomains in your Rails app. 

Go Rails  

🛠 Code and Tools  

[Strings::Truncation: Truncate Strings with Fullwidth Characters and ANSI Codes](https://github.com/piotrmurach/strings-truncation) — If you do a lot of truncating strings, a gem like this should be in your toolbelt. 

Piotr Murach   

[WebMock: Stub and Set Expectations on HTTP Requests for Testing](https://github.com/bblimke/webmock) — Supports lots of HTTP libraries like Net::HTTP, Excon, the `http` gem, and Curb::Easy. 

Bartosz Blimke   

[Founders/CTOs: Onboarding New Developers Taking Forever? We Can Help](https://hint.io/helps-you-ship-code) 

Hint sponsor   

[Ahoy 3.2.0: Simple Analytics for Rails Apps](https://github.com/ankane/ahoy) — Track visits and events in your app. By default, data is stored in your normal database. 

Andrew Kane   

[Invisible Captcha 2.0: Unobtrusive Spam Protection for Rails Forms](https://github.com/markets/invisible_captcha) — Your mileage may vary as ‘honeypot’ approaches can work well at first but are easily overcome by a determined abuser. 

Marc Anguera Insa   

[gruf: A gRPC Ruby Framework](https://github.com/bigcommerce/gruf) — A wrapper around the Ruby gRPC library that supports gRPC 1.10.x and above. 

BigCommerce       

💡**Tip of the Week** 

`Kernel#caller_locations` gives us a helpful view of the execution stack 

Have you ever needed to know the execution stack for a call? Perhaps you’re looking to provide a useful logging message with the entire stacktrace? Or you want to know where a specific method was called? Or you’re writing a gem and need to know where a method is being used? 

In any of these cases, you’re in luck because from Ruby 2.0 onwards we have access to [`Kernel#caller_locations`](https://ruby-doc.org/core-3.0.0/Kernel.html#method-i-caller_locations) which gives us an array of [`Thread::Backtrace::Location`](https://ruby-doc.org/core-3.0.0/Thread/Backtrace/Location.html) objects which represent the current execution stack. 

Here’s a simple little example with three methods, where the third method calls the second method which calls the first method which in turn tells us the `caller_locations`:  

def method_one
 caller_locations
 end
 
 def method_two
 method_one
 end
 
 def method_three
 method_two
 end
 
 pp method_three   

If we run this snippet (saved in a file called `example.rb`), we’ll get:  

$ ruby example.rb
 ["example.rb:6:in `method_two'",
 "example.rb:10:in `method_three'",
 "example.rb:13:in `<main>'"]   

We can see our stacktrace! `Kernel#caller_locations` can take two optional parameters which both work to limit the size of the array of `Thread::Backtrace::Location` objects we receive. The first (`start`) parameter tells how many stack entries to omit from the top of the stack. The second (`length`) parameter tells how many objects to include in the returned Array. 

A common use of `caller_locations` is getting the first element to see the calling method. This can be done by passing in the arguments `(1,1)`. If we modify our code slightly to use `caller_locations(1,1)`, we’ll get:  

$ ruby example.rb
 ["example.rb:6:in `method_two'"]   

And we can see that `method_two` is calling `method_one`. Lastly, there are a few useful methods on `Thread::Backtrace::Location` objects. The most notable ones are `#lineno`, `#label` and `#path` which give us the line number, method name and file path respectively. 

This week’s tip was written by [Jemma Issroff](https://twitter.com/jemmaissroff).          

Curated by [Peter Cooper](https://twitter.com/peterc), [Glenn Goodrich](https://twitter.com/ruprict) and [Cooperpress](https://cooperpress.com/). 

[Change your email address](https://rubyweekly.com/edit_subscription/*|UID|*) or[Stop getting this newsletter](https://rubyweekly.com/leave/*|UID|*) 

© Cooper Press Ltd · Fairfield Enterprise Centre, Louth, LN11 0LS, United Kingdom
