Class: CrystalSDK::Profile::Request

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id) ⇒ Request

Returns a new instance of Request.



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

def initialize(id)
  @id = id
end

Instance Attribute Details

#idObject (readonly)

Returns the value of attribute id.



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

def id
  @id
end

Class Method Details

.check_for_error(resp) ⇒ Object



26
27
28
29
30
# File 'lib/crystal_sdk/profile/request.rb', line 26

def check_for_error(resp)
  raise Profile::RateLimitHitError if resp.code == '429'
  raise Profile::NotAuthedError.new(Base.key) if resp.code == '401'
  raise Profile::NotFoundError if resp.code == '404'
end

.from_search(query) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
# File 'lib/crystal_sdk/profile/request.rb', line 14

def from_search(query)
  begin
    resp = Api.make_request(:post, 'profiles/async', params: query)
  rescue Nestful::ResponseError => e
    check_for_error(e.response)
    raise e
  end

  body = JSON.parse(resp.body || '{}', symbolize_names: true)
  Profile::Request.new(body[:request_id])
end

Instance Method Details

#did_find_profile?Boolean

Returns:

  • (Boolean)


65
66
67
68
69
70
71
72
# File 'lib/crystal_sdk/profile/request.rb', line 65

def did_find_profile?
  return false unless did_finish? && fetch_status == 'complete'

  info = fetch_request_info
  !info[:info][:error]
rescue Profile::NotFoundError
  false
end

#did_finish?Boolean

Returns:

  • (Boolean)


56
57
58
59
60
61
62
63
# File 'lib/crystal_sdk/profile/request.rb', line 56

def did_finish?
  status = fetch_status
  status == 'complete' || status == 'error'
rescue Profile::NotFoundError
  true
rescue Profile::NotAuthedError
  true
end

#fetch_request_infoObject



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/crystal_sdk/profile/request.rb', line 33

def fetch_request_info
  return @cached_data if @cached_data

  begin
    resp = Api.make_request(:get, "profiles/results/#{@id}")
  rescue Nestful::ResponseError => e
    Request.check_for_error(e.response)
    raise e
  end

  body = resp.body ? JSON.parse(resp.body, symbolize_names: true) : nil

  if body[:status] == 'complete' || body[:status] == 'error'
    @cached_data = body
  end

  body
end

#fetch_statusObject



52
53
54
# File 'lib/crystal_sdk/profile/request.rb', line 52

def fetch_status
  fetch_request_info[:status]
end

#profile_infoObject



74
75
76
77
78
79
80
81
82
# File 'lib/crystal_sdk/profile/request.rb', line 74

def profile_info
  return nil unless did_find_profile?

  req_info = fetch_request_info
  profile_info = RecursiveOpenStruct.new(req_info[:info])
  recommendations = RecursiveOpenStruct.new(req_info[:recommendations])

  { info: profile_info, recommendations: recommendations }
end