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
require the gem, either in your pry invocation
pry -rhttparty
or once in pry
require 'httparty'
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!
Related
In the Gemfile, the gem is listed like this:
gem "faraday"
This leads to the installation of the current version 1.10.0.
Then, following this documentation, I create a custom logger, but I get an error:
NameError: uninitialized constant Faraday::Logging
Did you mean? Logger
If open the Rails console and write the code Faraday::Logging::Formatter there, the error will be exactly the same.
Tell me, please, what is the matter and how to fix it?
UPD.
If I write this in the Rails console:
Faraday::Response::Logger::Formatter
Then I get this error:
NameError: uninitialized constant Faraday::Response::Logger::Formatter
Then I write this:
Faraday::Logging::Formatter
And there are no more errors. What is going on?
bundle exec rails c
Faraday::Logging::Formatter
=> NameError: uninitialized constant Faraday::Logging
Faraday::Logging::Formatter
=> NameError: uninitialized constant Faraday::Logging
Faraday::Response::Logger::Formatter
=> NameError: uninitialized constant Faraday::Response::Logger::Formatter
Faraday::Logging::Formatter
=> Faraday::Logging::Formatter
Say I have:
app/models/food/fruit.rb:
module Food
class Fruit
class_attribute :field_mapping
self.field_mapping = MyOrg::Trees::Details.field_mapping
# other things including:
end
end
In local/development mode, I can open a console and do/get this:
rails console
[1] pry(main)> Food::Fruit
NameError: uninitialized constant MyOrg::Trees::Details
MyOrg::Trees::Details will eventually be defined via a gem, but until then I want to fake it by adding the following to the top of the same file; why isn't this doing the trick?
module MyOrg
module Trees
class Details
def field_mapping
end
end
end
end
Note: when I paste the above into a local/development console, I can then successfully do this:
[2] pry(main)> MyOrg::Trees::Details.new.field_mapping
=> nil
When I added the above fake, I accidentally started trying to reference it to test what I was doing...
rails console
[1] pry(main)> MyOrg::Trees::Details
NameError: uninitialized constant MyOrg::Trees::Details
...but I should have called Food::Fruit as before, and I would have isolated the issue...
rails console
[1] pry(main)> Food::Fruit
NoMethodError: undefined method `field_mapping' for MyOrg::Trees::Details:Class
...ah, I need to make it a class method...
module MyOrg
module Trees
class Details
def self.field_mapping
end
end
end
end
...and then...
rails console
[1] pry(main)> Food::Fruit
NoMethodError: undefined method `values' for nil:NilClass
So, I appeased the undefined method error, and now have a new problem, which is that further down the original code there is actually another reference to field_mapping (field_mapping.values) which now has to be appeased...
module MyOrg
module Trees
class Details
def self.field_mapping
{}
end
end
end
end
...and now...
rails console
[1] pry(main)> Food::Fruit
=> Food::Fruit
Ran into an interesting scenario today that I'm unsure how to resolve.
Given a rails app with an initializer:
file: config/initializers/integrations.rb
Integrations::CONFIGS = { "key" => "value" }.freeze
If I go into bundle exec rails console and ask for that constant it works as expected:
Integrations::CONFIGS
=> {"key"=> "value"}
Then if I use reload! in the console, I lose that constant:
[2] pry(main)> reload!
Reloading...
=> true
[3] pry(main)> Integrations::CONFIGS
NameError: uninitialized constant Integrations::CONFIGS
from (pry):3:in `<main>'
If I remove the namespace and just have CONFIGS as a constant it works and reloads as expected. I've read through as much of the reload! documentation as I could find and from what I can tell this isn't expected.
My question being, how can I correctly use a namespaced constant in an initializer while also still being able to use reload!?
I'm getting the error uninitialized constant SessionsController I've searched, and can only find explanations of this error in reference to a NameError Does anyone know what the error means?
That is one of two "subtypes of" Name Error dealing with uninitialized variables. The language of "uninitialized constant" is due to the fact that SessionsController is capitalized. Both types are illustrated below:
new-host-3:bot palfvin$ irb
2.0.0p247 :001 > foobar
NameError: undefined local variable or method `foobar' for main:Object
from (irb):1
from /Users/palfvin/.rvm/rubies/ruby-2.0.0-p247/bin/irb:16:in `<main>'
2.0.0p247 :002 > Foobar
NameError: uninitialized constant Foobar
from (irb):2
from /Users/palfvin/.rvm/rubies/ruby-2.0.0-p247/bin/irb:16:in `<main>'
2.0.0p247 :003 >
It means that you're trying to use a Class or a Module that wasn't defined yet. Probably because you forgot to require them.
Make sure that this class SessionsController has been declared in your sessions_controller.rb.
Read more: http://ruby.about.com/od/faqs/qt/Nameerror-Uninitialized-Constant-Object-Something.htm
I would like to know if when using pry is possible to have access to the variable app?
As an example, when I try to access the root_path I get the following error:
[14] pry(main)> app.root_path
NameError: undefined local variable or method `app' for main:Object
Someone said that "It does now work with pry and 3.2.9". I am using rails 3.2.12, but it doesn't seem to work.
I have gem 'pry' in my GemFile group development and in config/environments/development.rb the following
# Use Pry instead of IRB
silence_warnings do
begin
require 'pry'
IRB = Pry
rescue LoadError
end
end
Yes, it works
➜ MyApp git:(master) rc
Loading development environment (Rails 3.2.13)
[1] pry(main)> app.root_path
=> "/"
I use pry-rails in favor of your overriding of IRB in an initializer.
group :development do
gem 'pry-rails'
end
https://github.com/rweng/pry-rails
Though this is solved, if you don't want to or can't use the 'pry-rails' gem for some reason you can also add the following into your .pryrc file:
if defined?(Rails) && Rails.env
extend Rails::ConsoleMethods
end
You can read more in the pry wiki