Class: Crowdskout::Services::ProfileService

Inherits:
BaseService
  • Object
show all
Defined in:
lib/crowdskout/services/profile_service.rb

Class Method Summary collapse

Class Method Details

.create_profile(profile) ⇒ Profile

more info - docs.crowdskout.com/api/#create-a-profile Create a new profile

Parameters:

  • profile (Profile)
    • the new profile object to add to Crowdskout

  • params (Hash)
    • A hash with query parameters

Returns:

  • (Profile)
    • the profile object

Raises:



36
37
38
39
40
41
42
43
44
45
# File 'lib/crowdskout/services/profile_service.rb', line 36

def create_profile(profile)
  raise Exceptions::ServiceException, "Profile object must not be nil." if profile.nil?
  url = Util::Config.get('endpoints.base_url') + Util::Config.get('endpoints.profile')
  url = build_url(url)
  payload = {
    profile: profile.to_hash
  }.to_json
  response = RestClient.post(url, payload, get_headers())
  Components::Profile.create(JSON.parse(response.body)["data"])
end

.create_profiles_bulk(profiles) ⇒ ResultSet

more info - docs.crowdskout.com/api/#create-many-profiles Create a bunch of new profiles

Parameters:

  • profiles (Array<Profile>)
    • an array of profile to bulk add

  • params (Hash)
    • A hash with query parameters

Returns:

  • (ResultSet)
    • the results set as an enumeration with profiles

Raises:



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/crowdskout/services/profile_service.rb', line 52

def create_profiles_bulk(profiles)
  raise Exceptions::ServiceException, "Must be an array of profiles." if profiles.nil? || !profiles.is_a?(Array)
  url = Util::Config.get('endpoints.base_url') + Util::Config.get('endpoints.profile_bulk')
  url = build_url(url)
  payload = {
    profiles: profiles.collect(&:to_hash)
  }.to_json
  response = RestClient.post(url, payload, get_headers())
  body = JSON.parse(response.body)

  profiles = []
  body['data'].each do |profile|
    profiles << Components::Profile.create(profile)
  end if body['data'].count > 0

  Components::ResultSet.new(profiles, body['messages'])
end

.get_profile(profile_id, collections) ⇒ Profile

more info - docs.crowdskout.com/api/#get-a-profile-by-id Get a profile based on the collections provided

Parameters:

  • profile_id (Integer)
    • the id of the profile

  • collections (String)
    • A csv of the requested collections

Returns:

  • (Profile)
    • the profile object

Raises:



17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/crowdskout/services/profile_service.rb', line 17

def get_profile(profile_id, collections)
  raise Exceptions::ServiceException, "Profile ID is required." if profile_id.nil?
  raise Exceptions::ServiceException, "A comma-deliminted list of collections is required." if collections.nil?
  params = {
    collections: collections
  }
  url = Util::Config.get('endpoints.base_url') +
        sprintf(Util::Config.get('endpoints.crud_profile'), profile_id)
  url = build_url(url, params)

  response = RestClient.get(url, get_headers())
  Components::Profile.create(JSON.parse(response.body)["data"])
end

.update_profile(profile) ⇒ Profile

more info - docs.crowdskout.com/api/#update-a-profile-by-id Update the given profile

Parameters:

  • profile (Profile)
    • the profile to update

Returns:

  • (Profile)
    • the profile object

Raises:



74
75
76
77
78
79
80
81
82
83
# File 'lib/crowdskout/services/profile_service.rb', line 74

def update_profile(profile)
  raise Exceptions::ServiceException, "Profile object must not be nil." if profile.nil?
  url = Util::Config.get('endpoints.base_url') + sprintf(Util::Config.get('endpoints.crud_profile'), profile.id)
  url = build_url(url)
  payload = {
    profile: profile.to_hash
  }.to_json
  response = RestClient.put(url, payload, get_headers())
  Components::Profile.create(JSON.parse(response.body)["data"])
end

.update_profiles_bulk(profiles) ⇒ ResultSet

more info - docs.crowdskout.com/api/#update-many-profiles Update a bunch of profiles

Parameters:

  • profiles (Array<Profile>)
    • an array of profile to bulk add

Returns:

  • (ResultSet)
    • the results set as an enumeration with profiles

Raises:



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/crowdskout/services/profile_service.rb', line 89

def update_profiles_bulk(profiles)
  raise Exceptions::ServiceException, "Must be an array of profiles." if profiles.nil? || !profiles.is_a?(Array)
  url = Util::Config.get('endpoints.base_url') + Util::Config.get('endpoints.profile_bulk')
  url = build_url(url)
  payload = {
    profiles: profiles.collect(&:to_hash)
  }.to_json
  response = RestClient.put(url, payload, get_headers())
  body = JSON.parse(response.body)

  profiles = []
  body['data'].each do |profile|
    profiles << Components::Profile.create(profile)
  end if body['data'].count > 0

  Components::ResultSet.new(profiles, body['messages'])
end