Class: NineteenNinetynine::Event::Profile

Inherits:
Object
  • Object
show all
Defined in:
lib/nineteen_ninetynine/event/profile.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(event) ⇒ Profile

Returns a new instance of Profile.



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/nineteen_ninetynine/event/profile.rb', line 6

def initialize(event)
  @raw = event

  @pubkey = event["pubkey"]
  content = JSON.parse(event["content"])
  @name = content["name"]
  @picture_url = content["picture"]
  set_icon

  if !@picture_url.nil? && !@picture_url.empty?
    unless File.exist?(icon_path)
      Thread.start do
        download_icon_image
      end
    end
  end
end

Instance Attribute Details

#icon_ioObject

Returns the value of attribute icon_io.



4
5
6
# File 'lib/nineteen_ninetynine/event/profile.rb', line 4

def icon_io
  @icon_io
end

#icon_pathObject

Returns the value of attribute icon_path.



4
5
6
# File 'lib/nineteen_ninetynine/event/profile.rb', line 4

def icon_path
  @icon_path
end

#nameObject

Returns the value of attribute name.



4
5
6
# File 'lib/nineteen_ninetynine/event/profile.rb', line 4

def name
  @name
end

#pubkeyObject

Returns the value of attribute pubkey.



4
5
6
# File 'lib/nineteen_ninetynine/event/profile.rb', line 4

def pubkey
  @pubkey
end

#rawObject

Returns the value of attribute raw.



4
5
6
# File 'lib/nineteen_ninetynine/event/profile.rb', line 4

def raw
  @raw
end

Instance Method Details

#download_icon_imageObject



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/nineteen_ninetynine/event/profile.rb', line 40

def download_icon_image
  if @picture_url.start_with?("data:image/")
    File.write(icon_path, Base64.decode64(@picture_url.split(",")[1]))
  elsif @picture_url.start_with?("http")
    url = URI.parse @picture_url
    begin
    res = Net::HTTP.get_response(url)
    case res
    when Net::HTTPSuccess, Net::HTTPRedirection
      File.write(icon_path, res.body)
    when Net::HTTPClientError, Net::HTTPServerError
      # ignore
    else
      puts "unknown"
      puts @picture_url
    end
  rescue SocketError, Net::OpenTimeout, OpenSSL::SSL::SSLError => e
    # ignore
  end
  else
    puts "Unknown schema"
    puts @picture_url
  end
end

#icon_dirObject



36
37
38
# File 'lib/nineteen_ninetynine/event/profile.rb', line 36

def icon_dir
  "#{XDG_CONFIG_DIR}/icons"
end

#set_iconObject



24
25
26
27
28
29
30
# File 'lib/nineteen_ninetynine/event/profile.rb', line 24

def set_icon
  if File.exist? icon_path
    @icon_path = icon_path
  else
    @icon_path = icon_dir + "/default.png"
  end
end