Class: DBLRuby::Widget

Inherits:
Object
  • Object
show all
Defined in:
lib/dblruby/widget.rb

Overview

Make a widget

Constant Summary collapse

BASE_URL =
'https://discordbots.org/api/widget/'.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(size: 'large', file_type: 'png', small_type: nil, topcolor: nil, middlecolor: nil, usernamecolor: nil, certifiedcolor: nil, datacolor: nil, labelcolor: nil, highlightcolor: nil, avatarbg: nil, leftcolor: nil, rightcolor: nil, lefttextcolor: nil, righttextcolor: nil, noavatar: nil, id: nil) ⇒ Widget

Make a new widget

Parameters:

  • size ('large', 'small') (defaults to: 'large')

    The size of the widget.

  • file_type ('png', 'svg') (defaults to: 'png')

    The file type of the image

  • small_type ('owner', 'status', 'upvotes', 'servers', 'lib') (defaults to: nil)

    The type of small widget.

  • topcolor (Hexadecimal) (defaults to: nil)

    The top color of the widget (Large)

  • middlecolor (Hexadecimal) (defaults to: nil)

    The middle color of the widget (Large)

  • usernamecolor (Hexadecimal) (defaults to: nil)

    The username color of the widget (Large)

  • certifiedcolor (Hexadecimal) (defaults to: nil)

    The certified color of the widget (Large)

  • datacolor (Hexadecimal) (defaults to: nil)

    The data color of the widget (Large)

  • labelcolor (Hexadecimal) (defaults to: nil)

    The label color of the widget (Large)

  • highlightcolor (Hexadecimal) (defaults to: nil)

    The highlight color of the widget (Large)

  • avatarbg (Hexadecimal) (defaults to: nil)

    The avatar background color of the widget (Small)

  • leftcolor (Hexadecimal) (defaults to: nil)

    The left color of the widget (Small)

  • rightcolor (Hexadecimal) (defaults to: nil)

    The right color of the widget (Small)

  • lefttextcolor (Hexadecimal) (defaults to: nil)

    The left text color of the widget (Small)

  • righttextcolor (Hexadecimal) (defaults to: nil)

    The right text color of the widget (Small)

  • noavatar (true, false) (defaults to: nil)

    Whether there should be an avatar (Small)

Raises:


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
# File 'lib/dblruby/widget.rb', line 22

def initialize(size: 'large', file_type: 'png',
               small_type: nil,
               topcolor: nil, middlecolor: nil, usernamecolor: nil, certifiedcolor: nil, datacolor: nil, labelcolor: nil, highlightcolor: nil,
               avatarbg: nil, leftcolor: nil, rightcolor: nil, lefttextcolor: nil, righttextcolor: nil, noavatar: nil,
               id: nil)
  @size = size
  @file_type = file_type
  @small_type = small_type
  @topcolor = topcolor
  @middlecolor = middlecolor
  @usernamecolor = usernamecolor
  @certifiedcolor = certifiedcolor
  @datacolor = datacolor
  @labelcolor = labelcolor
  @highlightcolor = highlightcolor
  @avatarbg = avatarbg
  @leftcolor = leftcolor
  @rightcolor = rightcolor
  @lefttextcolor = lefttextcolor
  @righttextcolor = righttextcolor
  @noavatar = noavatar
  @id = id

  raise DBLRuby::Errors::InvalidWidget, 'Small size specified but no small type was provided' if @size == 'small' && @small_type.nil?
  raise DBLRuby::Errors::InvalidWidget, 'Small parameter specified for large widget' if @size == 'large' && (!@avatarbg.nil? || !@leftcolor.nil? || !@rightcolor.nil? || !@lefttextcolor.nil? || !@righttextcolor.nil? || !@noavatar.nil?)
  raise DBLRuby::Errors::InvalidWidget, 'Large parameter specified for small widget' if @size == 'small' && (!@topcolor.nil? || !@middlecolor.nil? || !@usernamecolor.nil? || !@certifiedcolor.nil? || !@datacolor.nil? || !@labelcolor.nil? || !@highlightcolor.nil?)

  raise DBLRuby::Errors::InvalidWidget, 'Invalid size' unless %w[small large].include? @size
  raise DBLRuby::Errors::InvalidWidget, 'Invalid small widget type' if @size == 'small' && !%w[owner status upvotes servers lib].include?(@small_type)
  raise DBLRuby::Errors::InvalidWidget, 'Invalid file type' unless %w[png svg].include? @file_type
  raise DBLRuby::Errors::InvalidWidget, 'Invalid noavatar parameter' if !@noavatar.nil? && !%w[true false].include?(@noavatar)
end

Instance Attribute Details

#avatarbg=(value) ⇒ Object (writeonly)

Sets the attribute avatarbg

Parameters:

  • value

    the value to set the attribute avatarbg to.


80
81
82
# File 'lib/dblruby/widget.rb', line 80

def avatarbg=(value)
  @avatarbg = value
end

#certifiedcolor=(value) ⇒ Object (writeonly)

Sets the attribute certifiedcolor

Parameters:

  • value

    the value to set the attribute certifiedcolor to.


76
77
78
# File 'lib/dblruby/widget.rb', line 76

def certifiedcolor=(value)
  @certifiedcolor = value
end

#datacolor=(value) ⇒ Object (writeonly)

Sets the attribute datacolor

Parameters:

  • value

    the value to set the attribute datacolor to.


77
78
79
# File 'lib/dblruby/widget.rb', line 77

def datacolor=(value)
  @datacolor = value
end

#highlightcolor=(value) ⇒ Object (writeonly)

