Module: Adafruit::IO::Arguments

Included in:
Client
Defined in:
lib/adafruit/io/arguments.rb

Defined Under Namespace

Classes: ArgumentError

Instance Method Summary collapse

Instance Method Details

#extract_username(args) ⇒ Object

Allows us to give a username during client initialization or with a specific method.



7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/adafruit/io/arguments.rb', line 7

def extract_username(args)
  username = @username

  if args.last.is_a?(Hash) && args.last.has_key?(:username)
    username = args.last.delete(:username)
  end

  if username.nil?
    raise "cannot make request when username is nil"
  end

  [ username, args ]
end

#get_id_from_arguments(arguments) ⇒ Object

Same as get_key_from_arguments but looking for id



45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/adafruit/io/arguments.rb', line 45

def get_id_from_arguments(arguments)
  record_or_id = arguments.shift
  return nil if record_or_id.nil?

  if record_or_id.is_a?(String) || record_or_id.is_a?(Fixnum)
    record_or_id
  elsif record_or_id.is_a?(Hash) && (record_or_id.has_key?('id') || record_or_id.has_key?(:id))
    record_or_id['id'] || record_or_id[:id]
  elsif record_or_id.respond_to?(:id)
    record_or_id.id
  else
    raise 'unrecognized object or id value in arguments'
  end
end

#get_key_from_arguments(arguments) ⇒ Object

Allow users to pass a hash with key named ‘key’ or :key, or just a plain string to use as a key.

client.feed({ key: 'myfeed' })
client.feed({ 'key' => 'myfeed' })
client.feed('myfeed')

Are all equivalent.



29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/adafruit/io/arguments.rb', line 29

def get_key_from_arguments(arguments)
  record_or_key = arguments.shift
  return nil if record_or_key.nil?

  if record_or_key.is_a?(String)
    record_or_key
  elsif record_or_key.is_a?(Hash) && (record_or_key.has_key?('key') || record_or_key.has_key?(:key))
    record_or_key['key'] || record_or_key[:key]
  elsif record_or_key.respond_to?(:key)
    record_or_key.key
  else
    raise 'unrecognized object or key value in arguments'
  end
end

#get_query_from_arguments(arguments, params) ⇒ Object

Get a filtered list of parameters from the next argument



61
62
63
64
65
66
67
68
69
70
# File 'lib/adafruit/io/arguments.rb', line 61

def get_query_from_arguments(arguments, params)
  query = {}
  options = arguments.shift
  return query if options.nil?

  params.each do |param|
    query[param] = options[param.to_sym] if options.has_key?(param.to_sym)
  end
  query
end

#require_argument(arguments, argument_name) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/adafruit/io/arguments.rb', line 72

def require_argument(arguments, argument_name)
  arg = arguments.first
  if !arg.is_a?(Hash)
    raise ArgumentError.new("This request requires arguments to be a Hash containing the key: :#{argument_name}")
  end

  if !(arg.has_key?(argument_name) || arg.has_key?(argument_name.to_s))
    raise ArgumentError.new("Missing required parameter, make sure to include #{argument_name}")
  end

  arg[argument_name] || arg[argument_name.to_s]
end