Ruby on rails 在哪里使用HTTParty调用给定用户输入的API?
我试图创建一个使用用户输入的页面,并使用HTTParty调用堆栈交换API来返回信息。我应该将下面的文件放在哪里,如其中一个HTTParty中所示,以便HTTParty能够调用Ruby on rails 在哪里使用HTTParty调用给定用户输入的API?,ruby-on-rails,rest,httprequest,httparty,Ruby On Rails,Rest,Httprequest,Httparty,我试图创建一个使用用户输入的页面,并使用HTTParty调用堆栈交换API来返回信息。我应该将下面的文件放在哪里,如其中一个HTTParty中所示,以便HTTParty能够调用StackExchange.new(user\u input,1)(其中user\u input是用户在前端提供的某个变量)?另外,在Rails中是否有更通用的调用API的方法?我试图从多个API检索信息,并将所有这些信息返回给用户。我可以将多个get请求合并到一个类中吗 代码: 我应该把文件放在哪里 有几个选择。就我个人
StackExchange.new(user\u input,1)
(其中user\u input
是用户在前端提供的某个变量)?另外,在Rails中是否有更通用的调用API的方法?我试图从多个API检索信息,并将所有这些信息返回给用户。我可以将多个get请求合并到一个类中吗
代码:
我应该把文件放在哪里
有几个选择。就我个人而言,我会在app
目录下创建一个services
目录,并将其放入其中。这样,rails将自动加载文件。而且,我倾向于称之为stackexchangeseservice
(有点像Controllers
最后总是有Controllers
),但有些人不喜欢这样
在Rails中有没有更通用的调用API的方法
我不知道这是什么意思
我可以将多个get请求合并到一个类中吗
当然,你可以这样做:
class MultiFetchService
attr_accessor :args
class << self
def call(args={})
new(args).call
end
end # Class Methods
#==============================================================================================
# Instance Methods
#==============================================================================================
def initialize(args)
@args = args || {}
assign_args
end
def call
{
stack_exchange: StackExchange(user_input, user_id)
foo: FooService(some, other, args)
}
end
private
def assign_args
args.each do |k,v|
class_eval do
attr_accessor k
end
send("#{k}=",v)
end
end
end
由于使用了assign\u args
方法,对于发送到multifitchservice
的每个密钥,您的服务中将有一个方法(在本例中,user\u input
,user\u id
,部分
,其他
,args
)
我应该把文件放在哪里
有几个选择。就我个人而言,我会在app
目录下创建一个services
目录,并将其放入其中。这样,rails将自动加载文件。而且,我倾向于称之为stackexchangeseservice
(有点像Controllers
最后总是有Controllers
),但有些人不喜欢这样
在Rails中有没有更通用的调用API的方法
我不知道这是什么意思
我可以将多个get请求合并到一个类中吗
当然,你可以这样做:
class MultiFetchService
attr_accessor :args
class << self
def call(args={})
new(args).call
end
end # Class Methods
#==============================================================================================
# Instance Methods
#==============================================================================================
def initialize(args)
@args = args || {}
assign_args
end
def call
{
stack_exchange: StackExchange(user_input, user_id)
foo: FooService(some, other, args)
}
end
private
def assign_args
args.each do |k,v|
class_eval do
attr_accessor k
end
send("#{k}=",v)
end
end
end
由于使用了
assign\u args
方法,对于发送到multivetchservice
(在本例中,user\u input
,user\u id
,部分
,其他
,args
)的每个密钥,您都会有一个方法(在ruby中,您可以编写:
#some_file.rb:
class A
def initialize(x)
@x = x
end
def dostuff
puts "doing stuff with #{@x}"
end
end
class B
def go
a = A.new(10)
a.dostuff
end
end
b = B.new
b.go
--output:--
doing stuff with 10
这表明一个文件中定义的类可以使用同一文件中定义的另一个类
您还可以将类的名称更改为:
class StackExchange
def initialize(x)
@x = x
end
def dostuff
puts "doing stuff with #{@x}"
end
end
class UsersController
def go
a = StackExchange.new(10)
a.dostuff
end
end
b = UsersController.new
b.go
--output:--
doing stuff with 10
在rails应用程序中,rails负责执行以下行:
b = UsersController.new
b.go
当您创建到控制器35; action
的路由,然后在浏览器中输入匹配路径时,rails将创建控制器对象并调用action方法
您所要做的就是在控制器类中编写操作方法:
class UsersController
#You write this stuff:
def go
a = StackExchange.new(10)
a.dostuff
end
end
因此,对于一个非常简单的解决方案,您可以将StackExchange类粘贴到包含控制器类的文件的顶部,然后在控制器的任何操作/方法中,您可以调用StackExchange.new
但是,如果某些其他控制器中的操作(=方法)也需要使用StackExchange类,则可以将StackExchange类放在名为stack\u exchange.rb
的文件中,而不是将StackExchange类粘贴到每个控制器文件的顶部,并将文件放在一个中心位置,所有控制器都可以访问该文件
中心位置的一个选择是lib/
目录。您可以在lib/
目录中创建一个子目录,并将stack_exchange.rb文件放在其中:
lib/my_classes/stack_exchange.rb
然后,在包含一个需要使用StackExchange类的控制器类的文件顶部,您只需编写:
require 'my_classes/stack_exchange'
这比必须将整个StackExchange类粘贴到文件顶部要短
但有一个更简单的解决方案。在ruby中,一旦需要一个类,就不需要再次需要它,因此实际上不需要将require语句放在需要使用StackExchange类的每个控制器文件的顶部。相反,您可以使用配置文件要求StackExchange类一次,然后不必将require语句粘贴到任何控制器文件的顶部:
#config/initializers/my_require_classes.rb
require 'my_classes/stack_exchange'
请注意,如果对配置文件进行任何更改,则必须重新启动服务器。事实上,我发现require语句非常棘手,因此如果我更改require语句,有时我需要重新启动服务器
StackExchange类的另一个中心位置选择在
app/
目录中。在rails 5
中,app/
目录是自动加载的,这意味着rails为您创建require语句,您可以愉快地使用放置在app/
目录中的类,而不必担心require语句。在ruby中,您可以编写:
#some_file.rb:
class A
def initialize(x)
@x = x
end
def dostuff
puts "doing stuff with #{@x}"
end
end
class B
def go
a = A.new(10)
a.dostuff
end
end
b = B.new
b.go
--output:--
doing stuff with 10
这表明一个文件中定义的类可以使用同一文件中定义的另一个类
您还可以将类的名称更改为:
class StackExchange
def initialize(x)
@x = x
end
def dostuff
puts "doing stuff with #{@x}"
end
end
class UsersController
def go
a = StackExchange.new(10)
a.dostuff
end
end
b = UsersController.new
b.go
--output:--
doing stuff with 10
在rails应用程序中,rails负责执行以下行:
b = UsersController.new
b.go
当您创建到控制器35; action
的路由,然后在浏览器中输入匹配路径时,rails将创建控制器对象并调用action方法
您所要做的就是在控制器类中编写操作方法:
class UsersController
#You write this stuff:
def go
a = StackExchange.new(10)
a.dostuff
end
end
因此,对于一个非常简单的解决方案,您可以将StackExchange类粘贴到包含控制器类的文件的顶部,然后在控制器的任何操作/方法中,您可以调用StackExchange.new
但是如果其他一些控制器中的操作(=方法)也需要使用StackExchange c