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



88
89
90
91
92
# File 'lib/ayadn/fileops.rb', line 88

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



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

def self.make_paths(files_array)
  files_array.map do |file|
    unless File.exist?(file)
      Status.new.bad_path
      exit
    end
    File.absolute_path(file)
  end
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



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/ayadn/fileops.rb', line 44

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
    Status.new.no_curl
    exit
  end
end

.upload_avatar(file) ⇒ Object



70
71
72
73
74
75
76
77
# 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
    Status.new.no_curl
    exit
  end
end

.upload_cover(file) ⇒ Object



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

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

.upload_files(files) ⇒ Object



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

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