How do I incorporate this Ruby file into Rails? -
i have ruby file:
freshdesk.rb:
require "rest_client" require 'nokogiri' class freshdesk # custom errors class alreadyexistederror < standarderror; end class connectionerror < standarderror; end attr_accessor :base_url def initialize(base_url, username, password) @base_url = base_url restclient.add_before_execution_proc | req, params | req.basic_auth username, password end end # freshdesk api client support "get" id parameter optional # returns nil if there no response def self.fd_define_get(name, *args) name = name.to_s method_name = "get_" + name define_method method_name |*args| uri = mapping(name) uri.gsub!(/.xml/, "/#{args}.xml") if args.size > 0 begin response = restclient.get uri rescue exception response = nil end end end # freshdesk api client support "delete" required id parameter def self.fd_define_delete(name, *args) name = name.to_s method_name = "delete_" + name define_method method_name |args| uri = mapping(name) raise standarderror, "an id required delete" if args.size.eql? 0 uri.gsub!(/.xml/, "/#{args}.xml") restclient.delete uri end end # freshdesk api client support "post" optional key, value parameter # # throw: # alreadyexistederror if there exact copy of data in server # connectionerror if there connection problem server def self.fd_define_post(name, *args) name = name.to_s method_name = "post_" + name define_method method_name |args| raise standarderror, "arguments required modify data" if args.size.eql? 0 uri = mapping(name) builder = nokogiri::xml::builder.new |xml| xml.send(doc_name(name)) { args.each |key, value| xml.send(key, value) end } end begin response = restclient.post uri, builder.to_xml, :content_type => "text/xml" rescue restclient::unprocessableentity raise alreadyexistederror, "entry existed" rescue restclient::internalservererror raise connectionerror, "connection server failed. please check hostname" rescue restclient::found raise connectionerror, "connection server failed. please check username/password" rescue exception => e3 raise end response end end # freshdesk api client support "put" key, value parameter # # throw: # connectionerror if there connection problem server def self.fd_define_put(name, *args) name = name.to_s method_name = "put_" + name define_method method_name |args| raise standarderror, "arguments required modify data" if args.size.eql? 0 raise standarderror, "id required modify data" if args[:id].nil? uri = mapping(name) builder = nokogiri::xml::builder.new |xml| xml.send(doc_name(name)) { args.each |key, value| xml.send(key, value) end } end begin uri.gsub!(/.xml/, "/#{args[:id]}.xml") response = restclient.put uri, builder.to_xml, :content_type => "text/xml" rescue restclient::internalservererror raise connectionerror, "connection server failed. please check hostname" rescue restclient::found raise connectionerror, "connection server failed. please check username/password" rescue exception => e3 raise end response end end [:tickets, :ticket_fields, :users, :forums, :solutions, :companies].each |a| fd_define_get fd_define_post fd_define_delete fd_define_put end # mapping of object name url: # tickets => helpdesk/tickets.xml # ticket_fields => /ticket_fields.xml # users => /contacts.xml # forums => /categories.xml # solutions => /solution/categories.xml # companies => /customers.xml def mapping(method_name) path = case method_name when "tickets" file.join(@base_url + "helpdesk/tickets.xml") when "ticket_fields" file.join( @base_url, "ticket_fields.xml") when "users" file.join(@base_url, "contacts.xml") when "forums" file.join(@base_url + "categories.xml") when "solutions" file.join(@base_url + "solution/categories.xml") when "companies" file.join(@base_url + "customers.xml") end end # match root name of xml document freskdesk uses def doc_name(name) doc = case name when "tickets" "helpdesk_ticket" when "ticket_fields" "helpdesk-ticket-fields" when "users" "user" when "companies" "customer" else raise standarderror, "no root object call" end end end
to use do:
client = freshdesk.new("http://website.freshdesk.com/", "api key", "x") # example posts ticket client.post_tickets(:subject => 'this rails app',:email => "mytestemail@example.com", :description => "this ruby file bitches", :name => "richard ahn", :source => 2, :priority => 2, :name => "joshua siler")
then, in console do:
$ ruby freshdesk.rb
the information gets called client.post_tickets
gets posted freshdesk dashboard.
how integrate rails app?
where put file class name freshdesk
?
what do with:
client = freshdesk.new client.post_tickets
how link form user can submit?
file should go in lib
folder.
now, data form, i'll first create form.
<%= form_for(@client) |f| %> <%= f.label :subject %> <%= f.text_field :subject %> <%= f.label :name%> <%= f.text_field :name %> <%= f.label :description %> <%= f.text_field :description %> ... <% end %>
and in controller @client
post
new record (create method) enter code
def create client = freshdesk.new "http://website.freshdesk.com/", "api key", "x" client.post_tickets(params[:client]) ... end
Comments
Post a Comment