Class: FbResource::Client
- Inherits:
-
Object
- Object
- FbResource::Client
- Defined in:
- lib/client.rb
Instance Attribute Summary collapse
-
#config ⇒ Object
readonly
Returns the value of attribute config.
Class Method Summary collapse
- .get_token(url: "http://my.farmbot.io", email: "EMAIL NOT SET", password: "PASSWORD NOT SET", credentials: nil) ⇒ Object
- .public_key(url = "my.farmbot.io") ⇒ Object
Instance Method Summary collapse
- #api_url ⇒ Object
- #device ⇒ Object
-
#initialize {|@config| ... } ⇒ Client
constructor
A new instance of Client.
- #plants ⇒ Object
-
#public_key ⇒ Object
Useful when you need to sign arbitrary secrets and give them to the server.
- #schedules ⇒ Object
- #sequences ⇒ Object
Constructor Details
Instance Attribute Details
#config ⇒ Object (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_url ⇒ Object
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 |
#device ⇒ Object
43 44 45 |
# File 'lib/client.rb', line 43 def device @device ||= FbResource::Device.new(config) end |
#plants ⇒ Object
55 56 57 |
# File 'lib/client.rb', line 55 def plants @plants ||= FbResource::Plants.new(config) end |
#public_key ⇒ Object
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 |
#schedules ⇒ Object
47 48 49 |
# File 'lib/client.rb', line 47 def schedules @schedules ||= FbResource::Schedules.new(config) end |
#sequences ⇒ Object
51 52 53 |
# File 'lib/client.rb', line 51 def sequences @sequences ||= FbResource::Sequences.new(config) end |