Class: MOCO::Session

Inherits:
Object
  • Object
show all
Defined in:
lib/moco/entities/session.rb

Overview

Represents a MOCO API session for authentication.

The ‘/session` endpoint exchanges email/password credentials for an API key, and can verify an existing key.

Creating an API key (POST /session):

result = MOCO::Session.create(
  subdomain: "your-account",
  email: "you@example.com",
  password: "secret"
)
result["api_key"] # => "6f95f9a0..."
result["user_id"] # => 933590696

Verifying an existing key (GET /session):

identity = moco.session.verify
identity["id"]   # => 933590696
identity["uuid"] # => "7a60719d-..."

Note:

`create` does not require an existing Client - it uses a temporary
unauthenticated connection. `verify` uses the Client's configured
API key.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client) ⇒ Session

Returns a new instance of Session.



49
50
51
# File 'lib/moco/entities/session.rb', line 49

def initialize(client)
  @client = client
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



47
48
49
# File 'lib/moco/entities/session.rb', line 47

def client
  @client
end

Class Method Details

.create(subdomain:, email:, password:) ⇒ Object

Exchange email/password for an API key. Does not require a Client. Returns a Hash: { “api_key” => “…”, “user_id” => … }

Raises:



35
36
37
38
39
40
41
42
43
44
# File 'lib/moco/entities/session.rb', line 35

def create(subdomain:, email:, password:)
  conn = Faraday.new(url: "https://#{subdomain}.mocoapp.com/api/v1") do |f|
    f.request :json
    f.response :json
  end
  response = conn.post("session", { email:, password: })
  raise MOCO::Error, "Authentication failed: #{response.status}" unless response.success?

  response.body
end

Instance Method Details

#verifyObject

Verify the configured API key. Returns the identity Hash or raises on 401.



54
55
56
# File 'lib/moco/entities/session.rb', line 54

def verify
  client.get("session")
end