Sha256: e4c2d87ddc5342c14853caed0ebf4431a0ca0689d079095c065f01ca9c6f549f

Contents?: true

Size: 1.68 KB

Versions: 4

Compression:

Stored size: 1.68 KB

Contents

module Idonethis::UseCases
  module List
    class << self
      def apply(credential, args={})
        log = args[:log] || fail("You need to supply :log adapter")
        view = args[:view] || fail("You need to supply :view adapter")

        list = get(credential, log, args).tap{|result| log.call(result)}
        
        view.call list
      end

      private
      
      def get(credential, log, args)
        result = []
        page = 1
        
        loop do
          reply = get_page(page, credential, log, args)

          result += reply["results"]

          page += 1

          break if reply["next"].nil?
        end
        
        result
      end

      def get_page(page, credential, log, args) # https://idonethis.com/api/v0.1/dones/
        params = {"order_by" => "-done_date", "page_size" => 100, "page" => page}.
          merge(done_date_from(args)).
          merge({team: (args[:team] || credential[:team])})

        url = Idonethis::Index.dones(params)

        log.call "url: #{url}"
        
        internet = args[:internet] || fail("You need to supply :internet adapter")
         
        parse internet.get(url, { "Authorization" => "Token #{credential[:token]}", accept: "application/json"})
      end

      def done_date_from(args)
        opts = args[:opts] || []
      
        if opts.include?("week")
          return { done_date_after: (Time.now - (7*24*60*60)).strftime("%F") }
        end
        
        intersection = opts & %W{ today yesterday }

        return intersection.empty? ? { done_date: 'today' } : { done_date: intersection.first }
      end

      def parse(reply)
        require 'json'
        
        JSON.parse(reply.body)
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
idonethis-cli-0.20.0 lib/idonethis/use_cases/list.rb
idonethis-cli-0.19.1 lib/idonethis/use_cases/list.rb
idonethis-cli-0.19.0 lib/idonethis/use_cases/list.rb
idonethis-cli-0.15.0 lib/idonethis/use_cases/list.rb