Ruby on Rails - 如何组合来自2个独立API的输出数据并将它们添加到数据库中

时间:2022-02-08 14:29:22

In my application, I need to gather some data from 2 separate (3rd party) APIs and add them to my database for a record for better analyzing.

在我的应用程序中,我需要从2个独立的(第三方)API中收集一些数据,并将它们添加到我的数据库中以获得更好的分析记录。

Both APIs have the same amount of raws (For instance, when I request images for Album X, it will show me data for ALL images in that album)

两个API都具有相同数量的原始数据(例如,当我请求相册X的图像时,它将显示该相册中所有图像的数据)

So both APIs will have the same amount of images BUT different fields and field names.

因此,两个API将具有相同数量的图像但不同的字段和字段名称。

In my application, I'm only interested in getting data for 5 fields from both APIs. I get field_1, field_3 & field_5 from the first API while I get field_2 & field_4 from the second API.

在我的应用程序中,我只对从两个API获取5个字段的数据感兴趣。我从第一个API获取field_1,field_3和field_5,而我从第二个API获取field_2和field_4。

This is how I do it now to only get fields-value from the first API (I'm still stuck for how to put both APIs together).

这就是我现在如何只从第一个API获取字段值(我仍然坚持如何将两个API放在一起)。

## My Image Model
class Image < ActiveRecord::Base

  private

    def self.add_image_data_to_database(object, field_1, field_2, field_3, field_4, field_5)
      object.create(
          field_1: field_1,
          field_2: field_2,
          field_3: field_3,
          field_4: field_4,
          field_5: field_5,
      )
    end
end

## My Images Controller
class ImagesController < ApplicationController

  first_site_api = "api.website-1.com"
  first_api_results = HTTParty.get(first_site_api)
  first_api_json = JSON.parse(first_api_results)


  second_site_api = "api.website-2.com"
  second_api_results = HTTParty.get(second_site_api)
  second_api_json = JSON.parse(second_api_results)

  first_api_json.each do |data|
    Image.add_image_data_to_database(
        data['field_1'], 
        data['field_3'],
        data['field_5']
        )
  end

end

Basically what I need and wonder how to do, is to get some fields-value from the first API and some from the second API and put them together so I can add them all for 1 image.

基本上我需要并想知道如何做,是从第一个API获取一些字段值,从第二个API获取一些字段值并将它们放在一起,这样我就可以为1个图像添加它们。

1 个解决方案

#1


0  

You can get items out of a hash with .keep_if so you might try:

您可以使用.keep_if从哈希中获取项目,以便您可以尝试:

# My Image Model
class Image < ActiveRecord::Base

  private

    def self.add_image_data_to_database(object, hash)
      object.create(hash)
    end
end

## My Images Controller
class ImagesController < ApplicationController

  first_site_api = "api.website-1.com"
  first_api_results = HTTParty.get(first_site_api)

  hash = JSON.parse(first_api_results)
           .keep_if{|k, _v| [ 'field_1', 'field_3', 'field_5'].include?(k)}


  second_site_api = "api.website-2.com"
  second_api_results = HTTParty.get(second_site_api)

  hash.merge( JSON.parse(second_api_results)
    .keep_if{|k, _v| ['field_2', 'field_4'].include?(k) }
  )

  Image.add_image_data_to_database(hash)
end

#1


0  

You can get items out of a hash with .keep_if so you might try:

您可以使用.keep_if从哈希中获取项目,以便您可以尝试:

# My Image Model
class Image < ActiveRecord::Base

  private

    def self.add_image_data_to_database(object, hash)
      object.create(hash)
    end
end

## My Images Controller
class ImagesController < ApplicationController

  first_site_api = "api.website-1.com"
  first_api_results = HTTParty.get(first_site_api)

  hash = JSON.parse(first_api_results)
           .keep_if{|k, _v| [ 'field_1', 'field_3', 'field_5'].include?(k)}


  second_site_api = "api.website-2.com"
  second_api_results = HTTParty.get(second_site_api)

  hash.merge( JSON.parse(second_api_results)
    .keep_if{|k, _v| ['field_2', 'field_4'].include?(k) }
  )

  Image.add_image_data_to_database(hash)
end