ActiveAdmin/Httparty: Can I access json response from variable in view? - ruby-on-rails

I'm using an ActiveAdmin custom page and httparty to request a json response from a third-party api. I've successfully accessed the json data response, parsed it, set it to a variable, and used puts to see it in my console.
How can I access this variable to view it on an activeadmin page?
Here's my activeadmin page:
ActiveAdmin.register_page "API" do
content do
response = HTTParty.get("http://www.omdbapi.com/?s=war&apikey=#####")
res = response.body
result = JSON.parse res
#title = result["Search"][0]["Title"]
puts #title
end
controller do
# response = HTTParty.get("http://www.omdbapi.com/?s=war&apikey=#####")
# res = response.body
# result = JSON.parse res
# #title = result ["Search"][0]["Title"]
# puts #title
end
end
What I've tried:
*capturing JSON data from api works in either the content or controller block.
using a partial to render embedded ruby: <%= #title %>
capturing json data in controller and display #title in content area

text_node instead of puts should work here, see https://activeadmin.info/12-arbre-components.html

Related

How to check if HTTP request status is 200 OK in Rails

I want to do something like this post but in the actual Rails app, not the testing.
I want to see if the HTTP request was successful or not. If it's not successful (aka 404 Not Found), then I want to render a different HTML. But I can't figure out the syntax to compare.
Currently, I have:
def videos
# get current_user's wistia_project_id & authorization token
#current_user = current_user
project_id = #current_user.wistia_project_id
auth_token = "blah"
request = "https://api.wistia.com/v1/projects/#{project_id}.json?api_password=#{auth_token}"
#response = HTTP.get(request).body
puts HTTP.get(request).status
# handle errors: not 200 OK
if !HTTP.get(request).status:
render "/errors.html.erb/"
end
# get embed code for each video using the hashed_id, put in list
#video_iframe_urls = JSON.parse(#response)['medias'].map do |p|
"https://fast.wistia.com/embed/iframe/#{p["hashed_id"]}?version=v1&controlsVisibleOnLoad=true&playerColor=aae3d8"
end
end
require 'net/http'
uri = URI("https://api.wistia.com/v1/projects/#{project_id}.json?api_password=#{auth_token}")
res = Net::HTTP.get_response(uri)
# Status
puts res.code # => '200'
puts res.message # => 'OK'
puts res.class.name # => 'HTTPOK'
# Body
puts res.body if res.response_body_permitted?

If open-uri works, why does net/http return an empty string?

I am attempting to download a page from Wikipedia. For such a task, I am using gems. When using net/http, all I get is an empty string. So I tried with open-uri and it works fine.
Nevertheless, I prefer the first option because it gives me a much more explicit control; but why is it returning an empty string?
class Downloader
attr_accessor :entry, :url, :page
def initialize
# require 'net/http'
require 'open-uri'
end
def getEntry
print "Article name? "
#entry = gets.chomp
end
def getURL(entry)
if entry.include?(" ")
#url = "http://en.wikipedia.org/wiki/" + entry.gsub!(/\s/, "_")
else
#url = "http://en.wikipedia.org/wiki/" + entry
end
#url.downcase!
end
def getPage(url)
=begin THIS FAULTY SOLUTION RETURNS AN EMPTY STRING ???
connector = URI.parse(url)
connection = Net::HTTP.start(connector.host, connector.port) do |http|
http.get(connector.path)
end
puts "Body:"
#page = connection.body
=end
#page = open(url).read
end
end
test = Downloader.new
test.getEntry
test.getURL(test.entry)
test.getPage(test.url)
puts test.page
P.S.: I am an autodidact programmer so the code might not fit good practices. My apologies.
Because your request return 301 Redirect (check connection.code value), you should follow redirect manually if you are using net/http. Here is more details.

json and rails 3 http request

Having a page which contains list of users of my site in json format .I am using jquery getJson to get the dat from that url but I need these page not to be accessible if the user tried to open it by http request.
You can filter a specific action using request.xhr? that checks the value of header[X-Requested-With] of the request.
def your_action
unless request.xhr?
render status: 404 # or what you want
return
end
# action code
end

Passing params Hash in methods in rails

I have the following code in the controller
def show
client = GameAccounts::GameAccountsClient.new
..
json = client.get_accounts( ..)
..
end
I have the following code in GameAccountsClient
def get_accounts(..)
response = query_account(CGI.escape(params[:name])
JSON.parse(response.body)
end
I have the above code and i am not sure how to pass params[:name] while calling the get_accounts method in the controller. Can anyone help me out with passing the hash's in methods in rails ? .Thank you
If i understand it correctly, you just need to pass the params[:name] to your model method.
def show
client = GameAccounts::GameAccountsClient.new
json = client.get_accounts(params[:name])
end
def get_accounts(name)
response = query_account(CGI.escape(name)
JSON.parse(response.body)
end

Rails controller is returning escaped JSON

def show
#find parent call_flow
#call_flow = CallFlow.where('dnis' => params[:dnis]).first
if #call_flow.nil?
#response = ["no result"]
else
#find first routable options (should be a message)
#call_flow_options = #call_flow.routable_type.constantize.find(#call_flow.routable_id).options
#if there is a route after the first route, find it
unless #call_flow_options.first.target_routable_type.nil?
#target_routable = #call_flow_options.first.target_routable_type.constantize.find(#call_flow_options.first.target_routable_id).options
#call_flow_options.to_a.push(#target_routable.to_a)
end
#response = #call_flow.to_a.push(#call_flow_options)
end
respond_with #response
end
I get the data back but the browser doesn't recognize it as JSON because all the " double quotes are replaced with ".
If you're looking to have the entire response be JSON (rather than HTML/JavaScript that uses JSON) you can do:
render :json => #response

Resources