Module: Resources

Includes:
BaseClient
Included in:
Spartacus
Defined in:
lib/client/resources.rb

Instance Method Summary collapse

Methods included from BaseClient

#auth_header, #convert_keys, #convert_response, #handle_timeouts, #success?, #whitelist_params

Instance Method Details

#add_tag_to_resource(id, tag) ⇒ Array

Add a tag to a resource

Examples:

Add a tag to a resource

Spartacus#add_tag_to_resource(129, 'android')

Parameters:

  • id (Integer)

    A resource id.

  • id (String)

    The tag to add.

Returns:

  • (Array)

    The resource's tags.


72
73
74
75
76
77
78
79
80
81
# File 'lib/client/resources.rb', line 72

def add_tag_to_resource(id, tag)
  tag.gsub!(" ", "_")
  url = "#{@api_base_path}/resources/#{id}/tags"

  handle_timeouts do
    response = self.class.post(url, headers: auth_header,
                              body: { add: tag })
    JSON.parse(response.body)['new_list']
  end
end

#create_resource(name = nil, content = nil, is_public = false) ⇒ Resource

Create a resource

Examples:

Create a checkpoint

Spartacus#create_resource('Real Cool Resource', 'My content')

Parameters:

  • options (Hash)

    A customizable set of options.

  • name (String) (defaults to: nil)

    Resource name.

  • content (String) (defaults to: nil)

    Resource content.

  • is_public (Boolean) (defaults to: false)

    Resource privacy.

Returns:

  • (Resource)

    The created resource


15
16
17
18
19
20
21
22
23
24
# File 'lib/client/resources.rb', line 15

def create_resource(name=nil, content=nil, is_public=false)
  url = "#{@api_base_path}/resources/"

  handle_timeouts do
    response = self.class.post(url, headers: auth_header,
                              body: { resource: {
                                name: name, content: content, is_public: is_public } })
    convert_response(response, "resource")
  end
end

#remove_tag_from_resource(id, tag) ⇒ Array

Remove a tag from a resource

Examples:

Remove a tag from a resource

Spartacus#remove_tag_from_resource(129, 'android')

Parameters:

  • id (Integer)

    A resource id.

  • id (String)

    The tag to remove.

Returns:

  • (Array)

    The resource's tags.


90
91
92
93
94
95
96
97
98
99
# File 'lib/client/resources.rb', line 90

def remove_tag_from_resource(id, tag)
  tag.gsub!(" ", "_")
  url = "#{@api_base_path}/resources/#{id}/tags"

  handle_timeouts do
    response = self.class.post(url, headers: auth_header,
                              body: { remove: tag })
    JSON.parse(response.body)['new_list']
  end
end

#tags_for_resource(id) ⇒ Array

Return tags for a resource

Examples:

Remove a tag from a resource

Spartacus#tags_for_resource(129)

Parameters:

  • id (Integer)

    A resource id.

Returns:

  • (Array)

    The resource's tags.


56
57
58
59
60
61
62
63
# File 'lib/client/resources.rb', line 56

def tags_for_resource(id)
  url = "#{@api_base_path}/resources/#{id}/tags"

  handle_timeouts do
    response = self.class.post(url, headers: auth_header)
    JSON.parse(response.body)['new_list']
  end
end

#update_resource(id, options = {}) ⇒ Resource

Update a resource

Examples:

Update a checkpoint

Spartacus#update_resource(129, {name: 'Real Cool Resource'})

Parameters:

  • id (Integer)

    A resource id.

  • options (Hash) (defaults to: {})

    A customizable set of options.

Options Hash (options):

  • :name (String)

    Resource name.

  • :content (String)

    Resource content.

  • :is_public (Boolean)

    Resource privacy.

Returns:

  • (Resource)

    The updated resource


36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/client/resources.rb', line 36

def update_resource(id, options={})
  whitelist = ['name', 'content', 'is_public']

  options = convert_keys(options)
  resource_params = whitelist_params(options, whitelist)
  url = "#{@api_base_path}/resources/#{id}"

  handle_timeouts do
    response = self.class.put(url, headers: auth_header,
                              body: { resource: resource_params })
    convert_response(response, "resource")
  end
end