Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I incorporate this Ruby file into Rails?
    primarykey
    data
    text
    <p>I have this Ruby file:</p> <p>freshdesk.rb:</p> <pre><code> require "rest_client" require 'nokogiri' class Freshdesk # custom errors class AlreadyExistedError &lt; StandardError; end class ConnectionError &lt; StandardError; end attr_accessor :base_url def initialize(base_url, username, password) @base_url = base_url RestClient.add_before_execution_proc do | req, params | req.basic_auth username, password end end # Freshdesk API client support "GET" with id parameter optional # Returns nil if there is no response def self.fd_define_get(name, *args) name = name.to_s method_name = "get_" + name define_method method_name do |*args| uri = mapping(name) uri.gsub!(/.xml/, "/#{args}.xml") if args.size &gt; 0 begin response = RestClient.get uri rescue Exception response = nil end end end # Freshdesk API client support "DELETE" with the required id parameter def self.fd_define_delete(name, *args) name = name.to_s method_name = "delete_" + name define_method method_name do |args| uri = mapping(name) raise StandardError, "An ID is required to delete" if args.size.eql? 0 uri.gsub!(/.xml/, "/#{args}.xml") RestClient.delete uri end end # Freshdesk API client support "POST" with the optional key, value parameter # # Will throw: # AlreadyExistedError if there is exact copy of data in the server # ConnectionError if there is connection problem with the server def self.fd_define_post(name, *args) name = name.to_s method_name = "post_" + name define_method method_name do |args| raise StandardError, "Arguments are required to modify data" if args.size.eql? 0 uri = mapping(name) builder = Nokogiri::XML::Builder.new do |xml| xml.send(doc_name(name)) { args.each do |key, value| xml.send(key, value) end } end begin response = RestClient.post uri, builder.to_xml, :content_type =&gt; "text/xml" rescue RestClient::UnprocessableEntity raise AlreadyExistedError, "Entry already existed" rescue RestClient::InternalServerError raise ConnectionError, "Connection to the server failed. Please check hostname" rescue RestClient::Found raise ConnectionError, "Connection to the server failed. Please check username/password" rescue Exception =&gt; e3 raise end response end end # Freshdesk API client support "PUT" with key, value parameter # # Will throw: # ConnectionError if there is connection problem with the server def self.fd_define_put(name, *args) name = name.to_s method_name = "put_" + name define_method method_name do |args| raise StandardError, "Arguments are required to modify data" if args.size.eql? 0 raise StandardError, "id is required to modify data" if args[:id].nil? uri = mapping(name) builder = Nokogiri::XML::Builder.new do |xml| xml.send(doc_name(name)) { args.each do |key, value| xml.send(key, value) end } end begin uri.gsub!(/.xml/, "/#{args[:id]}.xml") response = RestClient.put uri, builder.to_xml, :content_type =&gt; "text/xml" rescue RestClient::InternalServerError raise ConnectionError, "Connection to the server failed. Please check hostname" rescue RestClient::Found raise ConnectionError, "Connection to the server failed. Please check username/password" rescue Exception =&gt; e3 raise end response end end [:tickets, :ticket_fields, :users, :forums, :solutions, :companies].each do |a| fd_define_get a fd_define_post a fd_define_delete a fd_define_put a end # Mapping of object name to url: # tickets =&gt; helpdesk/tickets.xml # ticket_fields =&gt; /ticket_fields.xml # users =&gt; /contacts.xml # forums =&gt; /categories.xml # solutions =&gt; /solution/categories.xml # companies =&gt; /customers.xml def mapping(method_name) path = case method_name when "tickets" then File.join(@base_url + "helpdesk/tickets.xml") when "ticket_fields" then File.join( @base_url, "ticket_fields.xml") when "users" then File.join(@base_url, "contacts.xml") when "forums" then File.join(@base_url + "categories.xml") when "solutions" then File.join(@base_url + "solution/categories.xml") when "companies" then File.join(@base_url + "customers.xml") end end # match with the root name of xml document that freskdesk uses def doc_name(name) doc = case name when "tickets" then "helpdesk_ticket" when "ticket_fields" then "helpdesk-ticket-fields" when "users" then "user" when "companies" then "customer" else raise StandardError, "No root object for this call" end end end </code></pre> <p>To use it I do:</p> <pre><code>client = Freshdesk.new("http://website.freshdesk.com/", "API KEY", "X") # example posts a ticket client.post_tickets(:subject =&gt; 'This is from the rails app',:email =&gt; "mytestemail@example.com", :description =&gt; "This is from the ruby file bitches", :name =&gt; "Richard Ahn", :source =&gt; 2, :priority =&gt; 2, :name =&gt; "Joshua Siler") </code></pre> <p>Then, in my console I do:</p> <pre><code>$ ruby freshdesk.rb </code></pre> <p>The information that gets called with <code>client.post_tickets</code> gets posted to my Freshdesk dashboard.</p> <p>How do I integrate this into my Rails app?</p> <p>Where do I put the file with the class name <code>FRESHDESK</code>?</p> <p>What do I do with:</p> <pre><code>client = Freshdesk.new client.post_tickets </code></pre> <p>How do I link this up with a form that the user can submit?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload