Class: Csv
- Inherits:
-
Object
- Object
- Csv
- Defined in:
- lib/sacrifice/csv.rb
Constant Summary collapse
- CREATE_OPTIONS =
[:name, :install, :locale]
- CHANGE_OPTIONS =
[:password]
- OUTPUT_ORDER =
[:id, :name, :email, :password, :login_url, :access_token]
Class Method Summary collapse
- .erase(app_name, file) ⇒ Object
- .friends(user, friends) ⇒ Object
- .generate(app_name, file, friends_file) ⇒ Object
- .ids(app, csv_file) ⇒ Object
- .init_output(app_name, file) ⇒ Object
Class Method Details
.erase(app_name, file) ⇒ Object
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/sacrifice/csv.rb', line 68 def self.erase app_name, file users = App.find!(app_name).users headers = [] CSV.read(file, headers: true, header_converters: :symbol).each { |data| if headers.empty? headers = CSV.read(file).first.map { |header| header.to_sym } end user = users.find { |user| user.id.to_s == data[:id].to_s } if user user.destroy puts "remove user ##{user.id}" else puts "user ##{data[:id]} not found" end } end |
.friends(user, friends) ⇒ Object
96 97 98 99 100 101 102 103 |
# File 'lib/sacrifice/csv.rb', line 96 def self.friends user, friends if friends.any? friends.each { |friend| user.send_friend_request_to(friend) friend.send_friend_request_to(user) } end end |
.generate(app_name, file, friends_file) ⇒ Object
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/sacrifice/csv.rb', line 10 def self.generate app_name, file, friends_file app = App.find! app_name output = init_output(app_name, file) friends = ids(app, friends_file) headers = [] CSV.read(file, headers: true, header_converters: :symbol).each { |data| if headers.empty? headers = CSV.read(file).first.map { |header| header.to_sym } else puts '===================================' end # set default options = {locale: 'ja_JP'} = {} # read option headers.each { |option| [option] = data[option] if CREATE_OPTIONS.include? option [option] = data[option] if CHANGE_OPTIONS.include? option } # execute create user = nil # repeat creating and destroying until user who has target gender created begin user.destroy if user user = app.create_user() end while user.invalid_gender(data[:gender]) # execute change if .any? and user.change() user.password = [:password] if [:password] elsif [:password] puts "Failed to update password to #{[:password]}" end # print created user user_map = user.attrs user_map[:name] = [:name] if [:name] puts user_map.map { |key, value| "#{key.upcase} : #{value}" } # log created user CSV.open(output, 'a') { |line| output_line = [] OUTPUT_ORDER.each { |key| output_line.push user_map[key] } line << output_line } unless user.id.nil? user = app.find_user(user.id) friends user, friends } end |
.ids(app, csv_file) ⇒ Object
88 89 90 91 92 93 94 |
# File 'lib/sacrifice/csv.rb', line 88 def self.ids app, csv_file ids = [] CSV.read(csv_file, headers: true, header_converters: :symbol).each { |data| ids.push app.find_user(data[:id]) or raise Thor::Error, "No user found w/id #{user.inspect}" } if csv_file ids end |
.init_output(app_name, file) ⇒ Object
105 106 107 108 109 110 111 112 |
# File 'lib/sacrifice/csv.rb', line 105 def self.init_output(app_name, file) output = "sacrificed_#{app_name}_#{file}" File.open(output, 'a').close output_file = File.open(output, 'r') CSV.open(output, 'a') { |line| line << OUTPUT_ORDER } if output_file.size == 0 output_file.close output end |