Sha256: e5382d05987f8432568ade17dd2d30925329b2e6ede93fa152440f8e2ae7b7eb

Contents?: true

Size: 1.34 KB

Versions: 1

Compression:

Stored size: 1.34 KB

Contents

require 'chronicle/etl'
require 'faraday'

module Chronicle
  module Pinboard 
    class PinboardExtractor < Chronicle::ETL::Extractor
      register_connector do |r|
        r.provider = 'pinboard'
        r.description = 'bookmarks'
      end

      setting :access_token, required: true

      def prepare
        raise(Chronicle::ETL::ExtractionError, "Access token is missing") if @config.access_token.empty?

        @bookmarks = load_bookmarks
        @username = @config.access_token.split(":").first
      end

      def extract
        @bookmarks.each do |bookmark|
          yield Chronicle::ETL::Extraction.new(data: bookmark, meta: { username: @username })
        end
      end

      def results_count
        @bookmarks.count
      end

      private

      def load_bookmarks
        params = {
          auth_token: @config.access_token,
          format: 'json',
          meta: true,
        }
        params[:fromdt] = @config.since.utc.iso8601 if @config.since

        conn = Faraday.new(
          url: 'https://api.pinboard.in/',
          params: params
        )

        response = conn.get('/v1/posts/all')
        bookmarks = JSON.parse(response.body, { symbolize_names: true })
        bookmarks = bookmarks.keep_if { |bookmark| Time.parse(bookmark[:time]) < @config.until } if @config.until
        bookmarks
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
chronicle-pinboard-0.1.1 lib/chronicle/pinboard/pinboard_extractor.rb