Sha256: 43b7263d1bfe9eb86bcfcfa76eb21ec3a6cef9d8527eb6099e86ce6395c94296
Contents?: true
Size: 1010 Bytes
Versions: 3
Compression:
Stored size: 1010 Bytes
Contents
module DiscourseApi module API def self.params(args) Params.new(args) end class Params def initialize(args) @args = args @required = [] @optional = [] @defaults = {} end def required(*keys) @required.concat(keys) self end def optional(*keys) @optional.concat(keys) self end def default(args) args.each do |k,v| @defaults[k] = v end self end 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 end end end
Version data entries
3 entries across 3 versions & 1 rubygems
Version | Path |
---|---|
discourse_api-0.3.0 | lib/discourse_api/api/params.rb |
discourse_api-0.2.9 | lib/discourse_api/api/params.rb |
discourse_api-0.2.8 | lib/discourse_api/api/params.rb |