Skip to content

Instantly share code, notes, and snippets.

@danielepolencic
Created April 14, 2018 19:43
Show Gist options
  • Save danielepolencic/1ee0463ca2b21ccbe69f2f763c448fad to your computer and use it in GitHub Desktop.
Save danielepolencic/1ee0463ca2b21ccbe69f2f763c448fad to your computer and use it in GitHub Desktop.
Mercury rewrite in Ruby for Huginn

Mercury Huginn

The following code sort of work app/models/agents/mercury_agent.rb:

module Agents
  class MercuryAgent < Agent
    include WebRequestConcern

    can_dry_run!
    no_bulk_receive!
    default_schedule "never"

    description do
      <<-MD
        A Mercury Agent receives events from other agents and extracts the contents from their URLs.
      MD
    end

    event_description <<-MD
      Events look like this:
        {
          "mercuryTitle": "...",
          "mercuryContent": "..."
        }

      Original event contents will be merged.
    MD

    def default_options
      {
        'url' => "http://www.example.com",
        'api_key' => "123",
        'expected_receive_period_in_days' => '1',
      }
    end

    def working?
      last_receive_at && last_receive_at > interpolated['expected_receive_period_in_days'].to_i.days.ago && !recent_error_logs?
    end

    def method
      (interpolated['method'].presence || 'post').to_s.downcase
    end

    def validate_options
      p options.inspect
      unless options['url'].present? && options['expected_receive_period_in_days'].present?
        errors.add(:base, "url and expected_receive_period_in_days are required fields")
      end

      # if options.has?('api_key')
      #   errors.add(:base, "api_key is a required field")
      # end

      validate_web_request_options!
    end

    def receive(incoming_events)
      incoming_events.each do |event|
        interpolate_with(event) do
          handle event.payload, event
        end
      end
    end

    def check
      handle interpolated['payload'].presence || {}
    end

    private

    def handle(data, event = Event.new)
      p "DO:"
      p interpolated['payload']
      p data.inspect
      response = faraday.run_request(:get, "https://mercury.postlight.com/parser?url=#{data[:url]}", {}, {'x-api-key': data[:api_key]})

      p response.status
      p response.status == 200
      if response.status == 200
        json = JSON.parse(response.body)
        create_event payload: event.payload.dup.merge(
          mercuryTitle: json['title'],
          mercuryContent: json['content'],
        )
      else
        create_event payload: event.payload.dup
      end
    end
  end
end

Only if a payload is provided to the dry run. The code doesn't not work with events. Payload:

{
  "url": "http://www.repubblica.it",
  "api_key": "123"
}

Issues with rails

  • Remeber to bind the right address and port - i.e. bundle exec rails s -p 4000 -b 0.0.0.0
  • Got a lot of errors with encoding. The one for bundler can be fixed by adding some extra lines to the Gemfile:
Encoding.default_external = Encoding::UTF_8
Encoding.default_internal = Encoding::UTF_8
  • Remember to create a db with:
apt-get update
apt-get install mysql-server
service mysql start
bundle exec rake db:create
bundle exec rake db:migrate
  • There's no default use. There's no need for an invite (if the latter is wrong, try-huginn)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment