Read the content of a div from a NET:HTTP response - ruby-on-rails

I am doing a http request to a website and I would like to store the value of a div from the response.
This is what I am doing in the controller:
class RoomsController < ApplicationController
require "uri"
require "net/http"
require 'json'
require 'cgi'
def test
dialect = params[:dialect]
text = params[:text]
uri = URI('http://www.degraeve.com/cgi-bin/babel.cgi')
params = { :w => text, :d => dialect }
uri.query = URI.encode_www_form(params)
res = Net::HTTP.get_response(uri)
data = res if res.is_a?(Net::HTTPSuccess)
render json: data.body
end
end
I am passing 2 parameters using GET method to get the response. But I need just the content of a div. How do I do that?

I did this:
dialect = current_user.dialect.name
text = params[:text]
uri = URI('http://www.degraeve.com/cgi-bin/babel.cgi')
params = { :w => text, :d => dialect }
uri.query = URI.encode_www_form(params)
response = Net::HTTP.get_response(uri)
render inline: response.body.html_safe, layout: false if response.is_a?(Net::HTTPSuccess)
and then got the div that I wanted in a .js file.

Related

Trying to Post an xml document to a server

I'm on an mission create an XML file and then POST this file to a server address. However I cant crack why it wont send, im currently stuck getting a "TypeError in CatalogController#gen_xml" > "String can't be coerced into Integer".
However im quite new to this and dont fully grasp to how to best execute this. Any help would be greatly appreciated!
Controller
class CatalogController < ApplicationController
require "builder/xmlmarkup"
require 'builder'
require 'rubygems'
require 'net/http'
def gen_xml
#xml = Builder::XmlMarkup.new
#catalogs=Catalog.all
url = "https://xxx.xxxx.com/xxxCXMLxx.aw/xx/cxml";
request = Net::HTTP::Post.new(url)
#request.add_field "Content-Type", "application/xml"
request.body = #xml
url = URI.parse(url)
req = Net::HTTP::Post.new(url.request_uri)
http = Net::HTTP.new(url.host, url.port)
http.use_ssl = (url.scheme == "https")
#response = http.request(req)
response = http.post(url, #xml, initheader = {'Content-Type' =>'text/xml'})
end
end
View with Button to call method
Super cXML Generator!
<%= form_tag catalog_gen_xml_path, method: :post do %>
<%= submit_tag 'Call Action' %>
<% end %>
Routes File
Rails.application.routes.draw do
get 'catalog/gen_xml'
post 'catalog/gen_xml'
end
Error Trace
Extracted source (around line #24):
22 http.use_ssl = (url.scheme == "https")
23 #response = http.request(req)
24 response = http.post(url, #xml, initheader = {'Content-Type' =>'text/xml'})
25 end
26 end
27
app/controllers/catalog_controller.rb:24:in `gen_xml'
Rails.root: /Users/i303072/railsapps/xmlbuilder
XML Builder File - gen_xml.builder
#xml.instruct! :xml, :version=>"1.0", :encoding=>"UTF-8"
#xml_markup.declare! :DOCTYPE, :chapter, :SYSTEM, "../dtds/chapter.dtd"
xml.cXML(:payloadID=>"XX", :timestamp=>"2018-03-11T11:28:01-07:00", :version=>"1.2.029", :xmllang=>"en-US") {
xml.Header {
xml.From {
xml.Credential(:domain=>"ID") {
xml.Identity "AN-T"
}#Credential
}#From
xml.To {
xml.Credential(:domain=>"ID") {
xml.Identity "AV-T"
}#Credential
xml.Correspondent{
xml.Contact(:role=>"correspondent") {
xml.Name("MRO", "xml:lang" => "en")
xml.PostalAddress{
xml.Country("US", "isoCountryCode" => "US")
}#PostalAddress
}#Contact
}#Correspondent
}#To
xml.Sender {
xml.Credential(:domain=>"NetworkID") {
xml.Identity "AN-T"
xml.SharedSecret "xxx"
}#Credential
xml.UserAgent "xxx"
}#Sender
} #Header
xml.Message(:deploymentMode=>"test") {
xml.ProductActivityMessage(:subcontractingIndicator=>"yes") {
xml.ProductActivityHeader(creationDate: "2018-03-13T22:00:00-08:00", messageID: "Cxxx")
}#ProductivityActivityMessage
}#Message
}#cXML

uninitialized constant "controllername::modulename" TableauServer

Im trying to test the tableau_trusted.rb example for trusted authentication for tableau server in Ruby on rails but I keep getting the error "uninitialized constantTableauTrustedsController::TableauTrustedInterface", this is my code:
tableautrusteds_controller.rb
class TableauTrustedsController < ApplicationController
include TableauTrustedInterface
def index
tabserver = 'xxxxx'
tabuser = 'test'
tabpath = 'views/Tableau_DW1/General?:iid=1'
tabparams = ':embed=yes&:toolbar=no'
ticket = tableau_get_trusted_ticket(tabserver, tabuser, request.remote_ip)
if ticket != "-1"
url = "http://#{tabserver}/trusted/#{ticket}/#{tabpath}?#{tabparams}"
redirect_to url
return
end
render :status => 403, :text => "Error with request"
end
end
module TableauTrustedInterface
require 'net/http'
require 'uri'
# the client_ip parameter isn't necessary to send in the POST unless you have
# wgserver.extended_trusted_ip_checking enabled (it's disabled by default)
def tableau_get_trusted_ticket(tabserver, tabuser, client_ip)
post_data = {
"username" => tabuser,
"client_ip" => client_ip
}
response = Net::HTTP.post_form(URI.parse("http://#{tabserver}/trusted"), post_data)
case response
when Net::HTTPSuccess
return response.body.to_s
else
return "-1"
end
end
end
I have changed the line "include TableauTrustedInterface" to "extend TableauTrustedInterface" but it didn't work.
Also, The URL I put in the browser is
http://localhost:3000/tableautrusteds/index, I use get 'tableautrusteds/index' in routes.rb.
I don't really know if that is important but some people ask me for this.
I am little bit new in rails so any help will be very appreciated.
I fixed my problem, if anybody was having a similar issue here is my code
module TableauTrustedInterfaces
require 'net/http'
require 'uri'
# the client_ip parameter isn't necessary to send in the POST unless you have
# wgserver.extended_trusted_ip_checking enabled (it's disabled by default)
def tableau_get_trusted_ticket(tabserver, tabuser, client_ip)
post_data = {
"username" => tabuser,
"client_ip" => client_ip
}
response = Net::HTTP.post_form(URI.parse("http://#{tabserver}/trusted"), post_data)
case response
when Net::HTTPSuccess
return response.body.to_s
else
return "-1"
end
end
end
class TableauTrustedController < ApplicationController
include TableauTrustedInterfaces
def index
tabserver = 'xxxxx'
tabuser = 'test'
tabpath = 'views/Tableau_DW1/General?:iid=1'
tabparams = ':embed=yes&:toolbar=no'
ticket = tableau_get_trusted_ticket(tabserver, tabuser, request.remote_ip)
if ticket != "-1"
url = "http://#{tabserver}/trusted/#{ticket}/#{tabpath}?#{tabparams}"
redirect_to url
return
end
render json: {}, status: :forbidden
end
end
In order to use the module it needs to be declared before the class. Also, and very important I changed the name of the file to tableau_trusted_controler.rb because the snake case that rails uses.

Issue regarding http POST api call using ruby code

I am going to access the Riskscreen api to authenticate users. To test the api I have written a ruby code snippet to make sample POST call to get the number of tokens I have from the Riskscreen api.
My code is:
require 'uri'
require 'net/http'
require 'net/https'
require 'json'
#toSend = {}.to_json
uri = URI.parse("https://api.riskscreen.com/api/v1/user/tokens")
https = Net::HTTP.new(uri.host,uri.port)
https.use_ssl = true
https.verify_mode = OpenSSL::SSL::VERIFY_NONE
header = {'api-key': 'my api key','Content-Type': 'application/json', 'Accept': 'application/json'}
req = Net::HTTP::Post.new(uri.path, header)
req.body = "[ #{#toSend} ]"
res = https.request(req)
puts "------------"
puts "Response #{res.code} #{res.message}: #{res.body}"
But I am getting the following error:
Response 400 Bad Request
If I change the header line to
header = {'api-key'=> 'my-api-key','Content-Type'=> 'application/json', 'Accept'=> 'application/json'}
then I am getting this error:
Response 401 Unauthorized
Sticking with this for a while. Please help me to sort out this.
Header's keys must be String instead of Symbol
header = {'api-key' => 'my api key','Content-Type' => 'application/json', 'Accept' => 'application/json'}
Another issue is net/http is capitalize header automatically, api-key -> Api-Key which cause Authorization Error on your server. One solution is to create new class to wrap api-key to prevent Ruby do that
class HeaderCaseSensitive < String
def capitalize
self
end
def split(*args)
super.each do |str|
HeaderCaseSensitive.new(str)
end
end
def to_s
self
end
end
Then change header:
header = {HeaderCaseSensitive.new('api-key') => 'xxxx','Content-Type' => 'application/json', 'Accept' => 'application/json'}
To sum up, following code will work:
require 'uri'
require 'net/http'
require 'net/https'
require 'json'
class HeaderCaseSensitive < String
def capitalize
self
end
def split(*args)
super.each do |str|
HeaderCaseSensitive.new(str)
end
end
def to_s
self
end
end
#toSend = {}.to_json
uri = URI.parse("https://api.riskscreen.com/api/v1/user/tokens")
https = Net::HTTP.new(uri.host,uri.port)
https.use_ssl = true
https.verify_mode = OpenSSL::SSL::VERIFY_NONE
header = {HeaderCaseSensitive.new('api-key') => 'xxx','Content-Type' => 'application/json', 'Accept' => 'application/json'}
https.set_debug_output($stdout)
req = Net::HTTP::Post.new(uri.path, header)
req.body = "[ #{#toSend} ]"
res = https.request(req)
puts "------------"
puts "Response #{res.code} #{res.message}: #{res.body}"
Can you try remove:
req.body = "[ #{#toSend} ]"
and replace by:
req.set_form_data({})
# or
req.body = "{}"
Sorry, I'm not sure about that.

Rails rewrite how to rewrite this code to ruby code?

I have a this class middleware:
class RedirectIt
require "net/https"
require "uri"
require 'open-uri'
APP_DOMAIN = 'http://www.mydomain.com'
def initialize(app)
#app = app
end
def call(env)
request = Rack::Request.new(env)
response = Rack::Response.new(env)
response.headers['Cache-Control'] = "public, max-age=#{84.hours.to_i}"
response.headers['Content-Type'] = 'image/png'
response.headers['Content-Disposition'] = 'inline'
response.body = "#{open('http://s3-eu-west-1.amazonaws.com/bucket/asdas.png').read}"
end
end
The problem is just that it gives the error:
Started GET "/?view=boks" for 127.0.0.1 at 2012-04-01 04:07:58 +0200
NoMethodError (You have a nil object when you didn't expect it!
You might have expected an instance of Array.
The error occurred while evaluating nil.[]=):
Are am doing something wrong? I have tried to rewrite this code I had in the controller:
def image_proxy
image_url = "http://s3-eu-west-1.amazonaws.com/bucket#{request.path}"
response.headers['Cache-Control'] = "public, max-age=#{84.hours.to_i}"
response.headers['Content-Type'] = 'image/png'
response.headers['Content-Disposition'] = 'inline'
render :text => open(image_url, "rb").read
end
The solution.
#PROXY BILLEDER
status, headers, response = #app.call(env)
headers['Cache-Control'] = "public, max-age=#{84.hours.to_i}"
headers['Content-Type'] = 'image/png'
headers['Content-Disposition'] = 'inline'
response_body = "#{(open('http://s3-eu-west-1.amazonaws.com/mybucket#{request.path()}')).read}"
[status, headers, response_body]

Parametrized get request in Ruby?

How do I make an HTTP GET request with parameters in Ruby?
It's easy to do when you're POSTing:
require 'net/http'
require 'uri'
HTTP.post_form URI.parse('http://www.example.com/search.cgi'),
{ "q" => "ruby", "max" => "50" }
But I see no way of passing GET parameters as a hash using 'net/http'.
Since version 1.9.2 (I think) you can actually pass the parameters as a hash to the URI::encode_www_form method like this:
require 'uri'
uri = URI.parse('http://www.example.com/search.cgi')
params = { :q => "ruby", :max => "50" }
# Add params to URI
uri.query = URI.encode_www_form( params )
and then fetch the result, depending on your preference
require 'open-uri'
puts uri.open.read
or
require 'net/http'
puts Net::HTTP.get(uri)
Use the following method:
require 'net/http'
require 'cgi'
def http_get(domain,path,params)
return Net::HTTP.get(domain, "#{path}?".concat(params.collect { |k,v| "#{k}=#{CGI::escape(v.to_s)}" }.join('&'))) if not params.nil?
return Net::HTTP.get(domain, path)
end
params = {:q => "ruby", :max => 50}
print http_get("www.example.com", "/search.cgi", params)
require 'net/http' require 'uri'
uri = URI.parse( "http://www.google.de/search" ); params = {'q'=>'cheese'}
http = Net::HTTP.new(uri.host, uri.port)
request = Net::HTTP::Get.new(uri.path)
request.set_form_data( params )
# instantiate a new Request object
request = Net::HTTP::Get.new( uri.path+ '?' + request.body )
response = http.request(request)
puts response.body
I would expect it to work without the second instantiation as it would be the first request-objects or the http-object's job but it worked for me this way.
Net::HTTP.get_print 'localhost', '/cgi-bin/badstore.cgi?searchquery=crystal&action=search&x=11&y=15'
or
uri = URI.parse("http://localhost")
req = Net::HTTP::Get.new("/cgi-bin/badstore.cgi?searchquery=crystal&action=search&x=11&y=15")
http = Net::HTTP.new(uri.host, uri.port)
response = http.start do |http|
http.request(req)
end
Use Excon:
conn = Excon.new('http://www.example.com/search.cgi')
conn.get(:query => { "q" => "ruby", "max" => "50" })
new to stack overflow and I guess I can't comment, but #chris.moose is missing double quotes in his function def. line 5 should be:
return Net::HTTP.get(domain, "#{path}?".concat(params.collect { |k,v| "#{k}=#{CGI::escape(v.to_s)}" }.reverse.join('&'))) if not params.nil?
or here's the whole thing redefined for copy/pasting
require 'net/http'
require 'cgi'
def http_get(domain,path,params)
return Net::HTTP.get(domain, "#{path}?".concat(params.collect { |k,v| "#{k}=#{CGI::escape(v.to_s)}" }.reverse.join('&'))) if not params.nil?
return Net::HTTP.get(domain, path)
end
<3
-mike
This is the easiest way to do it
require 'net/http' require 'uri'
#siteurl = "http://www.google.de/search/#{#yourquery}"
define your query
#yourquery = "whatever"
uri = URI.parse( "#siteurl" );
http = Net::HTTP.new(uri.host, uri.port)
request = Net::HTTP::Get.new(uri.path)
# instantiate a new Request object
request = Net::HTTP::Get.new( uri.path+ '?' + request.body )
response = http.request(request)
puts response.body
Might not be perfect but at least you get the idea.

Resources