Class: DiscourseApi::API::Params

Inherits:
Object
  • Object
show all
Defined in:
lib/discourse_api/api/params.rb

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ Params

Returns a new instance of Params.



8
9
10
11
12
13
# File 'lib/discourse_api/api/params.rb', line 8

def initialize(args)
  @args = args
  @required = []
  @optional = []
  @defaults = {}
end

Instance Method Details

#default(args) ⇒ Object



25
26
27
28
29
30
# File 'lib/discourse_api/api/params.rb', line 25

def default(args)
  args.each do |k,v|
    @defaults[k] = v
  end
  self
end

#optional(*keys) ⇒ Object



20
21
22
23
# File 'lib/discourse_api/api/params.rb', line 20

def optional(*keys)
  @optional.concat(keys)
  self
end

#required(*keys) ⇒ Object



15
16
17
18
# File 'lib/discourse_api/api/params.rb', line 15

def required(*keys)
  @required.concat(keys)
  self
end

#to_hObject



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/discourse_api/api/params.rb', line 32

def to_h
  h = {}

  @required.each do |k|
    h[k] = @args[k]
    raise ArgumentError.new("#{k} is required but not specified") unless h[k]
  end

  h =
    if @optional.length == 0
      @args.dup
    else
      @optional.each do |k|
        h[k] = @args[k]
      end
      h
    end

  @defaults.each do |k,v|
    h[k] = v unless h.key?(k)
  end

  h

end