Class: XeroKiwi::Connection

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

Overview

Represents a single Xero “Connection” — i.e. a tenant (organisation or practice) that an access token has been authorised against.

See: developer.xero.com/documentation/guides/oauth2/auth-flow#connections

Constant Summary collapse

ATTRIBUTES =
{
  id:               "id",
  auth_event_id:    "authEventId",
  tenant_id:        "tenantId",
  tenant_type:      "tenantType",
  tenant_name:      "tenantName",
  created_date_utc: "createdDateUtc",
  updated_date_utc: "updatedDateUtc"
}.freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attrs) ⇒ Connection

Returns a new instance of Connection.



30
31
32
33
34
35
36
37
38
39
# File 'lib/xero_kiwi/connection.rb', line 30

def initialize(attrs)
  attrs             = attrs.transform_keys(&:to_s)
  @id               = attrs["id"]
  @auth_event_id    = attrs["authEventId"]
  @tenant_id        = attrs["tenantId"]
  @tenant_type      = attrs["tenantType"]
  @tenant_name      = attrs["tenantName"]
  @created_date_utc = parse_time(attrs["createdDateUtc"])
  @updated_date_utc = parse_time(attrs["updatedDateUtc"])
end

Class Method Details

.from_response(payload) ⇒ Object



23
24
25
26
27
28
# File 'lib/xero_kiwi/connection.rb', line 23

def self.from_response(payload)
  return [] if payload.nil?

  items = payload.is_a?(Array) ? payload : [payload]
  items.map { |attrs| new(attrs) }
end

Instance Method Details

#==(other) ⇒ Object Also known as: eql?



48
49
50
# File 'lib/xero_kiwi/connection.rb', line 48

def ==(other)
  other.is_a?(Connection) && other.id == id
end

#hashObject



53
# File 'lib/xero_kiwi/connection.rb', line 53

def hash = [self.class, id].hash

#inspectObject



55
56
57
58
# File 'lib/xero_kiwi/connection.rb', line 55

def inspect
  "#<#{self.class} id=#{id.inspect} tenant_id=#{tenant_id.inspect} " \
    "tenant_name=#{tenant_name.inspect} tenant_type=#{tenant_type.inspect}>"
end

#organisation?Boolean

Returns:

  • (Boolean)


41
# File 'lib/xero_kiwi/connection.rb', line 41

def organisation? = tenant_type == "ORGANISATION"

#practice?Boolean

Returns:

  • (Boolean)


42
# File 'lib/xero_kiwi/connection.rb', line 42

def practice?     = tenant_type == "PRACTICE"

#to_hObject



44
45
46
# File 'lib/xero_kiwi/connection.rb', line 44

def to_h
  ATTRIBUTES.keys.to_h { |key| [key, public_send(key)] }
end