Class: M2X::Client::Key

Inherits:
Resource show all
Defined in:
lib/m2x/key.rb

Overview

Wrapper for M2X Keys API

Constant Summary collapse

PATH =
"/keys"

Instance Attribute Summary

Attributes inherited from Resource

#attributes

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Resource

#delete!, #initialize, #inspect, #refresh, #update!, #view

Constructor Details

This class inherits a constructor from M2X::Client::Resource

Class Method Details

.create!(client, params = {}) ⇒ Key

Method for Create Key endpoint. Note that, according to the parameters sent, you can create a Master API Key or a Device/Stream API Key.

Parameters:

  • client (Client)

    Client API

  • params (defaults to: {})

    Query parameters passed as keyword arguments. View M2X API Docs for listing of available parameters.

Returns:

  • (Key)

    The newly created Key.



31
32
33
34
35
# File 'lib/m2x/key.rb', line 31

def create!(client, params={})
  res = client.post(PATH, nil, params, "Content-Type" => "application/json")

  new(client, res.json) if res.success?
end

.list(client, params = {}) ⇒ Array

Method for List Keys endpoint.

Parameters:

  • client (Client)

    Client API

  • params (defaults to: {})

    Query parameters passed as keyword arguments. View M2X API Docs for listing of available parameters.

Returns:



16
17
18
19
20
# File 'lib/m2x/key.rb', line 16

def list(client, params={})
  res = client.get(PATH, params)

  res.json["keys"].map{ |atts| new(client, atts) } if res.success?
end

Instance Method Details

#pathObject



38
39
40
# File 'lib/m2x/key.rb', line 38

def path
  @path ||= "#{ PATH }/#{ URI.encode(@attributes.fetch("key")) }"
end

#regenerateKey

Method for Regenerate Key endpoint. Note that if you regenerate the key that you’re using for authentication then you would need to change your scripts to start using the new key token for all subsequent requests.

Returns:

  • (Key)

    The regenerated key.



50
51
52
53
54
55
56
57
# File 'lib/m2x/key.rb', line 50

def regenerate
  res = @client.post("#{path}/regenerate", nil, {})

  if res.success?
    @path = nil
    @attributes = res.json
  end
end