tender_import 0.0.6 → 0.0.7

Sign up to get free protection for your applications and to get access to all the features.
@@ -26,6 +26,12 @@
26
26
  # :body => 'You have terrible taste in tacos. Good day, sir.'
27
27
  # }]
28
28
  #
29
+ # # To add knowledge base you'll need sections
30
+ # section = archive.add_section :name => 'Basic Help'
31
+ #
32
+ # archive.add_kb section, :title => 'How to get help',
33
+ # :body => 'This is my awesome knowledge base'
34
+ #
29
35
  # # By default, files are written as you add them, so this will just
30
36
  # # assemble a gzipped tar archive from those files.
31
37
  # filename = archive.write_archive
@@ -55,6 +61,7 @@ class TenderImport::Archive
55
61
  @stats = {}
56
62
  @buffer = options.key?(:buffer) ? !!options[:buffer] : false
57
63
  @category_counter = Hash.new(0)
64
+ @section_counter = Hash.new(0)
58
65
  end
59
66
 
60
67
  # Returns the params on success, nil on failure
@@ -68,10 +75,20 @@ class TenderImport::Archive
68
75
  cat ? category_key(cat) : nil
69
76
  end
70
77
 
78
+ def add_section params
79
+ section = validate_and_store :section, params
80
+ section ? section_key(section) : nil
81
+ end
82
+
71
83
  def add_discussion category_key, params
72
84
  raise Error, "add_discussion: missing category key" if category_key.nil?
73
85
  validate_and_store :discussion, params, :key => category_key
74
86
  end
87
+
88
+ def add_kb section_key, params
89
+ raise Error, "add_kb: missing section key" if section_key.nil?
90
+ validate_and_store :kb, params, :key => section_key
91
+ end
75
92
 
76
93
  def category_key cat
77
94
  "category:#{category_id cat}".downcase
@@ -80,16 +97,33 @@ class TenderImport::Archive
80
97
  def category_id cat
81
98
  cat[:name].gsub(/\W+/,'_').downcase
82
99
  end
100
+
101
+ def section_key section
102
+ "section:#{section_id section}".downcase
103
+ end
104
+
105
+ def section_id section
106
+ section[:title].gsub(/\W+/,'_').downcase
107
+ end
83
108
 
84
109
  def categories
85
110
  @import[:category]
86
111
  end
112
+
113
+ def sections
114
+ @import[:section]
115
+ end
87
116
 
88
117
  def discussions category_key
89
118
  raise Error, "discussions: missing category key" if category_key.nil?
90
119
  @import[category_key] || []
91
120
  end
92
121
 
122
+ def kbs section_key
123
+ raise Error, "kbs: missing section key" if section_key.nil?
124
+ @import[section_key] || []
125
+ end
126
+
93
127
  def users
94
128
  @import[:user]
95
129
  end
@@ -97,6 +131,7 @@ class TenderImport::Archive
97
131
  def write_archive
98
132
  write_users if users
99
133
  write_categories_and_discussions if categories
134
+ write_sections_and_kbs if sections
100
135
  export_file = "export_#{site}.tgz"
101
136
  system "tar -zcf #{export_file} -C #{export_dir} ."
102
137
  system "rm -rf #{export_dir}"
@@ -123,6 +158,13 @@ class TenderImport::Archive
123
158
  file.puts Yajl::Encoder.encode(c)
124
159
  end
125
160
  end
161
+
162
+ def write_section s
163
+ mkdir_p export_dir('sections')
164
+ File.open(File.join(export_dir('sections'), "#{section_id(s)}.json"), "w") do |file|
165
+ file.puts Yajl::Encoder.encode(s)
166
+ end
167
+ end
126
168
 
127
169
  def write_categories_and_discussions
128
170
  categories.each do |c|
@@ -130,6 +172,13 @@ class TenderImport::Archive
130
172
  write_discussions c
131
173
  end
132
174
  end
175
+
176
+ def write_sections_and_kbs
177
+ sections.each do |s|
178
+ write_section s
179
+ write_kbs s
180
+ end
181
+ end
133
182
 
134
183
  def write_discussion category_id, discussion
135
184
  @category_counter[category_id] += 1
@@ -139,12 +188,27 @@ class TenderImport::Archive
139
188
  file.puts Yajl::Encoder.encode(discussion)
140
189
  end
141
190
  end
191
+
192
+ def write_kb section_id, kb
193
+ @section_counter[section_id] += 1
194
+ dir = File.join(export_dir('sections'), section_id)
195
+ mkdir_p dir
196
+ File.open(File.join(dir, "#{@section_counter[section_id]}.json"), "w") do |file|
197
+ file.puts Yajl::Encoder.encode(kb)
198
+ end
199
+ end
142
200
 
143
201
  def write_discussions category
144
202
  discussions(category_key(category)).each do |d|
145
203
  write_discussion category_id(category), d
146
204
  end
147
205
  end
206
+
207
+ def write_kbs section
208
+ kbs(section_key(section)).each do |k|
209
+ write_kb section_id(s), k
210
+ end
211
+ end
148
212
 
149
213
  protected
150
214
 
@@ -180,6 +244,10 @@ class TenderImport::Archive
180
244
  write_category params
181
245
  when :user
182
246
  write_user params
247
+ when :section
248
+ write_section params
249
+ when :kb
250
+ write_kb options[:key].split(':',2)[1], params
183
251
  end
184
252
  end
185
253
 
@@ -201,6 +269,9 @@ class TenderImport::Archive
201
269
  if type == :discussion && (params[:comments].nil? || params[:comments].any? {|c| c[:author_email].nil? || c[:author_email].empty?})
202
270
  problems << "Missing comments and authors in discussion data: #{params.inspect}."
203
271
  end
272
+ if type == :kb && (params[:title].nil? || params[:title].empty? || params[:body].nil? || params[:body].empty?)
273
+ problems << "Missing title or body in kb: #{params.inspect}"
274
+ end
204
275
  if problems.empty?
205
276
  true
206
277
  else
@@ -1,4 +1,4 @@
1
1
  module TenderImport
2
- VERSION = '0.0.6'
2
+ VERSION = '0.0.7'
3
3
  end
4
4
 
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tender_import
3
3
  version: !ruby/object:Gem::Version
4
- hash: 19
4
+ hash: 17
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 6
10
- version: 0.0.6
9
+ - 7
10
+ version: 0.0.7
11
11
  platform: ruby
12
12
  authors:
13
13
  - Zack Hobson
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2012-04-18 00:00:00 -07:00
18
+ date: 2012-07-30 00:00:00 -07:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -81,7 +81,7 @@ files:
81
81
  - lib/tender_import/zendesk_api_import.rb
82
82
  - lib/tender_import.rb
83
83
  - bin/zendesk2tender
84
- has_rdoc: true
84
+ has_rdoc: false
85
85
  homepage: https://help.tenderapp.com/kb/setup-installation/importing
86
86
  licenses: []
87
87