Class: Skr::Jobs::FreshBooks::Base

Inherits:
Lanes::Job
  • Object
show all
Defined in:
lib/skr/jobs/fresh_books/base.rb

Direct Known Subclasses

Import, Retrieve

Constant Summary collapse

STEPS =
%w{staff clients projects time_entries invoices}

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#fbObject (readonly)

Returns the value of attribute fb.



10
11
12
# File 'lib/skr/jobs/fresh_books/base.rb', line 10

def fb
  @fb
end

Instance Method Details

#fetch_each(reqtype) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/skr/jobs/fresh_books/base.rb', line 21

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

#process_each_type(account, token) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/skr/jobs/fresh_books/base.rb', line 37

def process_each_type(, token)
    @fb = ::FreshBooks::Client.new("#{}.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

#resp_data_for_req_type(type, resp) ⇒ Object



11
12
13
14
15
16
17
18
19
# File 'lib/skr/jobs/fresh_books/base.rb', line 11

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