Class: UnipassApi::Client

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}, &block) ⇒ Client

Returns a new instance of Client.



7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/unipass_api/client.rb', line 7

def initialize(options = {}, &block)
  @client_id     = options[:client_id]     || UnipassApi.options[:client_id]
  @client_secret = options[:client_secret] || UnipassApi.options[:client_secret]
  @site          = options[:site]          || UnipassApi.options[:site]
  @api_site      = options[:api_site]      || UnipassApi.options[:api_site]
  @authorize_url = options[:authorize_url] || UnipassApi.options[:authorize_url]
  @token_url     = options[:token_url]     || UnipassApi.options[:token_url]

  self.access_token  = options[:access_token]
  self.refresh_token = options[:refresh_token]
  self.expires_at    = options[:expires_at]
end

Instance Attribute Details

#access_tokenObject

Returns the value of attribute access_token.



5
6
7
# File 'lib/unipass_api/client.rb', line 5

def access_token
  @access_token
end

#expires_atObject

Returns the value of attribute expires_at.



5
6
7
# File 'lib/unipass_api/client.rb', line 5

def expires_at
  @expires_at
end

#refresh_tokenObject

Returns the value of attribute refresh_token.



5
6
7
# File 'lib/unipass_api/client.rb', line 5

def refresh_token
  @refresh_token
end

Instance Method Details

#clientObject



50
51
52
53
54
55
56
# File 'lib/unipass_api/client.rb', line 50

def client
  @client ||= ::OAuth2::Client.new(@client_id, @client_secret, {
      :site          => @api_site,
      :authorize_url => @authorize_url,
      :token_url     => @token_url
  })
end

#expired?Boolean

Returns:

  • (Boolean)


20
21
22
# File 'lib/unipass_api/client.rb', line 20

def expired?
  token.expired?
end

#get(url, options = {}) ⇒ Object



42
43
44
# File 'lib/unipass_api/client.rb', line 42

def get(url, options = {})
  request(:get, url, options)
end

#post(url, options = {}) ⇒ Object



46
47
48
# File 'lib/unipass_api/client.rb', line 46

def post(url, options = {})
  request(:post, url, options)
end

#refresh!Object



24
25
26
27
28
29
30
# File 'lib/unipass_api/client.rb', line 24

def refresh!
  new_token = token.refresh!
  self.access_token  = new_token.token
  self.refresh_token = new_token.refresh_token
  self.expires_at    = new_token.expires_at
  new_token
end

#request(verb, url, options = {}) ⇒ Object



32
33
34
35
36
37
38
39
40
# File 'lib/unipass_api/client.rb', line 32

def request(verb, url, options = {})
  token.request(verb, url, options).parsed
rescue OAuth2::Error => error
  case error.response.status
    when 404 then raise ResourceNotFound.new(error.response)
    when 422 then raise ResourceInvalid.new(error.response)
    else raise
  end
end