JSONRPC usage in Rails 3 - ruby-on-rails

I'm quite a beginner with Ruby On Rails and I need to implement client part of JSON API in order to use some external service.
I found a gem called jsonrpc and it looks perfect for me.
I've added it to my Gemfile as:
gem 'jsonrpc'
gem 'json'
and ran bundle
My code looks like this:
class SearchController < ApplicationController
def index
d = JsonRpcClient.new 'http://remote.bronni.ru/Dictionaries.ashx'
#countries = d.getCountries
end
But when I try to access it as http://localhost:3000/search, Rails says:
NameError in SearchesController#index
uninitialized constant SearchController::JsonRpcClient
I only have little experience with Rails so I'm asking you guys to help me out.
I really need to get this to work ASAP.
Thank you all in advance! I really appreciate your help!
UPDATE
I've tryed to get ActiveResource to work but I can't get how it build URL's. The remote service is not a Ruby XML app. (http://remote.bronni.ru/Dictionaries.ashx) And I need to use getCountries method so the URL should be http://remote.bronni.ru/Dictionaries.ashx/getCountries

Related

rails current_teacher keyword not set

I'm new to Ruby on Rails and I'm looking at an application that has a variable called current_teacher. I cannot seem to find where this is set. Everywhere I look the code seems to read from it but where is it set. Is this one of those things that Rails does for you. There is a mode and a table called teachers, so I'm sure this has something to do with it.
I'm very confused by statements like the following, can someone tell me how Rails does this?
if current_teacher.can_request_fieldtrip
Suppose you have a controller like :
class ClientsController < ApplicationController
  def new
if current_teacher.can_request_fieldtrip
# code
end
  end
end
Here is debugging tips :
(a) put this in your Gemfile and do bundle install :
`gem 'pry-rails', :group => :development`
(b) Put the line binding.pry just before the if statement.
(c) Start rails server using rails s.
(d) Hit the browser like http://localhost:3000/new
(e) Now you will be in the Pry console. Just do in the console,
method(:current_teacher).source_location
And the above line tell you where the method has been defined.
Documentation of Method#source_location
Returns the Ruby source filename and line number containing this method or nil if this method was not defined in Ruby (i.e. native)
Rails does not support authentication by itself, however there are a lot of 'add-ons' that rails can use. These 'add-ons' are called gems. This can be a little confusing because you can't actually see their code inside your project folder.
If you open a file called "Gemfile" (it should be in your project folder) you can see a list of gems that you use. Try searching their names on google, you will probably find official web page that contains it's documentation. That way can learn what they do and how to use them.
current_teacher method smells like "Devise" gem
https://github.com/plataformatec/devise
I'm not sure about can_request_fieldtrip, this could be a custom method defined in Teacher model.

Uninitialized Constant GooglePlaces

I am currently using the google_places gem to try to access the places API. I am using the following code to get results:
class PlacesController < ApplicationController
def index
if params[:search]
#client = ::GooglePlaces::Client.new(Rails.application.secrets.places_api_key)
#places = #client.spots_by_query(params[:search])
end
end
end
I am running into an error of uninitialized constant GooglePlaces, which is replaced with PlacesController::GooglePlaces if I don't scope out. I believe this is a scoping issue, but nothing that I have tried fixes the issue. I am following the directions in the repo's readme and assuming that I don't have to include the source in the lib directory of my site. I can use the gem correctly from the rails console.
To use this API in rails application you need to use google_places gem.
add in gem file and run bundle install and restart the server once
gem 'google_places'
Next Create a project in google console and generate secret key .
https://code.google.com/apis/console
https://developers.google.com/places/documentation/
Finally restart the server
The docs said the API auth call should be:
#client = GooglePlaces::Client.new(Rails.application.secrets.places_api_key)

Why uninitialized constant HomeController::HideMyAss?

Cant figure out what I'm doing wrong.
Using hidemyass gem for proxy.
Using github example code.
class HomeController < ApplicationController
require 'hidemyass'
def index
HideMyAss.options[:max_concurrency] = 3
response = HideMyAss.get("www.google.com", timeout: 2)
end
end
Whey I try to use them in my rails controller, I get the Uninitialized constant error.
uninitialized constant HomeController::HideMyAss
Tried looking at the source to figure out with no luck. Maybe it's the problem with my code. Gemfile is good and tried looking at all the things causing the problem.
There is no need to write require 'hidemyass'
You just need to add gem 'hidemyass' to Gemfile and do bundle install. Check installation with gem list hidemyass command. I have successfully checked it and used github example. It works fine and smoothly.

undefined method `caches_action' for ApplicationController:Class

I'm trying to upgrade to rails 4 beta 1, but I've a bit of a problem.
This is, in short, how my application controller looks like.
class ApplicationController < ApplicationController
caches_action :method
end
caches_action is moved out to it's own gem in Rails 4, so including the gem should fix the problem.
gem "actionpack-action_caching", github: "rails/actionpack-action_caching"
But when I run my requests specs or visit the application in a browser I get this error.
app/controllers/application_controller.rb:3:in `<class:ApplicationController>': undefined method `caches_action' for ApplicationController:Class (NoMethodError)
Why is that?
Rails 4.0.0.beta1
Ruby 2.0.0
Rspec 2.13.1
As the caching is not a part of core anymore, you need to explicitly require it in top of every file you use it in:
require 'actionpack/action_caching'
The problem is in Rails 4, they have extracted cache part into separate gems
if you are getting error for action caching then you need to add below gem
gem 'actionpack-action_caching'
for page caching have to add
gem 'actionpack-page_caching'
I also played around then I figure out that, have not added gem to do the same.
hope that will works. Thank you.
Iam using rails as an API rails new AppName --api
I got it working by adding
include ActionController::Caching
in class ApplicationController < ActionController::API

Why can't I locate this method in the rails api docs?

I'm studying some Rails 3 code from Spree:
module Spree
module Generators
class SiteGenerator < Rails::Generators::Base
source_root File.expand_path("../../templates", __FILE__)
desc "Configures an existing Rails application to use Spree."
def create_lib_files
template 'spree_site.rb', "lib/spree_site.rb"
end
def additional_tweaks
remove_file "public/index.html"
append_file "public/robots.txt", <<-ROBOTS
.... continues ....
This works with Rails 3, but I've looked up Rails::Generators::Base, following the inherited modules to Rails::Generators::Actions and the Thor classes, but still can't seem to find api documentation on the #template method. I can figure out what it does, but I'm troubled that I can't find the docs on it. It's got me feeling like a real newbie (though, since I haven't worked with Rails in quite awhile, I guess in some ways I am).
Any help would be appreciated. Please tell me why I'm not able to find this (and other) methods in the Rails api docs. What am I missing???
The template method is an instance method included with Thor::Actions and can be found at http://rubydoc.info/gems/thor/0.14.6/Thor/Actions:template

Resources