Class: BruntAPI::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/brunt_api/client.rb

Instance Method Summary collapse

Constructor Details

#initializeClient

Returns a new instance of Client.



9
10
11
12
13
14
15
# File 'lib/brunt_api/client.rb', line 9

def initialize
  @conn = Faraday.new do |faraday|
    faraday.request :json
    faraday.use :cookie_jar
    faraday.adapter Faraday.default_adapter
  end
end

Instance Method Details

#get_state(thing_uri) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
# File 'lib/brunt_api/client.rb', line 42

def get_state(thing_uri)
  res = @conn.get do |req|
    req.url 'https://thing.brunt.co/thing' + thing_uri
  end

  if res.success?
    JSON.parse(res.body)
  else
    handle_failed_response(res, 'Failed to get state of thing.')
  end
end

#get_thingsObject



30
31
32
33
34
35
36
37
38
39
40
# File 'lib/brunt_api/client.rb', line 30

def get_things
  res = @conn.get do |req|
    req.url 'https://sky.brunt.co/thing'
  end

  if res.success?
    JSON.parse(res.body)
  else
    handle_failed_response(res, 'Failed to get things.')
  end
end

#login(username, password) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/brunt_api/client.rb', line 17

def (username, password)
  res = @conn.post do |req|
    req.url 'https://sky.brunt.co/session'
    req.body = { ID: username, PASS: password }
  end

  if res.success?
    JSON.parse(res.body)
  else
    handle_failed_response(res, 'Failed to login.')
  end
end

#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