Class: TenderImport::Archive
- Inherits:
-
Object
- Object
- TenderImport::Archive
- Includes:
- FileUtils
- Defined in:
- lib/tender_import/archive.rb
Defined Under Namespace
Classes: Error
Instance Attribute Summary collapse
-
#buffer ⇒ Object
readonly
Returns the value of attribute buffer.
-
#report ⇒ Object
readonly
Returns the value of attribute report.
-
#site ⇒ Object
readonly
Returns the value of attribute site.
-
#stats ⇒ Object
readonly
Returns the value of attribute stats.
Instance Method Summary collapse
-
#add_category(params) ⇒ Object
Returns a handle needed for adding discussions.
- #add_discussion(category_key, params) ⇒ Object
- #add_kb(section_key, params) ⇒ Object
- #add_section(params) ⇒ Object
-
#add_user(params) ⇒ Object
Returns the params on success, nil on failure.
- #categories ⇒ Object
- #category_id(cat) ⇒ Object
- #category_key(cat) ⇒ Object
- #discussions(category_key) ⇒ Object
-
#initialize(site_name, options = {}) ⇒ Archive
constructor
Options:.
- #kbs(section_key) ⇒ Object
- #section_id(section) ⇒ Object
- #section_key(section) ⇒ Object
- #sections ⇒ Object
- #users ⇒ Object
- #write_archive ⇒ Object
- #write_categories_and_discussions ⇒ Object
- #write_category(c) ⇒ Object
- #write_discussion(category_id, discussion) ⇒ Object
- #write_discussions(category) ⇒ Object
- #write_kb(section_id, kb) ⇒ Object
- #write_kbs(section) ⇒ Object
- #write_section(s) ⇒ Object
- #write_sections_and_kbs ⇒ Object
- #write_user(user) ⇒ Object
- #write_users ⇒ Object
Constructor Details
#initialize(site_name, options = {}) ⇒ Archive
Options:
- buffer
-
When true, don’t flush to disk until the end. Defaults to false.
56 57 58 59 60 61 62 63 64 65 |
# File 'lib/tender_import/archive.rb', line 56 def initialize site_name, = {} @site = site_name @export_dir = ".#{site_name}-export-#{$$}" @report = [] @import = {} @stats = {} @buffer = .key?(:buffer) ? !![:buffer] : false @category_counter = Hash.new(0) @section_counter = Hash.new(0) end |
Instance Attribute Details
#buffer ⇒ Object (readonly)
Returns the value of attribute buffer.
50 51 52 |
# File 'lib/tender_import/archive.rb', line 50 def buffer @buffer end |
#report ⇒ Object (readonly)
Returns the value of attribute report.
50 51 52 |
# File 'lib/tender_import/archive.rb', line 50 def report @report end |
#site ⇒ Object (readonly)
Returns the value of attribute site.
50 51 52 |
# File 'lib/tender_import/archive.rb', line 50 def site @site end |
#stats ⇒ Object (readonly)
Returns the value of attribute stats.
50 51 52 |
# File 'lib/tender_import/archive.rb', line 50 def stats @stats end |
Instance Method Details
#add_category(params) ⇒ Object
Returns a handle needed for adding discussions
73 74 75 76 |
# File 'lib/tender_import/archive.rb', line 73 def add_category params cat = validate_and_store :category, params cat ? category_key(cat) : nil end |
#add_discussion(category_key, params) ⇒ Object
83 84 85 86 |
# File 'lib/tender_import/archive.rb', line 83 def add_discussion category_key, params raise Error, "add_discussion: missing category key" if category_key.nil? validate_and_store :discussion, params, :key => category_key end |
#add_kb(section_key, params) ⇒ Object
88 89 90 91 |
# File 'lib/tender_import/archive.rb', line 88 def add_kb section_key, params raise Error, "add_kb: missing section key" if section_key.nil? validate_and_store :kb, params, :key => section_key end |
#add_section(params) ⇒ Object
78 79 80 81 |
# File 'lib/tender_import/archive.rb', line 78 def add_section params section = validate_and_store :section, params section ? section_key(section) : nil end |
#add_user(params) ⇒ Object
Returns the params on success, nil on failure
68 69 70 |
# File 'lib/tender_import/archive.rb', line 68 def add_user params validate_and_store :user, {:state => 'user'}.merge(params) end |
#categories ⇒ Object
109 110 111 |
# File 'lib/tender_import/archive.rb', line 109 def categories @import[:category] end |
#category_id(cat) ⇒ Object
97 98 99 |
# File 'lib/tender_import/archive.rb', line 97 def category_id cat cat[:name].gsub(/\W+/,'_').downcase end |
#category_key(cat) ⇒ Object
93 94 95 |
# File 'lib/tender_import/archive.rb', line 93 def category_key cat "category:#{category_id cat}".downcase end |
#discussions(category_key) ⇒ Object
117 118 119 120 |
# File 'lib/tender_import/archive.rb', line 117 def discussions category_key raise Error, "discussions: missing category key" if category_key.nil? @import[category_key] || [] end |
#kbs(section_key) ⇒ Object
122 123 124 125 |
# File 'lib/tender_import/archive.rb', line 122 def kbs section_key raise Error, "kbs: missing section key" if section_key.nil? @import[section_key] || [] end |
#section_id(section) ⇒ Object
105 106 107 |
# File 'lib/tender_import/archive.rb', line 105 def section_id section section[:title].gsub(/\W+/,'_').downcase end |
#section_key(section) ⇒ Object
101 102 103 |
# File 'lib/tender_import/archive.rb', line 101 def section_key section "section:#{section_id section}".downcase end |
#sections ⇒ Object
113 114 115 |
# File 'lib/tender_import/archive.rb', line 113 def sections @import[:section] end |
#users ⇒ Object
127 128 129 |
# File 'lib/tender_import/archive.rb', line 127 def users @import[:user] end |
#write_archive ⇒ Object
131 132 133 134 135 136 137 138 139 |
# File 'lib/tender_import/archive.rb', line 131 def write_archive write_users if users write_categories_and_discussions if categories write_sections_and_kbs if sections export_file = "export_#{site}.tgz" system "tar -zcf #{export_file} -C #{export_dir} ." system "rm -rf #{export_dir}" return export_file end |
#write_categories_and_discussions ⇒ Object
169 170 171 172 173 174 |
# File 'lib/tender_import/archive.rb', line 169 def write_categories_and_discussions categories.each do |c| write_category c write_discussions c end end |
#write_category(c) ⇒ Object
155 156 157 158 159 160 |
# File 'lib/tender_import/archive.rb', line 155 def write_category c mkdir_p export_dir('categories') File.open(File.join(export_dir('categories'), "#{category_id(c)}.json"), "w") do |file| file.puts Yajl::Encoder.encode(c) end end |
#write_discussion(category_id, discussion) ⇒ Object
183 184 185 186 187 188 189 190 |
# File 'lib/tender_import/archive.rb', line 183 def write_discussion category_id, discussion @category_counter[category_id] += 1 dir = File.join(export_dir('categories'), category_id) mkdir_p dir File.open(File.join(dir, "#{@category_counter[category_id]}.json"), "w") do |file| file.puts Yajl::Encoder.encode(discussion) end end |
#write_discussions(category) ⇒ Object
201 202 203 204 205 |
# File 'lib/tender_import/archive.rb', line 201 def write_discussions category discussions(category_key(category)).each do |d| write_discussion category_id(category), d end end |
#write_kb(section_id, kb) ⇒ Object
192 193 194 195 196 197 198 199 |
# File 'lib/tender_import/archive.rb', line 192 def write_kb section_id, kb @section_counter[section_id] += 1 dir = File.join(export_dir('sections'), section_id) mkdir_p dir File.open(File.join(dir, "#{@section_counter[section_id]}.json"), "w") do |file| file.puts Yajl::Encoder.encode(kb) end end |
#write_kbs(section) ⇒ Object
207 208 209 210 211 |
# File 'lib/tender_import/archive.rb', line 207 def write_kbs section kbs(section_key(section)).each do |k| write_kb section_id(s), k end end |
#write_section(s) ⇒ Object
162 163 164 165 166 167 |
# File 'lib/tender_import/archive.rb', line 162 def write_section s mkdir_p export_dir('sections') File.open(File.join(export_dir('sections'), "#{section_id(s)}.json"), "w") do |file| file.puts Yajl::Encoder.encode(s) end end |
#write_sections_and_kbs ⇒ Object
176 177 178 179 180 181 |
# File 'lib/tender_import/archive.rb', line 176 def write_sections_and_kbs sections.each do |s| write_section s write_kbs s end end |
#write_user(user) ⇒ Object
141 142 143 144 145 146 147 |
# File 'lib/tender_import/archive.rb', line 141 def write_user user return unless user mkdir_p export_dir('users') File.open(File.join(export_dir('users'), "#{user[:email].gsub(/\W+/,'_')}.json"), "w") do |file| file.puts Yajl::Encoder.encode(user) end end |
#write_users ⇒ Object
149 150 151 152 153 |
# File 'lib/tender_import/archive.rb', line 149 def write_users users.each do |u| write_user u end end |