Ruby on rails Rails使用API
我是API的初学者。我正试着和你一起工作。我不想用它,因为首先我喜欢学习Ruby on rails Rails使用API,ruby-on-rails,ruby,httparty,Ruby On Rails,Ruby,Httparty,我是API的初学者。我正试着和你一起工作。我不想用它,因为首先我喜欢学习 class Forecast include HTTParty base_uri "api.forecast.io/forecast/#{@api_key}/#{@latitude},#{@longitude}" def initialize(api_key,latitude,longitude) self.api_key = api_key self.latitude = latitu
class Forecast
include HTTParty
base_uri "api.forecast.io/forecast/#{@api_key}/#{@latitude},#{@longitude}"
def initialize(api_key,latitude,longitude)
self.api_key = api_key
self.latitude = latitude
self.longitude = longitude
end
end
现在,初始化后的下一步应该是什么。我试图理解使用httparty gem的例子,但不知道到底要做什么
你能帮我用API修复it和point相关资源吗?在使用API时,我不使用HttpParty gem,而是使用它来进行并行http请求,从而启用并发性,但我相信如果使用HttpParty,下面的示例也会起作用。 我将使用一个简单的示例来演示如何使用API。假设您正在尝试与JSON api服务通信以获取产品列表 服务端点的url为http://path/to/products.json 在您的应用程序中,可以有一个products_controller.rb,其索引操作如下所示:
class ProductsController < ApplicationController
def index
# make a http request to the api to fetch the products in json format
hydra = Typhoeus::Hydra.hydra
get_products = Typhoeus::Request.new('http://path/to/products.json')
get_products.on_complete do |response|
products = MultipleProducts.from_json(response.body)
@products = products.products
end
hydra.queue get_products
hydra.run
end
end
class MultipleProducts
attr_reader :products
def initialize(attributes)
@products = attributes[:products]
end
def self.from_json(json_string)
parsed = JSON.parse(json_string)
products = parsed['products'].map do |product|
Product.new(product)
end
new(products: products)
end
end
class Product
include ActiveModel::Serializers::JSON
include ActiveModel::Validations
ATTRIBUTES = [:id, :name, :description, :price]
attr_accessor *ATTRIBUTES
validates_presence_of :id, :name, :price
def initialize(attributes = {})
self.attributes = attributes
end
def attributes
ATTRIBUTES.inject(ActiveSupport::HashWithIndifferentAccess.new) do |result, key|
result[key] = read_attribute_for_validation(key)
result
end
end
def attributes= (attrs)
attrs.each_pair {|k, v| send("#{k}=", v)}
end
def read_attribute_for_validation(key)
send(key)
end
end
这个json可以封装在一个名为multiple_products.rb的类中,该类的名称如下:
class ProductsController < ApplicationController
def index
# make a http request to the api to fetch the products in json format
hydra = Typhoeus::Hydra.hydra
get_products = Typhoeus::Request.new('http://path/to/products.json')
get_products.on_complete do |response|
products = MultipleProducts.from_json(response.body)
@products = products.products
end
hydra.queue get_products
hydra.run
end
end
class MultipleProducts
attr_reader :products
def initialize(attributes)
@products = attributes[:products]
end
def self.from_json(json_string)
parsed = JSON.parse(json_string)
products = parsed['products'].map do |product|
Product.new(product)
end
new(products: products)
end
end
class Product
include ActiveModel::Serializers::JSON
include ActiveModel::Validations
ATTRIBUTES = [:id, :name, :description, :price]
attr_accessor *ATTRIBUTES
validates_presence_of :id, :name, :price
def initialize(attributes = {})
self.attributes = attributes
end
def attributes
ATTRIBUTES.inject(ActiveSupport::HashWithIndifferentAccess.new) do |result, key|
result[key] = read_attribute_for_validation(key)
result
end
end
def attributes= (attrs)
attrs.each_pair {|k, v| send("#{k}=", v)}
end
def read_attribute_for_validation(key)
send(key)
end
end
然后,您可以使用创建如下产品模型:
class ProductsController < ApplicationController
def index
# make a http request to the api to fetch the products in json format
hydra = Typhoeus::Hydra.hydra
get_products = Typhoeus::Request.new('http://path/to/products.json')
get_products.on_complete do |response|
products = MultipleProducts.from_json(response.body)
@products = products.products
end
hydra.queue get_products
hydra.run
end
end
class MultipleProducts
attr_reader :products
def initialize(attributes)
@products = attributes[:products]
end
def self.from_json(json_string)
parsed = JSON.parse(json_string)
products = parsed['products'].map do |product|
Product.new(product)
end
new(products: products)
end
end
class Product
include ActiveModel::Serializers::JSON
include ActiveModel::Validations
ATTRIBUTES = [:id, :name, :description, :price]
attr_accessor *ATTRIBUTES
validates_presence_of :id, :name, :price
def initialize(attributes = {})
self.attributes = attributes
end
def attributes
ATTRIBUTES.inject(ActiveSupport::HashWithIndifferentAccess.new) do |result, key|
result[key] = read_attribute_for_validation(key)
result
end
end
def attributes= (attrs)
attrs.each_pair {|k, v| send("#{k}=", v)}
end
def read_attribute_for_validation(key)
send(key)
end
end
在app/views/products/index.html中,您可以拥有:
<h1>Products Listing</h1>
<ul>
<% @products.each do |product| %>
<li>Name: <%= product.name %> Price: <%= product.price %> </li>
<% end %>
</ul>
这将列出从api获取的所有产品。
这只是一个简单的示例,在使用API时涉及的内容要多得多。我建议您阅读更多详细信息。在使用API时,我不使用httparty gem,而是使用它,它允许进行并行http请求并因此启用并发性,但我相信如果您使用httparty,下面的示例也会起作用。 我将使用一个简单的示例来演示如何使用API。假设您正在尝试与JSON api服务通信以获取产品列表 服务端点的url为http://path/to/products.json 在您的应用程序中,可以有一个products_controller.rb,其索引操作如下所示:
class ProductsController < ApplicationController
def index
# make a http request to the api to fetch the products in json format
hydra = Typhoeus::Hydra.hydra
get_products = Typhoeus::Request.new('http://path/to/products.json')
get_products.on_complete do |response|
products = MultipleProducts.from_json(response.body)
@products = products.products
end
hydra.queue get_products
hydra.run
end
end
class MultipleProducts
attr_reader :products
def initialize(attributes)
@products = attributes[:products]
end
def self.from_json(json_string)
parsed = JSON.parse(json_string)
products = parsed['products'].map do |product|
Product.new(product)
end
new(products: products)
end
end
class Product
include ActiveModel::Serializers::JSON
include ActiveModel::Validations
ATTRIBUTES = [:id, :name, :description, :price]
attr_accessor *ATTRIBUTES
validates_presence_of :id, :name, :price
def initialize(attributes = {})
self.attributes = attributes
end
def attributes
ATTRIBUTES.inject(ActiveSupport::HashWithIndifferentAccess.new) do |result, key|
result[key] = read_attribute_for_validation(key)
result
end
end
def attributes= (attrs)
attrs.each_pair {|k, v| send("#{k}=", v)}
end
def read_attribute_for_validation(key)
send(key)
end
end
这个json可以封装在一个名为multiple_products.rb的类中,该类的名称如下:
class ProductsController < ApplicationController
def index
# make a http request to the api to fetch the products in json format
hydra = Typhoeus::Hydra.hydra
get_products = Typhoeus::Request.new('http://path/to/products.json')
get_products.on_complete do |response|
products = MultipleProducts.from_json(response.body)
@products = products.products
end
hydra.queue get_products
hydra.run
end
end
class MultipleProducts
attr_reader :products
def initialize(attributes)
@products = attributes[:products]
end
def self.from_json(json_string)
parsed = JSON.parse(json_string)
products = parsed['products'].map do |product|
Product.new(product)
end
new(products: products)
end
end
class Product
include ActiveModel::Serializers::JSON
include ActiveModel::Validations
ATTRIBUTES = [:id, :name, :description, :price]
attr_accessor *ATTRIBUTES
validates_presence_of :id, :name, :price
def initialize(attributes = {})
self.attributes = attributes
end
def attributes
ATTRIBUTES.inject(ActiveSupport::HashWithIndifferentAccess.new) do |result, key|
result[key] = read_attribute_for_validation(key)
result
end
end
def attributes= (attrs)
attrs.each_pair {|k, v| send("#{k}=", v)}
end
def read_attribute_for_validation(key)
send(key)
end
end
然后,您可以使用创建如下产品模型:
class ProductsController < ApplicationController
def index
# make a http request to the api to fetch the products in json format
hydra = Typhoeus::Hydra.hydra
get_products = Typhoeus::Request.new('http://path/to/products.json')
get_products.on_complete do |response|
products = MultipleProducts.from_json(response.body)
@products = products.products
end
hydra.queue get_products
hydra.run
end
end
class MultipleProducts
attr_reader :products
def initialize(attributes)
@products = attributes[:products]
end
def self.from_json(json_string)
parsed = JSON.parse(json_string)
products = parsed['products'].map do |product|
Product.new(product)
end
new(products: products)
end
end
class Product
include ActiveModel::Serializers::JSON
include ActiveModel::Validations
ATTRIBUTES = [:id, :name, :description, :price]
attr_accessor *ATTRIBUTES
validates_presence_of :id, :name, :price
def initialize(attributes = {})
self.attributes = attributes
end
def attributes
ATTRIBUTES.inject(ActiveSupport::HashWithIndifferentAccess.new) do |result, key|
result[key] = read_attribute_for_validation(key)
result
end
end
def attributes= (attrs)
attrs.each_pair {|k, v| send("#{k}=", v)}
end
def read_attribute_for_validation(key)
send(key)
end
end
在app/views/products/index.html中,您可以拥有:
<h1>Products Listing</h1>
<ul>
<% @products.each do |product| %>
<li>Name: <%= product.name %> Price: <%= product.price %> </li>
<% end %>
</ul>
这将列出从api获取的所有产品。
这只是一个简单的示例,在使用API时涉及的内容要多得多。我建议您阅读更多详细信息。WhoOa。。!这很复杂。我想我需要学习很多东西来开发API。需要找到一种简单的方法来理解所有这些库。但是参考这本书太多了。哇。。!这很复杂。我想我需要学习很多东西来开发API。需要找到一种简单的方法来理解所有这些库。但是参考这本书有很多好处。