Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 如何将这个Ruby文件合并到Rails中?_Ruby On Rails_Ruby_Ruby On Rails 3_Api - Fatal编程技术网

Ruby on rails 如何将这个Ruby文件合并到Rails中?

Ruby on rails 如何将这个Ruby文件合并到Rails中?,ruby-on-rails,ruby,ruby-on-rails-3,api,Ruby On Rails,Ruby,Ruby On Rails 3,Api,我有一个Ruby文件: 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

我有一个Ruby文件:

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 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 > 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 => "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 => 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 => "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 => 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 => 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" 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
然后,在我的控制台中,我执行以下操作:

$ ruby freshdesk.rb
通过
client.post_tickets
调用的信息将发布到我的Freshdesk仪表板

如何将其集成到Rails应用程序中

我应该把类名为FRESHDESK的文件放在哪里

我该如何处理:

client = Freshdesk.new 
client.post_tickets 

如何将此链接到用户可以提交的表单?

文件应放在
lib
文件夹中

现在,为了从表单中获取数据,我将首先创建一个表单

<%= form_for(@client) do |f| %>
  <%= f.label :subject %>
  <%= f.text_field :subject %>
  <%= f.label :name%>
  <%= f.text_field :name %>
  <%= f.label :description %>
  <%= f.text_field :description %>
  ...
<% end %>

为什么不把它添加到项目中呢?文件应该放在
lib
文件夹中。请阅读。当您创建问题时,它应该是复制问题所需的最低代码和数据。如果我只是通过表单将其添加到我的项目中,我将如何使用它?tadman?我也需要在路由中创建一个资源吗?还有,它说client=Freshdesk。new我也需要在那里添加我的api键,所以它应该看起来像client=Freshdesk。new(“,”api键“,”X”),我假设@client是一个资源。我已经为第二条评论更新了代码。很抱歉造成混淆。你认为你能告诉我你将如何设置路由吗……我想这就是我的问题所在……我想我的显示操作也有问题我得到了一个未定义的方法“model_name”我是否必须以某种方式包含lib文件?
<%= form_for(@client) do |f| %>
  <%= f.label :subject %>
  <%= f.text_field :subject %>
  <%= f.label :name%>
  <%= f.text_field :name %>
  <%= f.label :description %>
  <%= f.text_field :description %>
  ...
<% end %>
def create
  client = Freshdesk.new "http://website.freshdesk.com/", "API KEY", "X"
  client.post_tickets(params[:client])
  ...
end