Sha256: 59c7ed6dea4c31731b9365e22b7602adfdafa13b9a7874b2042c9993037e1441

Contents?: true

Size: 1.29 KB

Versions: 9

Compression:

Stored size: 1.29 KB

Contents

require 'json'

module Pickpocket
  module Articles
    class API
      ACTION_DELETE = 'delete'
      STATE_UNREAD  = 'unread'

      attr_reader :token_handler

      def initialize
        @token_handler = Authentication::TokenHandler.new
      end

      def retrieve
        uri      = URI(Pickpocket.config.pocket_retrieve_url)
        response = Net::HTTP.post_form(uri, {
            consumer_key: Pickpocket.config.consumer_key,
            access_token: access_token,
            state:        STATE_UNREAD
        })

        # TODO: error handling (unauthorized, etc)

        JSON.parse(response.body)
      end

      def delete(article_ids = [])
        return if article_ids.empty?

        uri         = URI(Pickpocket.config.pocket_send_url)
        json_action = article_ids.each_with_object([]) do |article_id, array|
          array << { action: ACTION_DELETE, item_id: article_id }
        end

        response = Net::HTTP.post_form(uri, {
            consumer_key: Pickpocket.config.consumer_key,
            access_token: access_token,
            actions:      JSON.dump(json_action)
        })

        # TODO: error handling (unauthorized, etc)

        JSON.parse(response.body)
      end

      private

      def access_token
        token_handler.read_auth
      end
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
pick-pocket-0.2.4 lib/pickpocket/articles/api.rb
pick-pocket-0.2.3 lib/pickpocket/articles/api.rb
pick-pocket-0.2.2 lib/pickpocket/articles/api.rb
pick-pocket-0.2.1 lib/pickpocket/articles/api.rb
pick-pocket-0.2.0 lib/pickpocket/articles/api.rb
pick-pocket-0.1.2 lib/pickpocket/articles/api.rb
pick-pocket-0.1.1 lib/pickpocket/articles/api.rb
pick-pocket-0.1.0 lib/pickpocket/articles/api.rb
pick-pocket-0.0.3 lib/pickpocket/articles/api.rb