简体   繁体   English

Ruby on Rails。 使用 Google Client API 解析电子邮件

[英]Ruby on Rails. Using Google Client API to parse emails

I am new to Ruby and have a question about how to do a specific task on Rails.我是 Ruby 新手,有一个关于如何在 Rails 上执行特定任务的问题。

I have a list of inventory and each item has a specific Stock ID that is emailed to my personal Gmail account.我有一个库存清单,每件商品都有一个特定的库存 ID,通过电子邮件发送到我的个人 Gmail 帐户。 I want my web application to listen for emails from a specific email account.我希望我的 Web 应用程序侦听来自特定电子邮件帐户的电子邮件。 When my gmail receives an email from that specific account I want my application to parse it for a couple of fields and insert the stock ID into my database.当我的 gmail 收到来自该特定帐户的电子邮件时,我希望我的应用程序将其解析为几个字段并将股票 ID 插入到我的数据库中。

For example: Let's say my database has an item with style code: A5U31 and size:10.例如:假设我的数据库有一个样式代码为:A5U31 且尺寸为:10 的项目。 The email will say something like item with style code: A5U31 and size:10 has Stock ID:329193020.该电子邮件会显示类似样式代码的商品:A5U31 和尺寸:10 的商品 ID:329193020。 I want my Rails application to search the database for an entry with that specific style code and size, and when it finds an entry to simply insert the stock ID into the row.我希望我的 Rails 应用程序在数据库中搜索具有特定样式代码和大小的条目,并在找到条目时将股票 ID 简单地插入到行中。

I am trying to using the Google-API-Client gem to this, but I am struggling, because I am still a beginner.我正在尝试为此使用 Google-API-Client gem,但我很挣扎,因为我仍然是初学者。 So far I have done this quick-start guide to authenticate my gmail account with my rails app.到目前为止,我已经完成了这个快速入门指南,以使用我的 rails 应用程序验证我的 gmail 帐户。

https://developers.google.com/gmail/api/quickstart/ruby?authuser=2 https://developers.google.com/gmail/api/quickstart/ruby?authuser=2

If someone could help me figure out how to write this sort of code and where to put it in my app(models, controllers, views) that would be very appreciated.如果有人能帮我弄清楚如何编写这种代码以及将它放在我的应用程序(模型、控制器、视图)中的哪个位置,我将不胜感激。 Thanks!谢谢!

I know it's been several months since you posted this, so you probably already have it worked out, but in case you still need a solution (or if someone else wants to do something similar), I'll share my thoughts:我知道你发布这个已经几个月了,所以你可能已经解决了,但如果你仍然需要一个解决方案(或者如果其他人想做类似的事情),我会分享我的想法:

At a high level, it sounds like your plan is在高层次上,听起来你的计划是

  1. Identify when a new email has come in (either by polling or by using a push notification).确定收到新电子邮件的时间(通过轮询或使用推送通知)。
  2. Grab the new email's content.获取新电子邮件的内容。
  3. Parse the email's content in order to extract relevant data.解析电子邮件的内容以提取相关数据。
  4. Use the data to query and update a database.使用数据查询和更新数据库。

Based on the documentation for the Gmail API , it does look like you should be able to set up push notifications , so you won't have to poll the endpoint to get the information you need.根据Gmail API文档,您确实应该能够设置推送通知,因此您无需轮询端点即可获取所需信息。

However, my big takeaway from this list is that none of the items on it really require Rails, since you're not exposing an external web API for requests.然而,我从这个列表中得到的最大收获是,其中没有一项真正需要 Rails,因为您没有为请求公开外部 Web API。 I suppose that you could leverage ActiveRecord to create an item model and use that to manage the database;我想您可以利用 ActiveRecord 创建一个项目模型并使用它来管理数据库; however, since it seems like you'd only need to make some basic SQL queries (and the same ones each time), I'm not sure that bringing in ActiveRecord adds much value.但是,由于您似乎只需要进行一些基本的 SQL 查询(并且每次都进行相同的查询),我不确定引入 ActiveRecord 会增加多少价值。

If I were trying to solve this problem myself, I would probably create a simple Ruby program that (a) uses the gem you mentioned to handle push notifications from the Gmail API, and (b) uses another gem to connect to whatever kind of database you're using (eg pg for Postgres) and make the necessary queries.如果我试图自己解决这个问题,我可能会创建一个简单的 Ruby 程序,它 (a) 使用您提到的 gem 来处理来自 Gmail API 的推送通知,并且 (b) 使用另一个 gem 连接到任何类型的数据库您正在使用(例如 Postgres 的pg )并进行必要的查询。

(All of this assumes, of course, that you aren't specifically using Rails for some other reason, eg adding this feature to an existing Rails application). (当然,所有这些都假设您不是出于其他原因专门使用 Rails,例如将此功能添加到现有 Rails 应用程序中)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM