Class: Ayadn::FileOps

Inherits:
Object
  • Object
show all
Defined in:
lib/ayadn/fileops.rb

Class Method Summary collapse

Class Method Details

.download_url(name, url) ⇒ Object



32
33
34
35
# File 'lib/ayadn/fileops.rb', line 32

def self.download_url(name, url)
  file = CNX.get_response_from(url)
  File.write(Settings.config[:paths][:downloads] + "/#{name}", file)
end

.get_users(list) ⇒ Object



86
87
88
89
90
# File 'lib/ayadn/fileops.rb', line 86

def self.get_users(list)
  h = {}
  list.each {|k,v| h[k] = { username: v[0], name: v[1] }}
  h
end

.make_paths(files_array) ⇒ Object



63
64
65
66
67
68
# File 'lib/ayadn/fileops.rb', line 63

def self.make_paths(files_array)
  files_array.map do |file|
    abort(Status.bad_path) unless File.exist?(file)
    File.absolute_path(file)
  end
end

.old_ayadn?Boolean

Returns:

  • (Boolean)


37
38
39
# File 'lib/ayadn/fileops.rb', line 37

def self.old_ayadn?
  Dir.exist?(Dir.home + "/ayadn/data")
end

.save_followers_list(list) ⇒ Object



22
23
24
25
# File 'lib/ayadn/fileops.rb', line 22

def self.save_followers_list(list)
  fr = get_users(list)
  File.write(Settings.config[:paths][:lists] + "/followers.json", fr.to_json)
end

.save_followings_list(list) ⇒ Object



17
18
19
20
# File 'lib/ayadn/fileops.rb', line 17

def self.save_followings_list(list)
  fg = get_users(list)
  File.write(Settings.config[:paths][:lists] + "/followings.json", fg.to_json)
end


5
6
7
# File 'lib/ayadn/fileops.rb', line 5

def self.save_links(obj, name)
  File.write(Settings.config[:paths][:lists] + "/#{name}", obj.to_json)
end

.save_message(resp) ⇒ Object



13
14
15
# File 'lib/ayadn/fileops.rb', line 13

def self.save_message(resp)
  File.write(Settings.config[:paths][:messages] + "/#{resp['data']['id']}.json", resp['data'].to_json)
end

.save_muted_list(list) ⇒ Object



27
28
29
30
# File 'lib/ayadn/fileops.rb', line 27

def self.save_muted_list(list)
  mt = get_users(list)
  File.write(Settings.config[:paths][:lists] + "/muted.json", mt.to_json)
end

.save_post(resp) ⇒ Object



9
10
11
# File 'lib/ayadn/fileops.rb', line 9

def self.save_post(resp)
  File.write(Settings.config[:paths][:posts] + "/#{resp['data']['id']}.json", resp['data'].to_json)
end

.upload(file, token) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/ayadn/fileops.rb', line 48

def self.upload(file, token)
  begin
    case File.extname(file).downcase
    when ".png"
      `curl -k -H 'Authorization: BEARER #{token}' https://api.app.net/files -F 'type=com.ayadn.files' -F "content=@#{file};type=image/png" -F 'public=true' -X POST`
    when ".gif"
      `curl -k -H 'Authorization: BEARER #{token}' https://api.app.net/files -F 'type=com.ayadn.files' -F "content=@#{file};type=image/gif" -F 'public=true' -X POST`
    else #jpg or jpeg or JPG or JPEG, automatically recognized as such
      `curl -k -H 'Authorization: BEARER #{token}' https://api.app.net/files -F 'type=com.ayadn.files' -F "content=@#{file}" -F 'public=true' -X POST`
    end
  rescue Errno::ENOENT
    abort(Status.no_curl)
  end
end

.upload_avatar(file) ⇒ Object



70
71
72
73
74
75
76
# File 'lib/ayadn/fileops.rb', line 70

def self.upload_avatar file
  begin
    `curl -X POST -H "Authorization: Bearer #{Settings.user_token}" -F "avatar=@#{file}" #{Endpoints.new.avatar}`
  rescue Errno::ENOENT
    abort(Status.no_curl)
  end
end

.upload_cover(file) ⇒ Object



78
79
80
81
82
83
84
# File 'lib/ayadn/fileops.rb', line 78

def self.upload_cover file
  begin
    `curl -X POST -H "Authorization: Bearer #{Settings.user_token}" -F "cover=@#{file}" #{Endpoints.new.cover}`
  rescue Errno::ENOENT
    abort(Status.no_curl)
  end
end

.upload_files(files) ⇒ Object



41
42
43
44
45
46
# File 'lib/ayadn/fileops.rb', line 41

def self.upload_files files
  files.map do |file|
    puts "\n#{file}\n\n"
    JSON.load(self.upload(file, Settings.user_token))
  end
end