Class: TeamworkApi::API::Params
- Inherits:
-
Object
- Object
- TeamworkApi::API::Params
- Defined in:
- lib/teamwork_api/api/params.rb
Instance Method Summary collapse
- #caseify_keys(h) ⇒ Object
- #default(args) ⇒ Object
-
#initialize(args) ⇒ Params
constructor
A new instance of Params.
- #optional(*keys) ⇒ Object
- #required(*keys) ⇒ Object
- #to_h ⇒ Object
Constructor Details
#initialize(args) ⇒ Params
Returns a new instance of Params.
8 9 10 11 12 13 14 15 |
# File 'lib/teamwork_api/api/params.rb', line 8 def initialize(args) raise ArgumentError.new('Required to be initialized with a Hash') unless args.is_a? Hash @args = args @required = [] @optional = [] @defaults = {} end |
Instance Method Details
#caseify_keys(h) ⇒ Object
54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/teamwork_api/api/params.rb', line 54 def caseify_keys(h) caseified_hash = {} h.each do |k, v| if k =~ /-/ key = k.to_s else key = k.to_s.camelize(:lower) end caseified_hash[key] = v end caseified_hash end |
#default(args) ⇒ Object
27 28 29 30 31 32 |
# File 'lib/teamwork_api/api/params.rb', line 27 def default(args) args.each do |k, v| @defaults[k] = v end self end |
#optional(*keys) ⇒ Object
22 23 24 25 |
# File 'lib/teamwork_api/api/params.rb', line 22 def optional(*keys) @optional.concat(keys) self end |
#required(*keys) ⇒ Object
17 18 19 20 |
# File 'lib/teamwork_api/api/params.rb', line 17 def required(*keys) @required.concat(keys) self end |
#to_h ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/teamwork_api/api/params.rb', line 34 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 @optional.each do |k| h[k] = @args[k] if @args.include?(k) end @defaults.each do |k, v| @args.key?(k) ? h[k] = @args[k] : h[k] = v end caseify_keys(h) end |