Sha256: af2b39198ae052f3c8a9eb1b933c39cad6a64ef2a03cf3308d81ca402fcba409

Contents?: true

Size: 1.84 KB

Versions: 4

Compression:

Stored size: 1.84 KB

Contents

require 'ruby-freshbooks'

module Skr
    module Jobs
        module FreshBooks

            class Base < Lanes::Job
                STEPS = %w{staff clients projects time_entries invoices}

                attr_reader :fb
                def resp_data_for_req_type(type, resp)
                    if type == 'staff'
                        data = resp['staff_members']
                        data[type]=data.delete('member')
                        data
                    else
                        resp[type.pluralize]
                    end
                end

                def fetch_each(reqtype)
                    Enumerator.new { | enum |
                        page = last_page = 1
                        while page <= last_page
                            resp = fb.__send__(reqtype).list(per_page: 25, page: page)
                            data = resp_data_for_req_type(reqtype, resp)
                            last_page = data['pages'].to_i
                            page += 1
                            Array.wrap(data[reqtype]).each{|record|
                                enum.yield record
                            }
                        end
                    }
                end


                def process_each_type(account, token)
                    @fb = ::FreshBooks::Client.new("#{account}.freshbooks.com", token)
                    output = {}
                    @ignored_ids ||= {}
                    STEPS.each_with_index do | step, index |
                        type = step.singularize
                        output[step] = fetch_each( type ).map { | record |
                            send("process_#{type}", record)
                        }.reject(&:nil?)
                        yield output, index
                    end
                    output
                end

            end
        end
    end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
stockor-0.5.0 lib/skr/jobs/fresh_books/base.rb
stockor-0.4.0 lib/skr/jobs/fresh_books/base.rb
stockor-0.3.0 lib/skr/jobs/fresh_books/base.rb
stockor-0.2 lib/skr/jobs/fresh_books/base.rb