Class: ActionNetworkRest::Tags

Inherits:
Base
  • Object
show all
Defined in:
lib/action_network_rest/tags.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#get, #list

Constructor Details

#initialize(tag_id = nil, client:) ⇒ Tags

Without a tag_id, this class is used for the Tag creation endpoint. With a tag_id, this class is used to initialise the Taggings class, like client.tags(123).taggings



8
9
10
# File 'lib/action_network_rest/tags.rb', line 8

def initialize(tag_id=nil, client:)
  super(client: client, tag_id: tag_id)
end

Instance Attribute Details

#tag_idObject

Returns the value of attribute tag_id.



3
4
5
# File 'lib/action_network_rest/tags.rb', line 3

def tag_id
  @tag_id
end

Instance Method Details

#base_pathObject



16
17
18
# File 'lib/action_network_rest/tags.rb', line 16

def base_path
  'tags/'
end

#create(name) ⇒ Object



20
21
22
23
24
# File 'lib/action_network_rest/tags.rb', line 20

def create(name)
  post_body = {name: name}
  response = client.post_request base_path, post_body
  object_from_response(response)
end

#find_by_name(name) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/action_network_rest/tags.rb', line 26

def find_by_name(name)
  # Action Network API doesn't support currently OData querying for tags
  # (https://actionnetwork.org/docs/v2#odata) so we need to retrieve a list of
  # all tags and iterate to find the one we're looking for.
  page = 1
  loop do
    tags = self.list(page: page)
    return nil if tags.empty?

    found_tag = tags.find { |t| t.name == name }
    return found_tag unless found_tag.nil?

    page += 1
  end
end

#taggingsObject



12
13
14
# File 'lib/action_network_rest/tags.rb', line 12

def taggings
  @_taggings ||= ActionNetworkRest::Taggings.new(client: client, tag_id: tag_id)
end