mdror
mdror

Reputation: 79

Httparty in rails

I am trying to learn how to use httparty. I ran 'gem install httparty', first in my terminal, expecting to be able to use it in a pry session but no luck.

Next, I created a new rails app, added the gem to my gem file and ran bundle and in a pry session tried to use httparty as follows:

[1] pry(main)> HTTParty.get("http://rubygems.org/api/v1/versions/httparty.json")
NameError: uninitialized constant HTTParty
from (pry):1:in `__pry__'
[2] pry(main)> HTTParty.get("http://rubygems.org/api/v1/versions/httparty.json)
[2] pry(main)* httParty.get("http://rubygems.org/api/v1/versions/httparty.json)
SyntaxError: unexpected tIDENTIFIER, expecting ')'
httParty.get("http://rubygems.org/api/v1/versions/httparty.json)
                  ^
[2] pry(main)> httParty.get("http://rubygems.org/api/v1/versions/httparty.json")
NameError: undefined local variable or method `httParty' for main:Object
from (pry):2:in `__pry__'
[3] pry(main)> response = httParty.get("http://rubygems.org/api/v1/versions/httparty.json")
NameError: undefined local variable or method `httParty' for main:Object
from (pry):3:in `__pry__'
[4] pry(main)> response = HTTParty.get('https://api.stackexchange.com/2.2/questions?site=stackoverflow')
NameError: uninitialized constant HTTParty
from (pry):4:in `__pry__'

Any help would be much appreciated. Thank you

Upvotes: 2

Views: 1649

Answers (2)

lab419
lab419

Reputation: 1258

require the gem, either in your pry invocation

pry -rhttparty

or once in pry

require 'httparty'

Upvotes: 1

scottknight
scottknight

Reputation: 442

I am working on the exact same thing right now. What I did was create a file test_party.rb in the rails lib directory. (Make sure to add config.autoload_paths += %W(#{config.root}/lib) to your config/application.rb

In the new lib file create a class TestParty and include HTTParty

Then in rails console you can run TestParty.whatever_you_want

Hope that helps!

Upvotes: 0

Related Questions