Sets the attribute highlightcolor

Parameters:

  • value

    the value to set the attribute highlightcolor to.


79
80
81
# File 'lib/dblruby/widget.rb', line 79

def highlightcolor=(value)
  @highlightcolor = value
end

#labelcolor=(value) ⇒ Object (writeonly)

Sets the attribute labelcolor

Parameters:

  • value

    the value to set the attribute labelcolor to.


78
79
80
# File 'lib/dblruby/widget.rb', line 78

def labelcolor=(value)
  @labelcolor = value
end

#leftcolor=(value) ⇒ Object (writeonly)

Sets the attribute leftcolor

Parameters:

  • value

    the value to set the attribute leftcolor to.


81
82
83
# File 'lib/dblruby/widget.rb', line 81

def leftcolor=(value)
  @leftcolor = value
end

#lefttextcolor=(value) ⇒ Object (writeonly)

Sets the attribute lefttextcolor

Parameters:

  • value

    the value to set the attribute lefttextcolor to.


83
84
85
# File 'lib/dblruby/widget.rb', line 83

def lefttextcolor=(value)
  @lefttextcolor = value
end

#middlecolor=(value) ⇒ Object (writeonly)

Sets the attribute middlecolor

Parameters:

  • value

    the value to set the attribute middlecolor to.


74
75
76
# File 'lib/dblruby/widget.rb', line 74

def middlecolor=(value)
  @middlecolor = value
end

#rightcolor=(value) ⇒ Object (writeonly)

Sets the attribute rightcolor

Parameters:

  • value

    the value to set the attribute rightcolor to.


82
83
84
# File 'lib/dblruby/widget.rb', line 82

def rightcolor=(value)
  @rightcolor = value
end

#righttextcolor=(value) ⇒ Object (writeonly)

Sets the attribute righttextcolor

Parameters:

  • value

    the value to set the attribute righttextcolor to.


84
85
86
# File 'lib/dblruby/widget.rb', line 84

def righttextcolor=(value)
  @righttextcolor = value
end

#topcolor=(value) ⇒ Object (writeonly)

Sets the attribute topcolor

Parameters:

  • value

    the value to set the attribute topcolor to.


73
74
75
# File 'lib/dblruby/widget.rb', line 73

def topcolor=(value)
  @topcolor = value
end

#usernamecolor=(value) ⇒ Object (writeonly)

Sets the attribute usernamecolor

Parameters:

  • value

    the value to set the attribute usernamecolor to.


75
76
77
# File 'lib/dblruby/widget.rb', line 75

def usernamecolor=(value)
  @usernamecolor = value
end

Instance Method Details

#file_type=(file_type) ⇒ Object


61
62
63
64
65
# File 'lib/dblruby/widget.rb', line 61

def file_type=(file_type)
  raise DBLRuby::Errors::InvalidWidget, 'Invalid file type' unless %w[png svg].include? file_type

  @file_type = file_type
end

#noavatar=(noavatar) ⇒ Object


86
87
88
89
90
# File 'lib/dblruby/widget.rb', line 86

def noavatar=(noavatar)
  raise DBLRuby::Errors::InvalidWidget, 'Invalid noavatar parameter' unless %w[true false].include? noavatar

  @noavatar = noavatar
end

#size=(size) ⇒ Object


55
56
57
58
59
# File 'lib/dblruby/widget.rb', line 55

def size=(size)
  raise DBLRuby::Errors::InvalidWidget, 'Invalid size' unless %w[small large].include? size

  @size = size
end

#small_type=(small_type) ⇒ Object


67
68
69
70
71
# File 'lib/dblruby/widget.rb', line 67

def small_type=(small_type)
  raise DBLRuby::Errors::InvalidWidget, 'Invalid small widget type' if @size == 'small' && !%w[owner status upvotes servers lib].include?(small_type)

  @small_type = small_type
end

#urlString Also known as: to_s

Returns The URL, ready for linking.

Returns:

  • (String)

    The URL, ready for linking.


93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/dblruby/widget.rb', line 93

def url
  params = []
  if @size == 'large'
    params.push("topcolor=#{@topcolor}") unless @topcolor.nil?
    params.push("middlecolor=#{@middlecolor}") unless @middlecolor.nil?
    params.push("usernamecolor=#{@usernamecolor}") unless @usernamecolor.nil?
    params.push("certifiedcolor=#{@certifiedcolor}") unless @certifiedcolor.nil?
    params.push("datacolor=#{@datacolor}") unless @datacolor.nil?
    params.push("labelcolor=#{@labelcolor}") unless @labelcolor.nil?
    params.push("highlightcolor=#{@highlightcolor}") unless @highlightcolor.nil?
    output = "#{BASE_URL}#{@id}.#{@file_type}"
  elsif @size == 'small'
    params.push("avatarbg=#{@avatarbg}") unless @avatarbg.nil?
    params.push("leftcolor=#{@leftcolor}") unless @leftcolor.nil?
    params.push("rightcolor=#{@rightcolor}") unless @rightcolor.nil?
    params.push("certifiedcolor=#{@certifiedcolor}") unless @certifiedcolor.nil?
    params.push("lefttextcolor=#{@lefttextcolor}") unless @lefttextcolor.nil?
    params.push("righttextcolor=#{@labelcolor}") unless @righttextcolor.nil?
    params.push("noavatar=#{@noavatar}") unless @noavatar.nil?
    output = "#{BASE_URL}#{@small_type}/#{@id}.#{@file_type}"
  end
  output += "?#{params.join('&')}" unless params.length.zero?
  output
end