Class: CrystalSDK::Profile

Inherits:
Object
  • Object
show all
Defined in:
lib/crystal_sdk/profile.rb,
lib/crystal_sdk/profile/errors.rb,
lib/crystal_sdk/profile/request.rb

Defined Under Namespace

Classes: NotAuthedError, NotFoundError, NotFoundYetError, RateLimitHitError, Request, UnexpectedError

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(info, recommendations) ⇒ Profile

Returns a new instance of Profile.



9
10
11
12
# File 'lib/crystal_sdk/profile.rb', line 9

def initialize(info, recommendations)
  @info = info
  @recommendations = recommendations
end

Instance Attribute Details

#infoObject (readonly)

Returns the value of attribute info.



7
8
9
# File 'lib/crystal_sdk/profile.rb', line 7

def info
  @info
end

#recommendationsObject (readonly)

Returns the value of attribute recommendations.



7
8
9
# File 'lib/crystal_sdk/profile.rb', line 7

def recommendations
  @recommendations
end

Class Method Details

.from_request(req) ⇒ Object



15
16
17
18
19
20
# File 'lib/crystal_sdk/profile.rb', line 15

def from_request(req)
  return nil unless req.did_find_profile?

  profile_info = req.profile_info
  Profile.new(profile_info[:info], profile_info[:recommendations])
end

.search(query, timeout: 30) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/crystal_sdk/profile.rb', line 22

def search(query, timeout: 30)
  request = nil
  profile = nil

  Timeout.timeout(timeout) do
    request = Profile::Request.from_search(query)

    loop do
      sleep(2) && next unless request.did_finish?
      raise NotFoundError unless request.did_find_profile?

      profile = Profile.from_request(request)
      break
    end
  end

  profile
rescue Timeout::Error
  raise NotFoundYetError.new(request)
end