ruby - Rails working with API -



ruby - Rails working with API -

i beginner in working api. i'm trying work forecast api. don't want utilize official wrapper, because first study , learn.

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

now should next step after initialization. i've tried understand using httparty gem examples, cant figure out do.

could help me prepare & point related resources apis?

i not utilize httparty gem when working apis instead utilize typhoeus gem allow making parallel http requests , hence enable concurrency believe illustration below work if utilize httparty. going utilize simple illustration show how work apis. let's trying communicate json api service fetch list of products.

the url of service endpoint http://path/to/products.json

in application, can have products_controller.rb index action looks this:

class productscontroller < applicationcontroller def index # create http request api fetch products in json format hydra = typhoeus::hydra.hydra get_products = typhoeus::request.new('http://path/to/products.json') get_products.on_complete |response| products = multipleproducts.from_json(response.body) @products = products.products end hydra.queue get_products hydra.run end end

let's http request http://path/to/products.json returns next json

{"products" [{"id": 1, "name": "first product", "description": "description", "price": "25.99"}, {"id": 2, "name": "second product", "description": "description", "price": "5.99"}]

this json can wrapped in class name like, multiple_products.rb looks this:

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 |product| product.new(product) end new(products: products) end end

you can utilize activemodel create product model this:

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) |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

in app/views/products/index.html, can have:

<h1>products listing</h1> <ul> <% @products.each |product| %> <li>name: <%= product.name %> price: <%= product.price %> </li> <% end %> </ul>

this list products fetched api. simple illustration , there much more involved when working apis. recommend read service-oriented design ruby , rails more details.

ruby-on-rails ruby httparty

Comments