Method: BruntAPI::Client#set_position

Defined in:
lib/brunt_api/client.rb

#set_position(thing_uri, position) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/brunt_api/client.rb', line 54

def set_position(thing_uri, position)
  unless position.kind_of?(Numeric)
    raise ArgumentError, 'Position must be Numeric.'
  end
  if position.kind_of?(Complex)
    raise ArgumentError, 'Position must not be Complex.'
  end
  if position < 0 or position > 100
    raise ArgumentError, 'Position must be in range (0.0..100.0).'
  end

  res = @conn.put do |req|
    req.url 'https://thing.brunt.co/thing' + thing_uri
    req.body = { requestPosition: position.to_f.to_s }
  end

  if res.success?
    begin
      JSON.parse(res.body)
    rescue
      'success'
    end
  else
    handle_failed_response(res, 'Failed to set position of thing.')
  end
end