Class: Ayadn::FileOps

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

Class Method Summary collapse

Class Method Details

.cache_list(list, name) ⇒ Object



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

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

.cached_list(name) ⇒ Object



9
10
11
12
13
14
15
16
17
# File 'lib/ayadn/fileops.rb', line 9

def self.cached_list(name)
  file_path = Settings.config[:paths][:lists] + "/.#{name}"
  if File.exist?(file_path)
    data = File.read(file_path)
    JSON.load(data)
  else
    nil
  end
end

.download_url(name, url) ⇒ Object



46
47
48
49
# File 'lib/ayadn/fileops.rb', line 46

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



102
103
104
105
106
# File 'lib/ayadn/fileops.rb', line 102

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



74
75
76
77
78
79
80
81
82
# File 'lib/ayadn/fileops.rb', line 74

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



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

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



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

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


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

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

.save_message(resp) ⇒ Object



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

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



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

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



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

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

.upload(file, token) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/ayadn/fileops.rb', line 58

def self.upload(file, token)
  begin
    case File.extname(file).downcase
    when ".png"
      `curl -k -H 'Authorization: BEARER #{token}' #{Settings.config[:api][:baseURL]}/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}' #{Settings.config[:api][:baseURL]}/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}' #{Settings.config[:api][:baseURL]}/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



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

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



93
94
95
96
97
98
99
100
# File 'lib/ayadn/fileops.rb', line 93

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



51
52
53
54
55
56
# File 'lib/ayadn/fileops.rb', line 51

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