Sha256: f291917ca86b87ee7e788dd77c3522384b9103313c93a41c022708a2304eb4ec

Contents?: true

Size: 1.21 KB

Versions: 10

Compression:

Stored size: 1.21 KB

Contents

# frozen_string_literal: true

module ActionNetworkRest
  class Tags < Base
    attr_accessor :tag_id

    # 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
    def initialize(tag_id = nil, client:)
      super(client: client, tag_id: tag_id)
    end

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

    def base_path
      'tags/'
    end

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

    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 = 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

    private

    def osdi_key
      'osdi:tags'
    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
action_network_rest-0.12.0 lib/action_network_rest/tags.rb
action_network_rest-1.0.1 lib/action_network_rest/tags.rb
action_network_rest-1.0.0 lib/action_network_rest/tags.rb
action_network_rest-0.11.0 lib/action_network_rest/tags.rb
action_network_rest-0.10.0 lib/action_network_rest/tags.rb
action_network_rest-0.9.0 lib/action_network_rest/tags.rb
action_network_rest-0.8.2 lib/action_network_rest/tags.rb
action_network_rest-0.8.1 lib/action_network_rest/tags.rb
action_network_rest-0.8.0 lib/action_network_rest/tags.rb
action_network_rest-0.7.0 lib/action_network_rest/tags.rb