http requests

37 views
Skip to first unread message

Joe Guerra

unread,
May 31, 2017, 1:27:42 PM5/31/17
to Ruby on Rails: Talk
Hmm, which gem would allow you to perform http requests within Rails?  I would like to send a request to the canadapost website and pass it some parameters and return a shipping total.

I do believe there is a canadapost gem, but I'm not sure how upto date it is.

I guess the other option is to like add paramters to a url and open that page on a new tab?

Thanks,
Joe

Hassan Schroeder

unread,
May 31, 2017, 1:36:20 PM5/31/17
to rubyonrails-talk
On Wed, May 31, 2017 at 10:27 AM, Joe Guerra <JGu...@jginfosys.com> wrote:
> Hmm, which gem would allow you to perform http requests within Rails? I
> would like to send a request to the canadapost website and pass it some
> parameters and return a shipping total.
>
> I do believe there is a canadapost gem, but I'm not sure how upto date it
> is.

Searching for `ruby gem canada post` turns up more than one -- why
don't you just try them and see which you prefer?

Similarly `ruby gem http` will show you some possibilities if you want
to roll your own solution. See also:
https://ruby-doc.org/stdlib-2.4.1/libdoc/net/http/rdoc/index.html

HTH!
--
Hassan Schroeder ------------------------ hassan.s...@gmail.com
twitter: @hassan
Consulting Availability : Silicon Valley or remote

Joe Guerra

unread,
May 31, 2017, 1:39:05 PM5/31/17
to Ruby on Rails: Talk
Ok, I'll try the gem.

How about passing the parameters in the url?  and opening up the canada post site with that?

Thanks,
Joe

Hassan Schroeder

unread,
May 31, 2017, 1:58:20 PM5/31/17
to rubyonrails-talk
On Wed, May 31, 2017 at 10:39 AM, Joe Guerra <JGu...@jginfosys.com> wrote:

> How about passing the parameters in the url? and opening up the canada post
> site with that?

You can certainly do that, if one of the gems doesn't meet your needs.

Have you read the Canada Post API docs?

Jason Fleetwood-Boldt

unread,
May 31, 2017, 2:50:55 PM5/31/17
to rubyonra...@googlegroups.com
Hassan is correctly encouraging you towards the higher-level abstractions, like a Gem specifically written for this purpose.

If you really want to know how to make outgoing http requests, the answer is the Net::HTTP Ruby library, which is documented here:


But as Hassan said if there's a higher level abstraction, like a Gem, prefer that to writing your own low-level code.

But as a ruby developer the Net::HTTP Ruby library is at the very least something you should learn about and play around with, even if you don't actually need it for this immediate purpose.
-Jason



--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-ta...@googlegroups.com.
To post to this group, send email to rubyonra...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/80f27a88-f7ca-4bc8-a66c-380da41a4ccf%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

If you'd like to reply by encrypted email you can find my public key on jasonfleetwoodboldt.com (more about setting GPG: https://gpgtools.org

steve enzer

unread,
Jun 1, 2017, 12:29:18 PM6/1/17
to Ruby on Rails: Talk
httparty might do the job

Joe Guerra

unread,
Jun 1, 2017, 1:46:39 PM6/1/17
to Ruby on Rails: Talk
Well, I got the canada post api keys.  

I've got to try it with one of following canada post gem...

Joe Guerra

unread,
Jun 2, 2017, 1:11:24 PM6/2/17
to Ruby on Rails: Talk
Hmm, doesn't seem to work with the example they give.   

I've used my api keys, and popped in some values.  It's supposed to return a hash.  

I'm not sure if it's actually connecting?  How would I check to see if the request was actually sent?  and what the response is?

Thanks,
Joe

Joe Guerra

unread,
Jun 2, 2017, 1:23:42 PM6/2/17
to Ruby on Rails: Talk
hmm, the canada post gem requires httparty as a runtime dependency.   Does it automatically get bundled with it, or do I need to bundle it myself?


On Thursday, June 1, 2017 at 12:29:18 PM UTC-4, steve enzer wrote:

Colin Law

unread,
Jun 2, 2017, 5:07:10 PM6/2/17
to Ruby on Rails: Talk
On 2 June 2017 at 18:23, Joe Guerra <JGu...@jginfosys.com> wrote:
> hmm, the canada post gem requires httparty as a runtime dependency. Does
> it automatically get bundled with it, or do I need to bundle it myself?

If it is properly defined then bundle install should install it.

Colin

>
>
> On Thursday, June 1, 2017 at 12:29:18 PM UTC-4, steve enzer wrote:
>>
>> httparty might do the job
>>
>> On Wednesday, May 31, 2017 at 10:27:42 AM UTC-7, Joe Guerra wrote:
>>>
>>> Hmm, which gem would allow you to perform http requests within Rails? I
>>> would like to send a request to the canadapost website and pass it some
>>> parameters and return a shipping total.
>>>
>>> I do believe there is a canadapost gem, but I'm not sure how upto date it
>>> is.
>>>
>>> I guess the other option is to like add paramters to a url and open that
>>> page on a new tab?
>>>
>>> Thanks,
>>> Joe
>
> --
> You received this message because you are subscribed to the Google Groups
> "Ruby on Rails: Talk" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to rubyonrails-ta...@googlegroups.com.
> To post to this group, send email to rubyonra...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/rubyonrails-talk/b97d3651-77b8-4995-b705-0b4c35db561c%40googlegroups.com.

Joe Guerra

unread,
Jun 5, 2017, 3:08:51 PM6/5/17
to Ruby on Rails: Talk
Yes, it installed it.  

But the gem is not very well documented.  I can't get it to work.

