I have to post data from one rails app to another and get a response in return from the latter app to the former.Both are RESTFUL apps
my api to post data from the former app is as follows
class OrderApiController < ApiController
def send_invoice_data
response = Hash.new
result = Hash.new
order = Order.includes(:status, :user, payment:
[:status]).where(txnid: params[:txnid]).first
if order
if (['Notary', 'Attestation','Franking'].include?
order.service.name)
no_of_copies = ((order.answers.where(question_id:
[37,15]).length > 0) ? order.answers.where(question_id:
[37,15]).first.body : 0).to_i
else
no_of_copies = ((order.answers.where(question_id:
[37,15]).length > 0) ? order.answers.where(question_id:
[37,15]).first.body : 0).to_i + 1
end
result['order'] = {
id: order.id,
txnid: order.txnid,
service_name: order.service.name,
:
:
discount: order.discount || '',
stamp_amount: order.stamp_amount || '',
delivery_amount: order.delivery_amount || '',
no_of_copies: no_of_copies
}
response.merge! ApiStatusList::OK
response['result'] = result
else
response.merge! ApiStatusList::INVALID_REQUEST
end
render :json => response
end
end
The controller code for the latter app is as follows:
class InvoiceApiController < ApiController
def order_invoice
response = Hash.new
result = Hash.new
debugger
if params[:order] && params[:order][:txnid]
@order = params[:order]
@order['stamp_amount'] = params[:order][:stamp_amount] || ''
@order['txnid'] = params[:order][:txnid]
@order['service_name'] = params[:order][:service_name] || ''
@order['payment_date'] = params[:order][:payment_date]
:
:
@order['discount'] = params[:order][:discount] || ''
@no_of_copies = params[:order][:no_of_copies]
pdf = WickedPdf.new.pdf_from_string(
render_to_string(template:
'invoices/generate_invoice.pdf.erb', filename: params[:order]
[:txnid] + ".pdf" ,
type: 'application/pdf', disposition:
'attachment', print_media_type: true))
save_path = Rails.root.join('pdfs', @order['txnid'] + ".pdf")
File.open(save_path, 'wb') do |file|
file << pdf
filename = @order['txnid'] + ".pdf"
end
file_name = @order['txnid'] + ".pdf"
upload = Invoice.upload(save_path, file_name)
response['result'] = upload
response.merge! ApiStatusList::OK
else
response.merge! ApiStatusList::INVALID_REQUEST
end
render :json => response
end
The second app sends the link of the pdf generated invoice as response back to the former app.
The link for the later app would like this:
http://192.168.0.104:3000/api/v0/generate_invoice?key=value
How do I achive this. I am a rails novice quite new to RESTFUL api development as well.So please help with elaboration for the solution.
I found a solution by using Httparty gem. This what I did
gem 'httparty'
in gemfile of both apps
bundle install
in console
The modified send_invoice method is below
def send_invoice_data
response = Hash.new
result = Hash.new
debugger
order = Order.includes(:status, :user, payment:
[:status]).where(txnid: params[:txnid]).first
if order
if (['Notary', 'Attestation','Franking'].include?
order.service.name)
no_of_copies = ((order.answers.where(question_id:
[37,15]).length > 0) ? order.answers.where(question_id:
[37,15]).first.body : 0).to_i
else
no_of_copies = ((order.answers.where(question_id:
[37,15]).length > 0) ? order.answers.where(question_id:
[37,15]).first.body : 0).to_i + 1
end
response.merge! ApiStatusList::OK
response =
HTTParty.post('http://localhost:3001/api/v0/generate_invoice?
key=docket', :body => { "order" => {
"id" => order.id,
"txnid" => order.txnid,
"service_name" => order.service.name,
"payment_date" => order.payment ?
order.payment.created_at.strftime('%d/%m/%Y') :
order.created_at.strftime('%d/%m/%Y'),
:
:
"delivery_amount" => order.delivery_amount || '',
"no_of_copies" => no_of_copies}}.to_json, :headers => {
'Content-Type' => 'application/json'})
else
response.merge! ApiStatusList::INVALID_REQUEST
end
render :json => response
end
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.