Class: GrowViral::Keystore::AccountFetcher

Inherits:
Object
  • Object
show all
Defined in:
lib/keystore/account_fetcher.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(application_id, uid, deps) ⇒ AccountFetcher

Returns a new instance of AccountFetcher.

Raises:



13
14
15
16
17
18
19
# File 'lib/keystore/account_fetcher.rb', line 13

def initialize(application_id, uid, deps)
  raise HandleNotUidError unless uid.is_a? Numeric

  @application_id = application_id
  @uid = uid
  @config = deps[:config]
end

Instance Attribute Details

#application_idObject (readonly)

Returns the value of attribute application_id.



12
13
14
# File 'lib/keystore/account_fetcher.rb', line 12

def application_id
  @application_id
end

#configObject (readonly)

Returns the value of attribute config.



12
13
14
# File 'lib/keystore/account_fetcher.rb', line 12

def config
  @config
end

#uidObject (readonly)

Returns the value of attribute uid.



12
13
14
# File 'lib/keystore/account_fetcher.rb', line 12

def uid
  @uid
end

Class Method Details

.exists?(*args) ⇒ Boolean

Returns:

  • (Boolean)


8
9
10
# File 'lib/keystore/account_fetcher.rb', line 8

def self.exists?(*args)
  new(*args).exists?
end

.fetch(*args) ⇒ Object



4
5
6
# File 'lib/keystore/account_fetcher.rb', line 4

def self.fetch(*args)
  new(*args).fetch
end

Instance Method Details

#exists?Boolean

Returns:

  • (Boolean)


21
22
23
24
25
# File 'lib/keystore/account_fetcher.rb', line 21

def exists?
  fetch
rescue GrowViral::NoAccountError
  false
end

#fetchObject



27
28
29
30
31
32
33
34
# File 'lib/keystore/account_fetcher.rb', line 27

def fetch
  response = Net::HTTP.get_response(uri)
  if response.code.to_i == 200
    Account.new JSON.parse(response.body)
  else
    raise GrowViral::NoAccountError.new(uid)
  end
end

#uriObject



36
37
38
# File 'lib/keystore/account_fetcher.rb', line 36

def uri
  @uri ||= URI.parse("#{config.host}/applications/#{application_id}/accounts/#{uid}")
end