Class: KlaviyoAPI::Profile

Inherits:
Base
  • Object
show all
Extended by:
Support::Countable
Defined in:
lib/klaviyo_api/resources/profile.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Support::Countable

count

Methods inherited from Base

activate_session, headers, reset_session, #to_h

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_symbol, *arguments) ⇒ Object (private)



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/klaviyo_api/resources/profile.rb', line 48

def method_missing(method_symbol, *arguments)
  method_name = method_symbol.to_s

  # Some fields start with a $ (e.g., '$title'). The methods `title`, `title?`
  # and `title=` should act upon the attribute `$title` if present. Otherwise,
  # they should act upon the attribute `title`
  #
  # Not a whole lot we can do if the item contains both `title` and `$title` -
  # this will prioritize the version with the $.
  if attributes.keys.any? { |attribute| attribute == "$#{method_name.sub(/\?|=$/, '')}" }
    method_symbol = "$#{method_name}".to_sym
  end

  super
end

Class Method Details

.collection_path(prefix_options = {}, query_options = {}) ⇒ Object



13
14
15
# File 'lib/klaviyo_api/resources/profile.rb', line 13

def collection_path(prefix_options = {}, query_options = {})
  super prefix_options, query_options.deep_merge({ api_key: headers['api-key'] })
end

.element_path(id, prefix_options = {}, query_options = {}) ⇒ Object



17
18
19
# File 'lib/klaviyo_api/resources/profile.rb', line 17

def element_path(id, prefix_options = {}, query_options = {})
  super id, prefix_options, query_options.deep_merge({ api_key: headers['api-key'] })
end

Instance Method Details

#createObject



26
27
28
# File 'lib/klaviyo_api/resources/profile.rb', line 26

def create
  raise KlaviyoAPI::InvalidOperation, 'Cannot create Profiles via API.'
end

#destroyObject



22
23
24
# File 'lib/klaviyo_api/resources/profile.rb', line 22

def destroy
  raise KlaviyoAPI::InvalidOperation, 'Cannot delete Profiles via API.'
end

#updateObject



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/klaviyo_api/resources/profile.rb', line 30

def update
  run_callbacks :update do
    # This endpoint does not accept JSON bodies. Additionally, if the API key is in the URL, the rest of the attributes
    # MUST be in the URL as well. The other option is to have the API key and all attributes in the body as `key=value` pairs.
    #
    # To reduce friction, as we already need the API key in the URL for other operations, let's just add all the attributes
    # to the URL. Unfortunately we will need to include ALL the attributes, not just the changed ones, because it seems
    # ActiveResource has no support for dirty models. https://github.com/rails/activeresource/issues/308
    path = self.class.element_path(id, prefix_options, attributes)

    connection.put(path, nil, self.class.headers).tap do |response|
      load_attributes_from_response(response)
    end
  end
end