class FbResource::Client

Attributes

config[R]

Public Class Methods

get_token(url: "http://my.farmbot.io", email: "EMAIL NOT SET", password: "PASSWORD NOT SET", credentials: nil) click to toggle source
# 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
new() { |config| ... } click to toggle source
# File lib/client.rb, line 9
def initialize(&blk)
  @config = Config.build
  yield(@config)
  post_config
end
public_key(url = "my.farmbot.io") click to toggle source
# 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

Private Class Methods

from_credentials(resource_url, credentials) click to toggle source
# File lib/client.rb, line 81
def self.from_credentials(resource_url, credentials)
  payload = {user: {credentials: credentials}}
  RestClient.post(resource_url, payload)
end
from_email_and_password(resource_url, email, password) click to toggle source
# File lib/client.rb, line 86
def self.from_email_and_password(resource_url, email, password)
  payload = {user: {email: email, password: password}}
  RestClient.post(resource_url, payload)
end
parse_token(result) click to toggle source
# File lib/client.rb, line 91
def self.parse_token(result)
  json = JSON.parse(result)
  token = json["token"]
  string = token["encoded"]
  string
end

Public Instance Methods

api_url() click to toggle source
# 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
device() click to toggle source
# File lib/client.rb, line 43
def device
  @device ||= FbResource::Device.new(config)
end
plants() click to toggle source
# File lib/client.rb, line 55
def plants
  @plants ||= FbResource::Plants.new(config)
end
public_key() click to toggle source

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

# File lib/client.rb, line 30
def public_key
  @public_key ||= self.class.public_key(config.url)
end
schedules() click to toggle source
# File lib/client.rb, line 47
def schedules
  @schedules ||= FbResource::Schedules.new(config)
end
sequences() click to toggle source
# File lib/client.rb, line 51
def sequences
  @sequences ||= FbResource::Sequences.new(config)
end

Private Instance Methods

invalidate(msg) click to toggle source
# File lib/client.rb, line 72
def invalidate(msg)
  raise InvalidConfig.new, msg
end
post_config() click to toggle source
# File lib/client.rb, line 76
def post_config
  invalidate("config needs `token` attribute") unless @config.token
  @config.url = api_url
end