http requests

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

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!

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

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

Have you read the Canada Post API docs?

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:

https://docs.ruby-lang.org/en/2.0.0/Net/HTTP.html

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

httparty might do the job

Well, I got the canada post api keys.

I’ve got to try it with one of following canada post gem…

https://rubygems.org/search?utf8=✓&query=canada+post

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

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?

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

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...

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.

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

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:

https://www.ruby-toolbox.com

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

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.