I have a translation_api.rb file, and one of the body inputs is a text input, which I isolate in a function & name 'ticket_description'. However, when I use this variable in my 'make_request' method, the value is nil, and therefore the API returns me an empty translation. How do I ensure that the value is carried over?
How do I best take the 'ticket_description' value and sub it into my API request?
translation_api.rb
module Ccenter
module Utils
class TranslationApi
attr_accessor :cc_message_id
include Service
InvalidResponseError = Class.new(StandardError)
def initialize(cc_message_id)
#cc_message_id = cc_message_id
end
def call
grab_de_message_text(cc_message_id)
make_request
end
private
def make_request
response = Faraday.post('https://api.deepl.com/v2/translate', auth_key: '', text: #ticket_description, target_lang: 'DE', source_lang: 'EN')
if response.status == 200
body = response.body
message_element = body.split('"')[-2]
return message_element
else
raise InvalidResponseError unless response.success?
end
end
def grab_de_message_text(cc_message_id)
ticket = Ccenter::Adapters::Zendesk::TicketBase.new(zendesk_id: #cc_message_id).zendesk_ticket
ticket_data = ticket.pretty_inspect
refined_data = ticket_data.split(" ")
ticket_description = refined_data.select { |word| word.include? 'description' }
#ticket_description = ticket_description
end
end
end
end
EDIT w/ further details of 'ticket'
ticket.class => ZendeskAPI::Ticket
#<ZendeskAPI::Ticket {"url"=>"https://-staging.zendesk.com/api/v2/tickets/2116450.json", "id"=>2116450, "external_id"=>nil, "via"=>{"channel"=>"email", "source"=>{"from"=>{"address"=>"xxxx#pairfinance.com", "name"=>"xxxx"}, "to"=>
{"name"=>"Pair Finance GmbH", "address"=>"support#pairfinance-staging.zendesk.com"}, "rel"=>nil}},
"created_at"=>2021-11-05 12:25:36 UTC, "updated_at"=>2021-11-12 11:09:33 UTC, "type"=>"incident", "subject"=>"Add Participant Check", "raw_subject"=>"Add Participant Check", "description"=>"Testing participant addition", "priority"=>nil, "status"=>"open", "recipient"=>"support#pairfinance-staging.zendesk.com", "requester_id"=>387320355880, "submitter_id"=>387320355880,
"assignee_id"=>2944262769, "organization_id"=>375908938920, "group_id"=>24802909, "collaborator_ids"=>[], "follower_ids"=>[], "email_cc_ids"=>[], "forum_topic_id"=>nil, "problem_id"=>nil, "has_incidents"=>false, "is_public"=>true, "due_at"=>nil, "tags"=>["creditor_test1", "not_matched"], "custom_fields"=>[#<ZendeskAPI::Trackie id=27262705 value="">, #<ZendeskAPI::Trackie id=360022581580 value="">, #<ZendeskAPI::Trackie id=360000824460 value=nil>, #<ZendeskAPI::Trackie id=45203605 value="">, #<ZendeskAPI::Trackie id=360000038229 value="">, #
<ZendeskAPI::Trackie id=360022718000 value="">, #<ZendeskAPI::Trackie id=360022689799 value="27">, #
<ZendeskAPI::Trackie id=360022840199 value="2021-11-12">, #<ZendeskAPI::Trackie id=1900000581074 value="0.0">, #<ZendeskAPI::Trackie id=360022823559 value="">, #
<ZendeskAPI::Trackie id=1900000642554 value=false>, #<ZendeskAPI::Trackie id=360022857519 value=nil>, #<ZendeskAPI::Trackie id=360022738860 value=nil>, #
<ZendeskAPI::Trackie id=1900000693194 value="">, #<ZendeskAPI::Trackie id=1900000768974 value=nil>, #<ZendeskAPI::Trackie id=360022886360 value=nil>, #<ZendeskAPI::Trackie id=1900000752094 value=nil>], "satisfaction_rating"=>nil, "sharing_agreement_ids"=>[], "fields"=>[#<ZendeskAPI::Trackie id=27262705 value="">, #<ZendeskAPI::Trackie id=360022581580 value="">, #<ZendeskAPI::Trackie id=360000824460 value=nil>, #<ZendeskAPI::Trackie id=45203605 value="">, #<ZendeskAPI::Trackie id=360000038229 value="">, #<ZendeskAPI::Trackie id=360022718000 value="">, #<ZendeskAPI::Trackie id=360022689799 value="27">, #<ZendeskAPI::Trackie id=360022840199 value="2021-11-12">, #<ZendeskAPI::Trackie id=1900000581074 value="0.0">, #<ZendeskAPI::Trackie id=360022823559 value="">, #<ZendeskAPI::Trackie id=1900000642554 value=false>, #<ZendeskAPI::Trackie id=360022857519 value=nil>, #<ZendeskAPI::Trackie id=360022738860 value=nil>, #<ZendeskAPI::Trackie id=1900000693194 value="">, #<ZendeskAPI::Trackie id=1900000768974 value=nil>, #<ZendeskAPI::Trackie id=360022886360 value=nil>, #<ZendeskAPI::Trackie id=1900000752094 value=nil>], "followup_ids"=>[], "brand_id"=>721889, "allow_channelback"=>false, "allow_attachments"=>true}>
Related
I am trying to add a search bar. I have also set the path. But everytime I try to click on search it directs me to this error. What is the error in this code?
This is my Inbox_Controller file. It says that the action 'Search' cannot be found in InboxController.
class InboxController < ApplicationController
before_action :valid_membership
before_action :change_password_next_login
before_action :agreed_to_terms
before_action :allowed_send_mail?
layout 'inbox'
def bulk
puts params
ids = params[:bulk_ids]
if ids
params[:commit]
case params[:commit]
when 'Archive'
ids.each do |id|
message = Message.find(id)
message.archived = true
message.save()
end
when 'Restore'
ids.each do |id|
message = Message.find(id)
message.archived = false
message.save()
end
else
puts 'invalid action!!'
end
if params[:folder] != ''
redirect_to inbox_index_path(folder: params[:folder])
else
redirect_to inbox_index_path
end
else
flash[:alert] = t('errors.inbox.no_emails_selected')
redirect_to :back
end
end
def index
per_page = 10
page = params[:page] ? params[:page] : 1
#inbox = Inbox.search(params[:search])
case params[:folder]
when 'archive'
#messages = current_user.archived_messages(page, per_page)
when 'drafts'
#messages = current_user.draft_messages(page, per_page)
when 'sent'
#messages = current_user.sent_messages(page, per_page)
else
#messages = current_user.received_messages(page, per_page)
end
end
def reply
original = Message.find(params[:id])
#quoted = "\n\nOn #{original.sent_time.strftime("%m/%d/%y %-I:%M %p")}, # {original.from.full_name} wrote:\n----------------\n#{original.body}"
#message = Message.new(
:parent => original,
:to => original.from,
:subject => "RE: #{original.subject}",
:body => #quoted,
)
render :compose
end
def move
#message = Message.find(params[:id])
folder = params[:destination]
case folder
when 'archive'
#message.archived = true
else
#message.archived = false
end
unless #message.save
puts #message.errors.full_messages
end
redirect_to inbox_index_path(folder: folder)
end
def show
#message = Message.find(params[:id])
if !#message.read? && #message.to == current_user
#message.read_time = DateTime.now
unless #message.save
puts #message.errors.full_messages
end
end
end
def edit
#message = Message.find(params[:id])
#message.to_name = #message.to.full_name
render 'compose'
end
def compose
#message = Message.new
if(params[:id])
#message.to = Mentor.find(params[:id])
end
end
def create
if(params[:message] && !params[:message][:id].empty?)
#message = Message.find(params[:message][:id])
#message.assign_attributes(message_params)
else
#message = Message.new(message_params)
end
if params[:parent_id] && !params[:parent_id].empty?
#message.parent = Message.find(params[:parent_id])
#message.replied_to_time = Time.now
end
#message.from = current_user
draft = params[:draft]
if draft
#message.draft = true
else
#message.sent_time = Time.now
#message.draft = false
end
# puts #message.as_json
if can_send_mail
if #message.save
if !draft
if current_user_or_guest.mentee?
current_user.credits += -1
current_user.save
end
UserMailer.inmail_notification(#message).deliver
end
redirect_to inbox_index_path(folder: draft ? 'drafts' : 'inbox'), notice: "Message successfully #{draft ? 'saved' : 'sent'}."
else
flash.now[:alert] = 'All email fields need to be filled out prior to sending/saving.'
render 'compose'
end
else
flash.now[:alert] = 'You do not have enough credits to send any more InMail to Game Changers.'
render 'compose'
end
ActivityLog.create(userid: current_user.id, points: 500, typeof: "message")
end
def allowed_send_mail?
unless !current_user.admin?
msg = "You are not authorized to access this page!"
show_error(msg)
end
end
def profile_complete
return true if current_user.mentor?
unless current_user.profile_complete?
flash[:alert] = t('errors.inbox.incomplete_profile')
redirect_to edit_user_registration_path
end
end
def message_params
params.require(:message).permit(:id, :to_name, :to_id, :subject, :body)
end
end
This is my relevant index.erb.html file.
<%= form_tag inbox_search_path, :method => 'get' do %>
<p>
<%= search_field_tag :Search, params[:search] %>
<%= submit_tag "Search", :name => nil %>
</p>
<% end %>
This is my relevant routes.rb file:
get 'inbox' => 'inbox#index', :as => 'inbox_index'
get 'inbox/show/:id' => 'inbox#show', :as => 'inbox_show'
get 'inbox/compose' => 'inbox#compose', :as => 'inbox_compose'
get 'inbox/compose/:id' => 'inbox#compose', :as => 'inbox_compose_to'
get 'inbox/edit/:id' => 'inbox#edit', :as => 'inbox_edit'
get 'inbox/move' => 'inbox#move', :as => 'inbox_move'
get 'inbox/reply' => 'inbox#reply', :as => 'inbox_reply'
get 'inbox/search' => 'inbox#search', :as => 'inbox_search'
post 'inbox/create' => 'inbox#create'
post 'inbox/bulk' => 'inbox#bulk'
There is no search method in this controller, the only search I see is a call to Inbox.search.
To debug this, start with the view where you actually do the "click". Is that click really supposed to trigger an action in your InboxController? If you think it should, why is there no action in that controller? If not, then the "click" was meant to go to another controller that actually would handle the search action, in which case you need to figure out why the "click" is trying to call a method in your InboxController rather than the desired controller. The problem could be something in your view or something in you routes, or you really should have that method in you InboxController, either way I suggest you try to figure out at least what should be happening and then post some more code stating what you think should be happening vs what is really happening.
I try to define own 'context' method in Rspec.
Have next:
module MiscSpecHelper
def its_ok
context "if everything is OK" do
yield
end
end
end
in spec file:
describe "GET index" do
its_ok do
it "has a 200 status code" do
get :index
expect(response.status).to eq(200)
end
end
end
I got:
GET index
has a 200 status code
I expect:
GET index
if everything is OK
has a 200 status code
Why does it ignore my 'context' description?
module MiscSpecHelper
def its_ok(&block)
context "if everything is OK", &block
end
end
In the following code i want to handle the exception.if msg[0] not found i have to catch that exception message msg[2] in rescue and if it is found put the success message msg[1]
puts "Verifying Home Page"
def verifyHomepage(*args)
begin
args.each do |msg|
page.find(msg[0])
puts msg[1]
rescue
puts msg[2]
end
end
end
verifyHomepage(['#logoAnchorr', 'logo anchor found', 'Logo anchor not Found'], ['.navbar-inner', 'Header Bar found', 'Header Bar not Found'])
In the above code iam getting
error sysntax error unexpected keyword rescue expecting keyword end
Salil has pointed you where to fix,that's correct. Now The below approach also you could adapt:
puts "Verifying Home Page"
def verifyHomepage(*args)
args.each do |msg|
next puts(msg[1]) if page.find(msg[0]) rescue nil
puts msg[2]
end
end
a = [['#logoAnchorr', 'logo anchor found', 'Logo anchor not Found'], ['.navbar-inner', 'Header Bar found', 'Header Bar not Found']]
verifyHomepage(*a)
Output:
Verifying Home Page
Logo anchor not Found
Header Bar not Found
You have to write begin inside the block
puts "Verifying Home Page"
def verifyHomepage(*args)
args.each do |msg|
begin
page.find(msg[0])
puts msg[1]
rescue
puts msg[2]
end
end
end
verifyHomepage(['#logoAnchorr', 'logo anchor found', 'Logo anchor not Found'], ['.navbar-inner', 'Header Bar found', 'Header Bar not Found'])
Am trying to implement amazon SNS using ruby.
I want to create a topic,delete a topic,subscribe to a topic,publish to a topic.These are included in the following code.
#!/usr/bin/env ruby
require 'rubygems'
require 'aws-sdk'
AWS.config(:access_key_id => 'BT62W53Q', :secret_access_key => '0Edwg')
#sns=AWS::SNS.new
#D requirements
alpha = #sns.topics.create('CSC470Test-Alpha')
#sns.topics.create('CSC470Test-Beta')
temp=gets
#sns.topics.each do |topic|
puts topic.name
if(topic.name=='CSC470Test-Beta')
topic.delete
end
end
puts
puts 'Beta now deleted.'
puts
#sns.topics.each do |topic|
puts topic.name
end
puts
temp=gets
puts
#C requirements
#sns.topics.each do |topic|
if(topic.name=='CSC470Test-Alpha')
subbed1=false
subbed2=false
subbed3=false
topic.subscriptions.each do |sub|
if(sub.endpoint=='sn#aine.com')
subbed1=true;
end
if(sub.endpoint=='pran#aine.com')
subbed2=true;
end
if(sub.endpoint=='http://cloud.comtor.org/csc470logger/logger')
subbed3=true;
end
end
if(!subbed1)
puts 'Subscribed sika.'
topic.subscribe('sn#aine.com')
end
if(!subbed2)
puts 'Subscribed prka'
topic.subscribe('pran#aine.com', :json => true)
end
if(!subbed3)
puts 'Subscribed comtor site.'
topic.subscribe('http://cloud.comtor.org/csc470logger/logger')
end
end
end
temp=gets
puts 'Topics with info:'
#sns.topics.each do |topic|
puts
puts 'Arn'
puts topic.arn
puts 'Owner'
puts topic.owner
puts 'Policy'
puts topic.policy
puts 'Name'
puts topic.display_name
puts 'Confirmed Subscriptions:'
puts topic.subscriptions.
select{ |s| s.arn != 'PendingConfirmation' }.
map(&:endpoint)
# if(subs.confirmation_authenticated?)
# puts 'Arn: ' + subs.arn
# puts 'Endpoint: ' + subs.endpoint
# puts 'Protocol: ' + subs.protocol
# end
end
puts
temp=gets
#sns.subscriptions.each do |subs|
puts "SubscriptionARN: #{ subs.arn} "
puts "TopicARN: #{subs.topic_arn} "
puts "Owner: #{subs.owner_id} "
puts "Delivery Policy: #{ subs.delivery_policy_json} "
end
while running this code in rails console. iam getting this error
C:/ProgramData/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/aws-sdk-1.8.5/l
ib/aws/core/client.rb:339:in `return_or_raise': The request signature we calcula
ted does not match the signature you provided. Check your AWS Secret Access Key
and signing method. Consult the service documentation for details. (AWS::SNS::Er
rors::SignatureDoesNotMatch)
from C:/ProgramData/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/aw
s-sdk-1.8.5/lib/aws/core/client.rb:440:in `client_request'
from (eval):3:in `create_topic'
from C:/ProgramData/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/aw
s-sdk-1.8.5/lib/aws/sns/topic_collection.rb:24:in `create'
I'm doing a simple Net:LDAP search and when I'm outputting an entry's attribute that may not exist for every entry, I get an error "NoMethodError: undefined method 'some_attribute'"
Here is the code:
require 'rubygems'
require 'net/ldap'
ldap = Net::LDAP.new
ldap.host = 'ldap.example.com'
ldap.port = 389
if ldap.bind
filter = Net::LDAP::Filter.eq( "sn", "Smith" )
treebase = "ou=people,o=company"
ldap.search( :base => treebase, :filter => filter, :return_result => false) do |entry|
puts #{entry.some_attribute}
end
end
else
puts "bind unsuccessful"
end
I tried also doing:
if entry.respond_to?(some_attribute)
puts "#{entry.some_attribute}"
end
That didn't work, it returns as false for every entry (when some entries have the attribute).
Ruby is expecting a symbol in the respond_to? method call.
ruby-1.8.7-p299 > class Foo
ruby-1.8.7-p299 ?> attr_accessor :some_attr
ruby-1.8.7-p299 ?> end
=> nil
ruby-1.8.7-p299 > Foo.new.respond_to?(some_attr)
NameError: undefined local variable or method `some_attr' for #<Object:0xb77ce950>
from (irb):4
ruby-1.8.7-p299 > Foo.new.respond_to?(:some_attr)
=> true