~vesto/huginn_sqlite3_agent

891d293bf82ab9659b65a502895c71a017653052 — Steve Gattuso 3 years ago
initial commit
A  => .gitignore +3 -0
@@ 1,3 @@
spec/huginn
.env
Gemfile.lock

A  => Gemfile +5 -0
@@ 1,5 @@
source 'https://rubygems.org'

gemspec
gem 'huginn_agent', "~> 0.3.0"
gem 'sqlite3'

A  => LICENSE.txt +7 -0
@@ 1,7 @@
Copyright (c) 2021 Steve Gattuso

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

A  => README.md +21 -0
@@ 1,21 @@
# SQLite3 Agent
This gem provides an agent for [Huginn](https://github.com/huginn/huginn) which
provides an easy interface for writing to SQLite 3 databases.

For detailed instructions on its usage, please see the Markdown description
within the agent's source (which will also be displayed in your Huginn
dashboard).

## Installation
Like any other Huginn agent, you may install this agent by adding thw following
to your `.env` file:

```
ADDITIONAL_GEMS=huginn_sqlite3_agent
```

or, if you wish to stay on the bleeding edge:

```
ADDITIONAL_GEMS=huginn_sqlite3_agent(git:https://github.com/stevenleeg/huginn_sqlite3_agent.git)
```

A  => Rakefile +5 -0
@@ 1,5 @@
#!/usr/bin/env rake

require 'huginn_agent'

HuginnAgent.load_tasks(branch: 'master', remote: 'https://github.com/cantino/huginn.git')

A  => huginn_sqlite3_agent.gemspec +27 -0
@@ 1,27 @@
# coding: utf-8
lib = File.expand_path('../lib', __FILE__)
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)

Gem::Specification.new do |spec|
  spec.name          = "huginn_sqlite3_agent"
  spec.version       = '0.1'
  spec.authors       = ["Steve Gattuso"]
  spec.email         = ["steve@stevegattuso.me"]

  spec.summary       = %q{A huginn agent for writing data to SQLite3 databases}
  spec.description   = %q{A huginn agent for writing data to SQLite3 databases}

  spec.homepage      = "https://github.com/stevenleeg/huginn_sqlite3_agent"

  spec.license       = "MIT"

  spec.files         = Dir['LICENSE.txt', 'lib/**/*']
  spec.executables   = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
  spec.test_files    = Dir['spec/**/*.rb']
  spec.require_paths = ["lib"]

  spec.add_development_dependency "bundler", "~> 1.7"
  spec.add_development_dependency "rake", "~> 10.0"

  spec.add_runtime_dependency "huginn_agent", '~> 0.2'
end

A  => lib/huginn_sqlite3_agent.rb +3 -0
@@ 1,3 @@
require 'huginn_agent'

HuginnAgent.register 'huginn_sqlite3_agent/sqlite3_write_agent'

A  => lib/huginn_sqlite3_agent/sqlite3_agent.rb +68 -0
@@ 1,68 @@
require 'sqlite3'

module Agents
  class Sqlite3WriteAgent < Agent
    include FormConfigurable

    cannot_be_scheduled!

    description <<-MD
      This agent allows data to be written to a SQLite3 database. You can use
      this as a means to pass data into a format that is perhaps a more durable
      than Huginn's internal database + be a bit more queryable than the
      `events` table. Generally you'll want to use this in combination with a
      Javascript agent, which will allow you to intake arbitrary events and
      properly format them into the event schema expected by this agent.

      Note that the agent expects the sqlite3 database at `database_path` to
      already exist. You are responsible for setting up this db file and its
      tables yourself.

      ## Writing Data
      This agent accepts events in the following format:
      
          {
            "query": "INSERT INTO some_table (col1, col2) VALUES(?, ?)",
            "parameters": ["abc", 123]
          }

      Regardless of the result, the agent will _not_ transmit any outgoing
      events. It will, however, log whether or not the query was successful.
    MD

    form_configurable :database_path, type: :string
    def validate_options
      if options['database_path'].blank?
        errors.add(:database_path, 'cannot be blank')
      elsif !File.file?(options['database_path'])
        errors.add(:database_path, 'does not exist')
      end
    end

    def default_options
      {
        'database_path' => '/path/to/some/sqlite3.db',
      }
    end

    def working?
      if memory['last_success'].nil?
        true
      else
        memory['last_success']
      end
    end

    def receive(incoming_events)
      memory['last_success'] = false

      db = SQLite3::Database.new options['database_path']
      incoming_events.each do |event|
        db.execute event.payload['query'], event.payload['parameters']
        log("Executed query '#{event.payload['query']}' with params: #{event.payload['parameters']}")
      end

      memory['last_success'] = true
    end
  end
end