Class: FbResource::Client

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize {|@config| ... } ⇒ Client

Returns a new instance of Client.

Yields:



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

def initialize(&blk)
  @config = Config.build
  yield(@config)
  post_config
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



7
8
9
# File 'lib/client.rb', line 7

def config
  @config
end

Class Method Details

.get_token(url: "http://my.farmbot.io", email: "EMAIL NOT SET", password: "PASSWORD NOT SET", credentials: nil) ⇒ Object



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

def self.get_token(url: "http://my.farmbot.io",
                   email: "EMAIL NOT SET",
                   password: "PASSWORD NOT SET",
                   credentials: nil)
  # TODO handle auth errors in a more civilized manner.
  resource_url = url + "/api/tokens"
  if(credentials)
    result = from_credentials(resource_url, credentials)
  else
    result = from_email_and_password(resource_url, email, password)
  end
  parse_token(result)
end

.public_key(url = "my.farmbot.io") ⇒ Object



34
35
36
37
38
39
40
41
# File 'lib/client.rb', line 34

def self.public_key(url = "my.farmbot.io")
  wow = Http.get(url + "/api/public_key", {})
  if wow.response.code.to_i < 400
    return OpenSSL::PKey::RSA.new(wow.body)
  else
    raise "Unable to fetch public key. Is the server down?"
  end
end

Instance Method Details

#api_urlObject



59
60
61
62
63
64
65
66
67
68
# File 'lib/client.rb', line 59

def api_url
  raw = config.token.split(".")[1] + "=="
  token_string = Base64.decode64(raw)
  hash = JSON.parse(token_string)
  hash["iss"]
rescue JSON::ParserError
  msg = "Farmbot Resource couldn't parse the auth token provided:\n\n"
  msg += token_string + "\n\n"
  raise InvalidConfig, msg
end

#deviceObject



43
44
45
# File 'lib/client.rb', line 43

def device
  @device ||= FbResource::Device.new(config)
end

#plantsObject



55
56
57
# File 'lib/client.rb', line 55

def plants
  @plants ||= FbResource::Plants.new(config)
end

#public_keyObject

Useful when you need to sign arbitrary secrets and give them to the server



30
31
32
# File 'lib/client.rb', line 30

def public_key
  @public_key ||= self.class.public_key(config.url)
end

#schedulesObject



47
48
49
# File 'lib/client.rb', line 47

def schedules
  @schedules ||= FbResource::Schedules.new(config)
end

#sequencesObject



51
52
53
# File 'lib/client.rb', line 51

def sequences
  @sequences ||= FbResource::Sequences.new(config)
end