Class: TenderImport::ZendeskApiImport::Exporter

Inherits:
Object
  • Object
show all
Includes:
FileUtils, Log
Defined in:
lib/tender_import/zendesk_api_import.rb

Overview

}}}

Class Attribute Summary collapse

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Log

#debug, #log

Constructor Details

#initialize(client) ⇒ Exporter

Returns a new instance of Exporter.



162
163
164
165
166
167
168
169
170
171
172
# File 'lib/tender_import/zendesk_api_import.rb', line 162

def initialize client
  @client = client
  @author_email = {}
  @logger = client.logger
  @archive = TenderImport::Archive.new(client.subdomain)
  unless html2text_bin
    if `which html2text.py`.empty?
      raise Error, 'missing prerequisite: html2text.py is not in your PATH'
    end
  end
end

Class Attribute Details

.html2text_binObject

Returns the value of attribute html2text_bin.



152
153
154
# File 'lib/tender_import/zendesk_api_import.rb', line 152

def html2text_bin
  @html2text_bin
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



158
159
160
# File 'lib/tender_import/zendesk_api_import.rb', line 158

def client
  @client
end

#loggerObject (readonly)

Returns the value of attribute logger.



158
159
160
# File 'lib/tender_import/zendesk_api_import.rb', line 158

def logger
  @logger
end

Instance Method Details

#create_archiveObject

}}}



269
270
271
272
273
# File 'lib/tender_import/zendesk_api_import.rb', line 269

def create_archive # {{{
  export_file = @archive.write_archive
  log "created #{export_file}"
  return export_file
end

#export_categoriesObject

}}}



200
201
202
203
204
205
206
207
208
209
# File 'lib/tender_import/zendesk_api_import.rb', line 200

def export_categories # {{{
  log 'exporting categories'
  client.forums.each do |forum|
    log "exporting category #{forum['name']}"
    category = @archive.add_category \
        :name => forum['name'],
        :summary => forum['description']
    export_discussions(forum['id'], category)
  end
end

#export_discussions(forum_id, category) ⇒ Object

}}}



243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
# File 'lib/tender_import/zendesk_api_import.rb', line 243

def export_discussions forum_id, category # {{{
  client.entries(forum_id).each do |entry|
    comments = client.posts(entry['id']).map do |post|
      dump_body post, post['body']
      {
        :body => load_body(entry),
        :author_email => author_email(post['user_id']),
        :created_at => post['created_at'],
        :updated_at => post['updated_at'],
      }
    end
    dump_body entry, entry['body']
    log "exporting discussion #{entry['title']}"
    @archive.add_discussion category,
      :title    => entry['title'],
      :author_email => author_email(entry['submitter_id']),
      :comments => [{
        :body => load_body(entry),
        :author_email => author_email(entry['submitter_id']),
        :created_at => entry['created_at'],
        :updated_at => entry['updated_at'],
      }] + comments
    rm "tmp/#{entry['id']}_body.html"
  end
end

#export_ticketsObject

}}}



211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
# File 'lib/tender_import/zendesk_api_import.rb', line 211

def export_tickets # {{{
  log "exporting open tickets"
  tickets = client.open_tickets
  if tickets.size > 0
    # create category for tickets
    log "creating ticket category"
    category = @archive.add_category \
      :name => 'Tickets',
      :summary => 'Imported from ZenDesk.'
    # export tickets into new category
    tickets.each do |ticket|
      comments = ticket['comments'].map do |post|
        {
          :body => post['value'],
          :author_email => author_email(post['author_id']),
          :created_at => post['created_at'],
          :updated_at => post['updated_at'],
        }
      end
      log "exporting ticket #{ticket['nice_id']}"
      @archive.add_discussion category, 
        :title        => ticket['subject'],
        :state        => ticket['is_locked'] ? 'resolved' : 'open',
        :private      => !ticket['is_public'],
        :author_email => author_email(ticket['submitter_id']),
        :created_at   => ticket['created_at'],
        :updated_at   => ticket['updated_at'],
        :comments     => comments
    end
  end
end

#export_usersObject

{{{



186
187
188
189
190
191
192
193
194
195
196
197
198
# File 'lib/tender_import/zendesk_api_import.rb', line 186

def export_users # {{{
  log 'exporting users'
  client.users.each do |user|
    @author_email[user['id'].to_s] = user['email']
    log "exporting user #{user['email']}"
    @archive.add_user \
      :name => user['name'],
      :email => user['email'],
      :created_at => user['created_at'],
      :updated_at => user['updated_at'],
      :state => (user['roles'].to_i == 0 ? 'user' : 'support')
  end
end

#html2text_binObject



154
155
156
# File 'lib/tender_import/zendesk_api_import.rb', line 154

def html2text_bin
  self.class.html2text_bin
end

#reportObject



182
183
184
# File 'lib/tender_import/zendesk_api_import.rb', line 182

def report
  @archive.report
end

#statsObject



178
179
180
# File 'lib/tender_import/zendesk_api_import.rb', line 178

def stats
  @archive.stats
end

#to_sObject



174
175
176
# File 'lib/tender_import/zendesk_api_import.rb', line 174

def to_s
  "#{self.class.name} (#{client.subdomain})"
end