Class: Mautic::Proxy

Inherits:
Object
  • Object
show all
Defined in:
lib/mautic/proxy.rb

Instance Method Summary collapse

Constructor Details

#initialize(connection, endpoint, options = nil) ⇒ Proxy

Returns a new instance of Proxy.



4
5
6
7
8
9
10
# File 'lib/mautic/proxy.rb', line 4

def initialize(connection, endpoint, options = nil)
  @options = options || {}
  @connection = connection
  klass = @options.delete(:klass) || "Mautic::#{endpoint.classify}"
  @target = klass.safe_constantize || Mautic.const_set(endpoint.classify, Class.new(Mautic::Model))
  @endpoint = endpoint
end

Instance Method Details

#all(options = {}, &block) ⇒ Object



24
25
26
27
28
29
30
31
32
33
# File 'lib/mautic/proxy.rb', line 24

def all(options = {}, &block)
  if options[:limit] == 'all'
    options.delete(:limit)
    limit_all(options, &block)
  else
    results = where(options)
    results.each { |i| yield i } if block_given?
    results
  end
end

#build_instance(data) ⇒ Object



20
21
22
# File 'lib/mautic/proxy.rb', line 20

def build_instance(data)
  @target.new(@connection, data)
end

#countObject



57
58
59
60
61
62
# File 'lib/mautic/proxy.rb', line 57

def count
  return @count if defined? @count

  json = @connection.request(:get, "api/#{@endpoint}", { limit: 1 })
  @count = json["total"].to_i
end

#data_nameObject



16
17
18
# File 'lib/mautic/proxy.rb', line 16

def data_name
  @endpoint.split("/").last
end

#find(id) ⇒ Object



51
52
53
54
55
# File 'lib/mautic/proxy.rb', line 51

def find(id)
  json = @connection.request(:get, "api/#{@endpoint}/#{id}")
  @last_response = json
  build_instance json[data_name.singularize]
end

#firstObject



47
48
49
# File 'lib/mautic/proxy.rb', line 47

def first
  where(limit: 1).first
end

#new(attributes = {}) ⇒ Object



12
13
14
# File 'lib/mautic/proxy.rb', line 12

def new(attributes = {})
  build_instance attributes
end

#where(params = {}) ⇒ Object

Parameters:

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

See Also:



37
38
39
40
41
42
43
44
45
# File 'lib/mautic/proxy.rb', line 37

def where(params = {})
  q = params.reverse_merge(@options[:default_params] || {})
  json = @connection.request(:get, "api/#{@endpoint}", params: q)
  @count = json["total"].to_i
  @last_response = json
  json[data_name].collect do |id, attributes|
    build_instance attributes || id
  end
end