Class: ActiveCampaign

Inherits:
Object
  • Object
show all
Includes:
Client
Defined in:
lib/active-campaign-rails.rb,
lib/active-campaign-rails/client.rb

Defined Under Namespace

Modules: Client

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Client

#action_calls

Constructor Details

#initialize(args) ⇒ ActiveCampaign

Returns a new instance of ActiveCampaign.



12
13
14
15
16
17
18
19
20
21
22
# File 'lib/active-campaign-rails.rb', line 12

def initialize(args)

  # Parse args into instance_variable
  args.each do |k,v|
    instance_variable_set("@#{k}", v) unless v.nil?
  end

  # Set default api_output to json if not set
  @api_output = 'json' if @api_output == nil

end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(api_action, *args, &block) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/active-campaign-rails.rb', line 25

def method_missing(api_action, *args, &block)

  # Generate api_url
  api_url = generate_api_url(api_action)

  # Check method for api_action given
  case action_calls[api_action][:method]
  when 'get'

    # Generate API parameter from given argument
    api_params = (args.any?) ? args.first.map{|k,v| "#{k}=#{v}"}.join('&') : nil

    # Join API url and API parameters
    api_url = api_params ? "#{api_url}&#{api_params}" : api_url

    # Make a call to API server with GET method
    response = RestClient.get(api_url)

    # Return response from API server
    # Default to JSON
    return response.body

  when 'post'

    # API parameters for POST method
    api_params = args.first

    # For event tracking the visit param must have a json value
    if visit = api_params[:visit]
      api_params[:visit] = visit.to_json if visit.is_a?(Hash)
    end

    # Make a call to API server with POST method
    response = RestClient.post(api_url, api_params)

    # Return response from API server
    # Default to JSON
    return response.body

  when 'delete'

    # API parameters for DELETE method
    api_params = args.first.merge(api_key: @api_key, api_output: @api_output)

    api_url = "#{action_calls[api_action][:endpoint] || @api_endpoint}#{action_calls[api_action][:path] || '/admin/api.php'}"

    # Make a call to API server with DELETE method
    response = RestClient::Request.execute(method: :delete, url: api_url, headers: { params: api_params })

    # Return response from API server
    # Default to JSON
    return response.body

  end

end

Instance Attribute Details

#api_endpointObject (readonly)

Returns the value of attribute api_endpoint.



10
11
12
# File 'lib/active-campaign-rails.rb', line 10

def api_endpoint
  @api_endpoint
end

#api_keyObject (readonly)

Returns the value of attribute api_key.



10
11
12
# File 'lib/active-campaign-rails.rb', line 10

def api_key
  @api_key
end