Class: Parse::Importer::Store

Inherits:
Object
  • Object
show all
Defined in:
lib/parse/importer/store.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(dirname = nil) ⇒ Store

Returns a new instance of Store.



6
7
8
9
10
11
12
# File 'lib/parse/importer/store.rb', line 6

def initialize dirname=nil
  @parse_class_table = {}
  @parse_object_table = {}
  @store = {}
  @relations = []
  load dirname if dirname
end

Instance Attribute Details

#storeObject (readonly)

Returns the value of attribute store.



4
5
6
# File 'lib/parse/importer/store.rb', line 4

def store
  @store
end

Instance Method Details

#add_relationsObject



90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/parse/importer/store.rb', line 90

def add_relations
  @relations.each do |filepath, column_name, parse_class_name|
    parse_class = Parse::Object(parse_class_name)
    results = JSON.parse File.read(filepath)
    results['results'].each do |relation|
      object = parse_class.find_by_id new_object_id_from_old_one(relation['owningId'])
      related_object = @parse_object_table[relation['relatedId']]
      object.send "#{column_name}=", Parse::Op::AddRelation.new(related_object.pointer)
      object.save!
      puts "#{relation['owningId']} and #{relation['relatedId']} have been connected."
    end
  end
end

#create_blank_objectsObject



45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/parse/importer/store.rb', line 45

def create_blank_objects
  @parse_class_table.each do |old_object_id, parse_class|
    parse_object = parse_class.new
    if parse_class.parse_class_name == '_User'
      parse_object.username = SecureRandom.uuid # temporary username
      parse_object.password = 'password'
    end
    parse_object.save
    @parse_object_table[old_object_id] = parse_object
    puts "#{parse_class.parse_class_name}(#{old_object_id}) has been created."
  end
end

#load(dirname) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/parse/importer/store.rb', line 14

def load dirname
  Dir["#{dirname}/*"].each do |filepath|
    filename = filepath.sub(%r|#{dirname}/|, '')
    if filename =~ /^_Join:(.*):(.*).json$/
      column_name = $1
      parse_class_name = $2
      @relations << [filepath, column_name, parse_class_name]
    elsif filename =~ /^(.*).json$/
      parse_class_name = $1
      parse_class = Parse::Object(parse_class_name)
      rows = []
      results = JSON.parse File.read(filepath)
      results['results'].each do |data|
        rows << data
        @parse_class_table[data['objectId']] = parse_class
      end
      @store[parse_class_name] = rows
    end
  end
end

#new_object_id_from_old_one(object_id) ⇒ Object



35
36
37
# File 'lib/parse/importer/store.rb', line 35

def new_object_id_from_old_one object_id
  @parse_object_table[object_id].objectId
end

#saveObject



39
40
41
42
43
# File 'lib/parse/importer/store.rb', line 39

def save
  create_blank_objects
  update_objects
  add_relations
end

#update_objectsObject



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/parse/importer/store.rb', line 58

def update_objects
  @store.each do |parse_class_name, rows|
    rows.each do |data|
      parse_object = @parse_object_table[data['objectId']]
      data.each do |k, v|
        if v.is_a? Hash
          case v['__type']
          when 'Pointer'
            v['objectId'] = new_object_id_from_old_one v['objectId']
          when 'File'
            open v['url'] do |filedata|
              filepath = Tempfile.open 'temp' do |tempfile|
                tempfile.write filedata.read
                tempfile.path
              end
              name = File.basename(v['url']).split('-').last
              parse_file = Parse::ParseFile.new :name => name, :content => filepath
              parse_file.save
              data[k] = parse_file
            end
          end
        end
      end
      data.reject! do |k, v|
        %w(sessionToken bcryptPassword).include? k
      end
      parse_object.update! data
      puts "#{parse_object.parse_class_name}(#{data['objectId']}) has been updated."
    end
  end
end