Class: Crowdskout::Services::AttributeService

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

Class Method Summary collapse

Class Method Details

.create_attribute(new_attribute) ⇒ Attribute

Parameters:

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

Returns:

  • (Attribute)

Raises:



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

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:



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

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:



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

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



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

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:



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

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