Class: Bsm::Sso::Client::AbstractResource

Inherits:
Hash
  • Object
show all
Defined in:
lib/bsm/sso/client/abstract_resource.rb

Direct Known Subclasses

User

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = nil) ⇒ AbstractResource

Constuctor

Parameters:

  • attributes (Hash, NilClass) (defaults to: nil)

    the attributes to assign



57
58
59
60
# File 'lib/bsm/sso/client/abstract_resource.rb', line 57

def initialize(attributes=nil)
  super()
  update(attributes.stringify_keys) if attributes.is_a?(Hash)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *arguments) ⇒ Object (protected)



79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/bsm/sso/client/abstract_resource.rb', line 79

def method_missing(method, *arguments)
  method = method.to_s.sub(/([=?])$/, '')
  punctation = Regexp.last_match(1)

  case punctation
  when '='
    store(method, arguments.first)
  when '?'
    self[method]
  else
    key?(method) ? fetch(method) : super
  end
end

Class Method Details

.get(path, params = {}) ⇒ Bsm::Sso::Client::AbstractResource

Returns fetches object from remote.

Parameters:

  • path (String)
  • params, (Hash)

    request params - see Excon::Connection#request

Returns:



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/bsm/sso/client/abstract_resource.rb', line 34

def get(path, params={})
  params[:query] ||= params.delete(:params)
  collection       = params.delete(:collection)
  params           = params.merge(path: path)
  params[:headers] = (params[:headers] || {}).merge(headers)

  response = site.get(params)
  return (collection ? [] : nil) unless response.status == 200

  result = ActiveSupport::JSON.decode(response.body)
  result = Array.wrap(result).map do |record|
    instance = new(record)
    instance if instance.id
  end.compact
  collection ? result : result.first
rescue ActiveSupport::JSON.parse_error
  collection ? [] : nil
end

.headersHash

Returns default headers.

Returns:

  • (Hash)

    default headers



27
28
29
# File 'lib/bsm/sso/client/abstract_resource.rb', line 27

def headers
  { 'Authorization' => Bsm::Sso::Client.verifier.generate(Bsm::Sso::Client.token_timeout.from_now) }
end

.siteExcon::Connection

Returns site connection.

Returns:

  • (Excon::Connection)

    site connection



20
21
22
23
24
# File 'lib/bsm/sso/client/abstract_resource.rb', line 20

def site
  @site ||
    (superclass.respond_to?(:site) && superclass.site) ||
    raise("No site specified for #{name}. Please specify #{name}.site = 'http://your.sso.host'")
end

.site=(url) ⇒ Object

Parameters:

  • url (String)


11
12
13
14
15
16
17
# File 'lib/bsm/sso/client/abstract_resource.rb', line 11

def site=(url)
  @site = Excon.new url,
    mock: defined?(WebMock),
    idempotent: true,
    expects: [200, 422],
    headers: { 'Accept' => Mime[:json].to_s, 'Content-Type' => Mime[:json].to_s }
end

Instance Method Details

#attributesHash

Returns attributes hash.

Returns:

  • (Hash)

    attributes hash



73
74
75
# File 'lib/bsm/sso/client/abstract_resource.rb', line 73

def attributes
  dup
end

#idInteger

Returns ID, the primary key.

Returns:

  • (Integer)

    ID, the primary key



63
64
65
# File 'lib/bsm/sso/client/abstract_resource.rb', line 63

def id
  self['id']
end

#respond_to_missing?(method) ⇒ Boolean

Returns true, if method exists?.

Returns:

  • (Boolean)

    true, if method exists?



68
69
70
# File 'lib/bsm/sso/client/abstract_resource.rb', line 68

def respond_to_missing?(method, *)
  super || key?(method.to_s.sub(/[=?]$/, ''))
end