CMS for a 3rd Party API

I’m building a new app that consumes, simplifies, and delivers an API from a 3rd party API. My rails models are:

  • Store (has many Products)
  • Products (belongs to Store)

So for example, I have an store which has products. The products just consist of a Product ID. That ID is used in the 3rd party API to look up information (like a product name, image, price, etc). I can then deliver that information to with a much simpler interface to someone else and use it inside the CMS.

Currently, I have a ProductLookup controller which is handling all the API requests and returns either json or html /product_lookups/:id(.:format) I’m struggling to figure out how to load the data I get back from the API in another view. I.E. /product_lookups/abc is fine, but how do I get a product to show up on in /store/1?

Is this even the right way to even go about this?

What up @edwardloveall!

I’m not sure if I’m understanding this completely so if forgive me if this is not the answer you’re looking for. It seems that in your product_lookups controller you should be creating some sort of ‘ExternalProduct’ that is built from the fetched api information. Something that would provide a clean interface to your application. This same product could be used to build the JSON in your product_lookups controller but also be used to present that information in your store view.

Maybe something like this(please forgive my completely made up code that is 100% a general idea):

class ExternalProduct
  attr_reader :name, :price, :url
  def initialize(product_id)
    @product_id = product_id
    lookup_data_from_external_api
  end

  private
  def lookup_data_from_external_api
     returned_data = magical_methods_actually_grabbing_data
     @name = returned_data[:name]
     @price = returned_data[:price]
     @url = returned_data[:url]
  end
end

You may also want to split up the loading of the data from the external API depending on how complicated things are, just trying to get a place for you to get started.

Let me know if I ran down the right path or not!

Thanks Draper. Let me make sure I understand what this would allow me to do because it looks like it might work.

If this is implemented, I could use something like:

def show
  @store = Store.find(params[:id])
  @extrenal_products = @store.products.map { |product|
    ExternalProduct.new(product[:product_id])
  }
end

and then have access to all the data I need in @extrenal_products in the view? The association makes it tricky I think. I sort of have to have two representations of the product. One belongs to a store and one is from the 3rd party API.

I think you understood perfectly! That is a great way to start!

You could also come back and make a method on the product that would return the external product object by calling ExternalProduct.new(self.id).

Ah, so like:

class ProductsController < ApplicationController
  def product_data
    ExternalProduct.new(self.id)
  end
end

Or would it be on the model?

Actually, it seems like it’s the Model:

class Product < ActiveRecord::Base
  ...

  def product_details
    ExternalProduct.new(self.product_id)
  end
end

Yes, exactly the second one. Did that help out?

It did! Thank you. Everything works as it should now.