class StorageRoomExporter
Constants
- PAGE_SIZE
Attributes
collections[R]
Public Instance Methods
export_collections()
click to toggle source
# File lib/storage_room_exporter.rb, line 11 def export_collections puts 'Exporting collections:' collections.each do |collection| puts collection['name'] save_to_file(COLLECTIONS_DATA_DIR, collection['entry_type'], format_json(collection)) end end
export_entries()
click to toggle source
# File lib/storage_room_exporter.rb, line 19 def export_entries collections.each do |collection| puts "Exporting entries for: #{collection['name']}" entries(collection).each do |entry| entry_id = File.basename(entry['@url']) save_to_file("#{ENTRIES_DATA_DIR}/#{collection['entry_type'].downcase}", "#{entry_id}", format_json(entry)) end end end
mapping_collections_input_types()
click to toggle source
# File lib/storage_room_exporter.rb, line 29 def mapping_collections_input_types read_collection_data do |collection_attributes, fields, file_path| translate_fields(fields) File.open(file_path, 'w') { |file| file.write(format_json(collection_attributes)) } end end
Private Instance Methods
collection_id(collection)
click to toggle source
# File lib/storage_room_exporter.rb, line 95 def collection_id(collection) File.basename(collection['@url']) end
entries(collection)
click to toggle source
# File lib/storage_room_exporter.rb, line 99 def entries(collection) entries = [] page = 1 begin response = get_request("collections/#{collection_id(collection)}/entries", page)['array'] total = response['@total_resources'] entries += response['resources'] page += 1 end while entries.size < total entries end
format_json(item)
click to toggle source
# File lib/storage_room_exporter.rb, line 91 def format_json(item) JSON.pretty_generate(JSON.parse(item.to_json)) end
get_request(path, page= nil)
click to toggle source
# File lib/storage_room_exporter.rb, line 77 def get_request(path, page= nil) url = "#{STORAGE_ROOM_URL}#{CREDENTIALS['ACCOUNT_ID']}/#{path}.json?auth_token=#{CREDENTIALS['APPLICATION_API_KEY']}&per_page=#{PAGE_SIZE}" url += "&page=#{page}" if page uri = URI.parse(url) http = Net::HTTP.new(uri.host, uri.port) request = Net::HTTP::Get.new(uri.request_uri) response = http.request(request) if response.code == '200' JSON.parse(response.body) else fail "ERROR: #{response.body}\n#{uri.inspect}" end end
mapping_array_type(field)
click to toggle source
# File lib/storage_room_exporter.rb, line 63 def mapping_array_type(field) field['link_type'] = 'Entry' if field[:@type] == 'ManyAssociationField' field['link'] = 'Symbol' if field[:@type] == 'ArrayField' end
read_collection_data() { |collection_attributes, collection_attributes, file_path| ... }
click to toggle source
# File lib/storage_room_exporter.rb, line 38 def read_collection_data Dir.glob("#{COLLECTIONS_DATA_DIR}/*json") do |file_path| collection_attributes = JSON.parse(File.read(file_path), symbolize_names: true) yield collection_attributes, collection_attributes[:fields], file_path end end
save_to_file(dir, file_name, json)
click to toggle source
# File lib/storage_room_exporter.rb, line 68 def save_to_file(dir, file_name, json) FileUtils.mkdir_p dir unless File.directory?(dir) File.open("#{dir}/#{file_name.downcase}.json", 'w') { |file| file.write(json) } end
translate_fields(fields)
click to toggle source
# File lib/storage_room_exporter.rb, line 45 def translate_fields(fields) fields.each do |field| translate_input_type(field) mapping_array_type(field) end end
translate_input_type(field)
click to toggle source
# File lib/storage_room_exporter.rb, line 52 def translate_input_type(field) field_type = field[:input_type] unless CONTENTFUL_TYPES.include? field_type field[:input_type] = begin I18n.t! "fields.input_type.#{field[:@type]}.#{field_type}" rescue I18n::MissingTranslationData I18n.t "fields.input_type.#{field_type}" end end end