Class: Wework::Api::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/wework/api/base.rb

Direct Known Subclasses

Agent, Contact

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(corp_id, agent_id, agent_secret, options = {}) ⇒ Base

Returns a new instance of Base.



12
13
14
15
16
17
# File 'lib/wework/api/base.rb', line 12

def initialize(corp_id, agent_id, agent_secret, options={})
  @corp_id      = corp_id
  @agent_id     = agent_id
  @agent_secret = agent_secret
  @options      = options
end

Instance Attribute Details

#agent_idObject (readonly)

Returns the value of attribute agent_id.



7
8
9
# File 'lib/wework/api/base.rb', line 7

def agent_id
  @agent_id
end

#agent_secretObject (readonly)

Returns the value of attribute agent_secret.



7
8
9
# File 'lib/wework/api/base.rb', line 7

def agent_secret
  @agent_secret
end

#corp_idObject (readonly)

Returns the value of attribute corp_id.



7
8
9
# File 'lib/wework/api/base.rb', line 7

def corp_id
  @corp_id
end

#optionsObject

Returns the value of attribute options.



8
9
10
# File 'lib/wework/api/base.rb', line 8

def options
  @options
end

Instance Method Details

#get(path, headers = {}) ⇒ Object



27
28
29
30
31
# File 'lib/wework/api/base.rb', line 27

def get(path, headers = {})
  with_access_token(headers[:params]) do |params|
    request.get path, headers.merge(params: params)
  end
end

#post(path, payload, headers = {}) ⇒ Object



33
34
35
36
37
# File 'lib/wework/api/base.rb', line 33

def post(path, payload, headers = {})
  with_access_token(headers[:params]) do |params|
    request.post path, payload, headers.merge(params: params)
  end
end

#post_file(path, file, headers = {}) ⇒ Object



39
40
41
42
43
# File 'lib/wework/api/base.rb', line 39

def post_file(path, file, headers = {})
  with_access_token(headers[:params]) do |params|
    request.post_file path, file, headers.merge(params: params)
  end
end

#requestObject



23
24
25
# File 'lib/wework/api/base.rb', line 23

def request
  @request ||= Wework::Request.new(API_ENDPOINT, false)
end

#token_storeObject



19
20
21
# File 'lib/wework/api/base.rb', line 19

def token_store
  @token_store ||= Token::RedisStore.new self
end

#with_access_token(params = {}, tries = 2) ⇒ Object



45
46
47
48
49
50
51
# File 'lib/wework/api/base.rb', line 45

def with_access_token(params = {}, tries = 2)
  params ||= {}
  yield(params.merge(access_token: access_token))
rescue AccessTokenExpiredError
  token_store.refresh_token
  retry unless (tries -= 1).zero?
end