Hassan Schroeder

unread,
Jun 5, 2017, 4:17:20 PM6/5/17
to rubyonrails-talk
On Mon, Jun 5, 2017 at 12:08 PM, Joe Guerra <JGu...@jginfosys.com> wrote:
> Yes, it installed it.
>
> But the gem is not very well documented. I can't get it to work.

That's a shame.

If only there were somewhere you could post your failing code and
get some other people to help...

Joe Guerra

unread,
Jun 6, 2017, 4:20:36 PM6/6/17
to Ruby on Rails: Talk
Yes, I'm going to email the author of the gem.  Get his take on it. 
It hasn't been updated for over 2 years.  Maybe something has changed on the Canada post side.

Hassan Schroeder

unread,
Jun 6, 2017, 4:29:06 PM6/6/17
to rubyonrails-talk
On Tue, Jun 6, 2017 at 1:20 PM, Joe Guerra <JGu...@jginfosys.com> wrote:
> Yes, I'm going to email the author of the gem. Get his take on it.

SMH. Apparently my attempt at sarcasm fell straight through...

>> > But the gem is not very well documented. I can't get it to work.
>>
>> That's a shame.
>>
>> If only there were somewhere you could post your failing code and
>> get some other people to help...

TO BE BLUNT: If you want help, POST THE CODE AND ERROR,
or better yet, a standalone test case.

Saying "it doesn't work" doesn't help you understand the issue, or
allow anyone else to offer any useful advice. (The author of the gem
in question is probably not a mind-reader either.)

See also: http://www.catb.org/esr/faqs/smart-questions.html

Jason Fleetwood-Boldt

unread,
Jun 6, 2017, 5:02:02 PM6/6/17
to rubyonra...@googlegroups.com
Joe -


This (gems having gone stale) is generally par for the course in the Ruby world.

This is a good resource that gives you some idea of how popular & active any given gem might be:


Choosing an old gem that is stale (hasn't been updated in a couple of years) is common. Then again, the team I work with did an exhaustive internet search for some gem code that had to do with image optimization. They told me that one or more of the gems they were looking at hadn't been updated since 2015. We all laughed and thought to ourselves that meant it was a stale codebase.

Then upon more digging, we realized that the code from 2015 actually worked fine, and we just hadn't spent enough time trying to make it work for us. So in that case, it wasn't that no one had worked on image optimization since 2015, it was because that problem was solved in 2015 (in that example), and didn't need more work because it continued to work in 2017. 


As far as Canada Post, we use EasyPost for our shipping labels (https://www.easypost.com), which is NOT a gem, but in fact a SAS service. (It, incidentally, does have an open-source gem that has the API for communicating with EasyPost. But you pay them a small fee to generate each label).


Before you email the author of the gem, I'd like to take a small moment to expound on the nature of open-source software:

They said this back in the 90s, but it's so important and so relevant to you I'm gonna say it again:

"Free as in speech, not free as in beer."


"Free software" (what we called open source back in the 90s) isn't "free" like you don't have to pay for it. If that's what you think, you are totally wrong and need to adjust your mindset. It is "free as in speech" — can be copied, modified, etc, without limits imposed on it. 

So consider that someone — two years ago — wrote some code to interface with Canada Post. They put their code on Github. Putting OS code on Github has no guarantee that that author is going to continue to maintain it. 

And take a step back and think about this: Why would they? You have a need to have Ruby code that works with Canada Post. Maybe they do too, or maybe they have moved on an work somewhere else now, or maybe they won the lottery and retired, or maybe they got sick of being a Ruby developer and got a new profession. Or maybe (heavens to betsy!) they switched to Node.JS.

It's kind of like Ray Bradbury's great The Martian Chronicles: Someone was here once, they left some artifacts, you get to enjoy those artifacts, but you don't get to be them or live in their (ancient, bygone) society. (Ok so that Martian Chronicles analogy was a stretch. I'll work on it. But that is actually the core thesis of the Martian Chronicles — one of the best books of all time.)

My point is that unless you are offering to pay him or her, you need to think about the implicit need-dynamics of emailing him to "get his take on it." Obviously, there's nothing wrong with that on the face of it. But "free software" isn't "free" like you don't have to pay for it ("free as in beer"), it's "free" like the 1st amendment of the US Constitution (sorry Canadians!), there can be no laws recognized that limit it ("free speech").

This is a crucial and important concept to understand as you navigate the world of old gems that are no longer usable.

Oh and since you're new to the community: Check out that "Fork" button on Github! 

Fork the Gem, fix it, and submit your changes back to the original repository — you will then be contributing to the whole community. If you think the author is gone (like the martians in the Martian Chronicles), dead (that happens too— in fact I've corresponded with Github tech support over their policy regarding passed-away authors), or no longer cares, you can detach your fork from the origin repository and make a "V2" (or whatever) of the gem, upgraded with your fixes, rename it, and then you'll be the Gem's author. 

Then in 2 years from now when Canada Post changes their API again, some aspiring Ruby code will email YOU and say "why doesn't this gem work"! 
-Jason







On Jun 6, 2017, at 4:20 PM, Joe Guerra <JGu...@jginfosys.com> wrote:

Yes, I'm going to email the author of the gem.  Get his take on it. 
It hasn't been updated for over 2 years.  Maybe something has changed on the Canada post side.

Joe Guerra

unread,
Jun 6, 2017, 7:02:10 PM6/6/17
to Ruby on Rails: Talk
Thanks.  If I can figure out to make my own http request to my canada post account, I can possibly grab the shipping rate if I pass it weight, length, height, and width.
Reply all
Reply to author
Forward
0 new messages