Class: Mautic::Proxy
- Inherits:
-
Object
- Object
- Mautic::Proxy
- Defined in:
- lib/mautic/proxy.rb
Instance Method Summary collapse
- #all(options = {}, &block) ⇒ Object
- #build_instance(data) ⇒ Object
- #count ⇒ Object
- #data_name ⇒ Object
- #find(id) ⇒ Object
- #first ⇒ Object
-
#initialize(connection, endpoint, options = nil) ⇒ Proxy
constructor
A new instance of Proxy.
- #new(attributes = {}) ⇒ Object
- #where(params = {}) ⇒ Object
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, = nil) @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( = {}, &block) if [:limit] == 'all' .delete(:limit) limit_all(, &block) else results = where() 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 |
#count ⇒ Object
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_name ⇒ Object
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 |
#first ⇒ Object
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
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 |