Class: ApiResource::Finders::AbstractFinder

Inherits:
Object
  • Object
show all
Defined in:
lib/api_resource/finders/abstract_finder.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(klass, condition) ⇒ AbstractFinder

Returns a new instance of AbstractFinder.



14
15
16
17
18
19
20
# File 'lib/api_resource/finders/abstract_finder.rb', line 14

def initialize(klass, condition)
  @klass = klass
  @condition = condition
  @found = false

  @klass.load_resource_definition
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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

proxy unknown methods to the internal_object



121
122
123
# File 'lib/api_resource/finders/abstract_finder.rb', line 121

def method_missing(method, *args, &block)
  self.internal_object.send(method, *args, &block)
end

Instance Attribute Details

#conditionObject

Returns the value of attribute condition.



8
9
10
# File 'lib/api_resource/finders/abstract_finder.rb', line 8

def condition
  @condition
end

#foundObject (readonly)

Returns the value of attribute found.



9
10
11
# File 'lib/api_resource/finders/abstract_finder.rb', line 9

def found
  @found
end

#internal_objectObject (readonly)

Returns the value of attribute internal_object.



9
10
11
# File 'lib/api_resource/finders/abstract_finder.rb', line 9

def internal_object
  @internal_object
end

#klassObject

Returns the value of attribute klass.



8
9
10
# File 'lib/api_resource/finders/abstract_finder.rb', line 8

def klass
  @klass
end

Instance Method Details

#all(*args) ⇒ Object



112
113
114
115
116
117
118
# File 'lib/api_resource/finders/abstract_finder.rb', line 112

def all(*args)
  if args.blank?
    self.internal_object
  else
    self.klass.send(:all, *args)
  end
end

#headersHash

Return the headers for our response from the server

Returns:

  • (Hash)

    Headers hash



60
61
62
# File 'lib/api_resource/finders/abstract_finder.rb', line 60

def headers
  self.response.try(:headers)
end

#instance_of?(klass) ⇒ Boolean

Allows us to respond correctly to instance_of? based on our internal_object

Parameters:

  • klass (Class)

    Class to check

Returns:

  • (Boolean)


29
30
31
# File 'lib/api_resource/finders/abstract_finder.rb', line 29

def instance_of?(klass)
  super || self.internal_object.instance_of?(klass)
end

#is_a?(klass) ⇒ Boolean

Allows us to respond correctly to is_a? based on our internal_object

Parameters:

  • klass (Class)

    Class to check

Returns:

  • (Boolean)


40
41
42
# File 'lib/api_resource/finders/abstract_finder.rb', line 40

def is_a?(klass)
  super || self.internal_object.is_a?(klass)
end

#kind_of?(klass) ⇒ Boolean

Allows us to respond correctly to kind_of? based on our internal_object

Parameters:

  • klass (Class)

    Class to check

Returns:

  • (Boolean)


51
52
53
# File 'lib/api_resource/finders/abstract_finder.rb', line 51

def kind_of?(klass)
  self.is_a?(klass)
end

#loadObject



73
74
75
# File 'lib/api_resource/finders/abstract_finder.rb', line 73

def load
  raise NotImplementedError("Must be defined in a subclass")
end

#offsetFixnum

Offset returned from the server

Returns:

  • (Fixnum)


81
82
83
# File 'lib/api_resource/finders/abstract_finder.rb', line 81

def offset
  self.headers.try(:[], 'ApiResource-Offset').to_i
end

#paginated?Boolean

Is this a paginated find?

Returns:

  • (Boolean)


89
90
91
# File 'lib/api_resource/finders/abstract_finder.rb', line 89

def paginated?
  @condition.paginated?
end

#responseApiResource::Response

Getter for our response from the server



106
107
108
109
110
# File 'lib/api_resource/finders/abstract_finder.rb', line 106

def response
  @response ||= begin
    self.klass.connection.get(self.build_load_path)
  end
end

#total_entriesFixnum

Total number of entries the server has told us are in our collection

Returns:

  • (Fixnum)


98
99
100
# File 'lib/api_resource/finders/abstract_finder.rb', line 98

def total_entries
  self.headers.try(:[], 'ApiResource-Total-Entries').to_i
end