简体   繁体   English

Httparty在轨道上

[英]Httparty in rails

I am trying to learn how to use httparty. 我正在尝试学习如何使用httparty。 I ran 'gem install httparty', first in my terminal, expecting to be able to use it in a pry session but no luck. 我首先在终端上运行了“ gem install httparty”,希望能够在一次撬动中使用它,但是没有运气。

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: 接下来,我创建了一个新的Rails应用程序,将gem添加到我的gem文件中并运行了捆绑包,并在一个窥探会话中尝试如下使用httparty:

[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 谢谢

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. 我所做的是在rails lib目录中创建一个文件test_party.rb (Make sure to add config.autoload_paths += %W(#{config.root}/lib) to your config/application.rb (确保将config.autoload_paths += %W(#{config.root}/lib)config/application.rb

In the new lib file create a class TestParty and include HTTParty 在新的lib文件中,创建一个类TestParty并include HTTParty

Then in rails console you can run TestParty.whatever_you_want 然后在Rails控制台中,您可以运行TestParty.whatever_you_want

Hope that helps! 希望有帮助!

require the gem, either in your pry invocation 在您的撬动调用中需要宝石

pry -rhttparty

or once in pry 或一次撬

require 'httparty'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM