Class: Zaikio::Hub::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/zaikio/hub/client.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(token: nil, client_id: nil, client_secret: nil) ⇒ Client

Returns a new instance of Client.



42
43
44
45
46
# File 'lib/zaikio/hub/client.rb', line 42

def initialize(token: nil, client_id: nil, client_secret: nil)
  @token = token
  @client_id = client_id
  @client_secret = client_secret
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args, &block) ⇒ Object



122
123
124
125
126
# File 'lib/zaikio/hub/client.rb', line 122

def method_missing(method_name, *args, &block)
  result = with_auth { subject_class.new.public_send(method_name, *args, &block) }

  RequestWrapper.new(result, self)
end

Class Method Details

.from_credentials(client_id, client_secret) ⇒ Object



38
39
40
# File 'lib/zaikio/hub/client.rb', line 38

def self.from_credentials(client_id, client_secret)
  new(client_id: client_id, client_secret: client_secret)
end

.from_token(token) ⇒ Object



34
35
36
# File 'lib/zaikio/hub/client.rb', line 34

def self.from_token(token)
  new(token: token)
end

Instance Method Details

#connectionsObject



98
99
100
101
102
# File 'lib/zaikio/hub/client.rb', line 98

def connections
  raise "Only available with credentials" unless credentials?

  RequestWrapper.new(Zaikio::Hub::Connection.all, self)
end

#credentials?Boolean

Returns:

  • (Boolean)


66
67
68
# File 'lib/zaikio/hub/client.rb', line 66

def credentials?
  @token.nil?
end

#jwtObject



48
49
50
51
52
53
54
55
56
# File 'lib/zaikio/hub/client.rb', line 48

def jwt
  return if @token.nil?

  @jwt ||= begin
    payload = JWT.decode(@token, nil, false).first

    Zaikio::Hub.create_token_data(payload)
  end
end

#organizationObject



86
87
88
89
90
# File 'lib/zaikio/hub/client.rb', line 86

def organization
  raise "Current organization is not available for person" unless organization?

  with_auth { Zaikio::Hub::CurrentOrganization.find }
end

#organization?Boolean

Returns:

  • (Boolean)


58
59
60
# File 'lib/zaikio/hub/client.rb', line 58

def organization?
  jwt&.subject_type == "Organization"
end

#personObject



92
93
94
95
96
# File 'lib/zaikio/hub/client.rb', line 92

def person
  raise "Current person is not available for organization" unless person?

  with_auth { Zaikio::Hub::CurrentPerson.find }
end

#person?Boolean

Returns:

  • (Boolean)


62
63
64
# File 'lib/zaikio/hub/client.rb', line 62

def person?
  jwt&.subject_type == "Person"
end

#respond_to_missing?(method_name, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


116
117
118
119
120
# File 'lib/zaikio/hub/client.rb', line 116

def respond_to_missing?(method_name, include_private = false)
  return super if credentials?

  subject_class.new.respond_to?(method_name, include_private) || super
end

#subject_classObject



70
71
72
73
74
75
76
# File 'lib/zaikio/hub/client.rb', line 70

def subject_class
  if organization?
    Zaikio::Hub::CurrentOrganization
  elsif person?
    Zaikio::Hub::CurrentPerson
  end
end

#subscriptionsObject



104
105
106
107
108
# File 'lib/zaikio/hub/client.rb', line 104

def subscriptions
  raise "Only available with credentials" unless credentials?

  RequestWrapper.new(Zaikio::Hub::Subscription.all, self)
end

#test_accountsObject



110
111
112
113
114
# File 'lib/zaikio/hub/client.rb', line 110

def test_accounts
  raise "Only available with credentials" unless credentials?

  RequestWrapper.new(Zaikio::Hub::TestAccount.all, self)
end

#with_auth(&block) ⇒ Object



78
79
80
81
82
83
84
# File 'lib/zaikio/hub/client.rb', line 78

def with_auth(&block)
  if @token.nil?
    Zaikio::Hub.with_basic_auth(@client_id, @client_secret, &block)
  else
    Zaikio::Hub.with_token(@token, &block)
  end
end