Class: Crowdskout::Services::AttributeService

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

Instance Attribute Summary

Attributes inherited from BaseService

#access_token, #api_key

Instance Method Summary collapse

Methods inherited from BaseService

#initialize

Constructor Details

This class inherits a constructor from Crowdskout::Services::BaseService

Instance Method Details

#create_attribute(new_attribute) ⇒ Attribute

Parameters:

  • attribute (Attribute)
    • attribute object to add to Crowdskout

Returns:

  • (Attribute)

Raises:



45
46
47
48
49
50
51
52
# File 'lib/crowdskout/services/attribute_service.rb', line 45

def create_attribute(new_attribute)
  raise Exceptions::ServiceException, "Attribute must not be nil" if new_attribute.nil?
  url = Util::Config.get('endpoints.base_url') + Util::Config.get('endpoints.attribute')
  url = build_url(url)
  payload = new_attribute.to_json
  response = RestClient.post(url, payload, get_headers())
  Components::Attribute.create(JSON.parse(response.body)["data"])
end

#delete_attribute(attribute_id) ⇒ boolean

Parameters:

  • attribute_id (Integer)
    • the id of the attribute to update

Returns:

  • (boolean)

Raises:



69
70
71
72
73
74
75
# File 'lib/crowdskout/services/attribute_service.rb', line 69

def delete_attribute(attribute_id)
  raise Exceptions::ServiceException, "Attribute ID is required." if attribute_id.nil?
  url = Util::Config.get('endpoints.base_url') + sprintf(Util::Config.get('endpoints.crud_attribute'), attribute_id)
  url = build_url(url)
  response = RestClient.delete(url, get_headers())
  response.code == 204
end

#get_attribute(attribute_id, params = {}) ⇒ Attribute

Parameters:

  • attribute_id (Integer)
    • the id of the attribute to fetch

  • params (Hash) (defaults to: {})
    • query parameters

Returns:

  • (Attribute)

Raises:



33
34
35
36
37
38
39
40
# File 'lib/crowdskout/services/attribute_service.rb', line 33

def get_attribute(attribute_id, params = {})
  raise Exceptions::ServiceException, "Attribute ID is required." if attribute_id.nil?
  url = Util::Config.get('endpoints.base_url') +
        sprintf(Util::Config.get('endpoints.crud_attribute'), attribute_id)
  url = build_url(url, params)
  response = RestClient.get(url, get_headers())
  Components::Attribute.create(JSON.parse(response.body)["data"])
end

#get_attributes(params = {}) ⇒ ResultSet

Parameters:

  • params (Hash) (defaults to: {})
    • query parameters

Returns:

  • (ResultSet)

    set of Components::Attributes



14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/crowdskout/services/attribute_service.rb', line 14

def get_attributes(params = {})
  url = Util::Config.get('endpoints.base_url') + Util::Config.get('endpoints.attributes')
  url = build_url(url, params)

  response = RestClient.get(url, get_headers())
  body = JSON.parse(response.body)

  attributes = []
  body['data']['list'].each do |attribute|
    attributes << Components::Attribute.create(attribute)
  end if body['data']["total"] > 0

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

#update_attribute(attribute_id, params = {}) ⇒ Attribute

Parameters:

  • attribute_id (Integer)
    • the id of the attribute to update

  • params (Hash) (defaults to: {})
    • query parameters

Returns:

  • (Attribute)

Raises:



58
59
60
61
62
63
64
# File 'lib/crowdskout/services/attribute_service.rb', line 58

def update_attribute(attribute_id, params = {})
  raise Exceptions::ServiceException, "Attribute ID is required." if attribute_id.nil?
  url = Util::Config.get('endpoints.base_url') + sprintf(Util::Config.get('endpoints.crud_attribute'), attribute_id)
  url = build_url(url)
  response = RestClient.put(url, params, get_headers())
  Components::Attribute.create(JSON.parse(response.body)["data"])
end