Class: OpenHAB::API

Inherits:
Object
  • Object
show all
Defined in:
lib/rspec/openhab/api.rb

Instance Method Summary collapse

Constructor Details

#initialize(url, token = nil) ⇒ API



7
8
9
10
11
12
13
14
# File 'lib/rspec/openhab/api.rb', line 7

def initialize(url, token = nil)
  @faraday = Faraday.new(url) do |f|
    f.response :raise_error
    f.response :json
    f.path_prefix = "/rest/"
    f.headers = { "X-OPENHAB-TOKEN" => token } if token
  end
end

Instance Method Details

#authenticated?Boolean



52
53
54
# File 'lib/rspec/openhab/api.rb', line 52

def authenticated?
  @faraday.headers.key?("X-OPENHAB-TOKEN")
end

#channel_typesObject



40
41
42
# File 'lib/rspec/openhab/api.rb', line 40

def channel_types
  @faraday.get("channel-types").body
end

#item(name) ⇒ Object



34
35
36
37
38
# File 'lib/rspec/openhab/api.rb', line 34

def item(name)
  @faraday.get("items/#{name}").body
rescue Faraday::ResourceNotFound
  nil
end

#itemsObject



30
31
32
# File 'lib/rspec/openhab/api.rb', line 30

def items
  @faraday.get("items", metadata: ".*").body
end

#localeObject



22
23
24
# File 'lib/rspec/openhab/api.rb', line 22

def locale
  root_data["locale"]
end

#measurement_systemObject



26
27
28
# File 'lib/rspec/openhab/api.rb', line 26

def measurement_system
  root_data["measurementSystem"]
end

#thing_typesObject



44
45
46
# File 'lib/rspec/openhab/api.rb', line 44

def thing_types
  @faraday.get("thing-types").body
end

#thingsObject



48
49
50
# File 'lib/rspec/openhab/api.rb', line 48

def things
  @faraday.get("things").body
end

#versionObject



16
17
18
19
20
# File 'lib/rspec/openhab/api.rb', line 16

def version
  version = root_data.dig("runtimeInfo", "version")
  version = "#{version}-SNAPSHOT" if root_data.dig("runtimeInfo", "buildString")&.start_with?("Build #")
  version
end