Class: Cardiac::ResourceAdapter

Inherits:
Object
  • Object
show all
Extended by:
ActiveModel::Callbacks
Includes:
Cardiac::Representation::LookupMethods, Cardiac::ResourceCache::InstanceMethods
Defined in:
lib/cardiac/resource/adapter.rb

Overview

An adapter for performing operations on a resource.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Cardiac::Representation::LookupMethods

#coder_for, #mime_types, #mimes_for

Constructor Details

#initialize(klass, base, payload = nil) ⇒ ResourceAdapter

Returns a new instance of ResourceAdapter.



27
28
29
30
31
# File 'lib/cardiac/resource/adapter.rb', line 27

def initialize(klass,base,payload=nil)
  @klass               = klass
  @reflection          = base.to_reflection if base.respond_to? :to_reflection
  resolve! base
end

Instance Attribute Details

#klassObject

Returns the value of attribute klass.



16
17
18
# File 'lib/cardiac/resource/adapter.rb', line 16

def klass
  @klass
end

#payloadObject

Returns the value of attribute payload.



16
17
18
# File 'lib/cardiac/resource/adapter.rb', line 16

def payload
  @payload
end

#resourceObject

Returns the value of attribute resource.



16
17
18
# File 'lib/cardiac/resource/adapter.rb', line 16

def resource
  @resource
end

#resultObject

Returns the value of attribute result.



16
17
18
# File 'lib/cardiac/resource/adapter.rb', line 16

def result
  @result
end

Instance Method Details

#__client_options__Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/cardiac/resource/adapter.rb', line 33

def __client_options__
  if resolved?
    @__client_options__ ||= resource.send(:build_client_options).tap do |h|
      h = (h[:headers] ||= {})
        
      # Content-Type
      if content_type = h.delete(:content_type).presence
        content_type = mimes_for(content_type).first
      else
        content_type = encoder_reflection.base_reflection.default_type
      end
      h['content_type'] = content_type.try(:content_type) || 'application/x-www-form-urlencoded'
          
      # Accept
      if accept = h.delete(:accepts).presence and Array===accept
        accept = accept.map{|ext| mimes_for(ext.to_s.strip).first }
      else
        accept = decoder_reflections.map{|dr| dr.base_reflection.default_type }.compact
      end
      h['accept'] = accept.empty? ? '*/*; q=0.5, application/json' : accept.join('; ')
    end
  end
end

#call!(*arguments, &block) ⇒ Object

Performs a remote call by performing the remaining phases in the lifecycle of this adapter.



63
64
65
66
67
68
69
70
71
72
# File 'lib/cardiac/resource/adapter.rb', line 63

def call! *arguments, &block
  self.result = nil
  
  resolved? or raise UnresolvableResourceError
  prepared? or prepare! or raise InvalidOperationError
  encode! *arguments
  execute! &block
ensure
  decode! if completed?
end

#http_verbObject

Convenience method to return the current HTTP verb



58
59
60
# File 'lib/cardiac/resource/adapter.rb', line 58

def http_verb
  @http_verb ||= (defined? @__client_options__ and @__client_options__[:method].to_s.upcase)
end

#prepared?Boolean

Returns:

  • (Boolean)


78
79
80
# File 'lib/cardiac/resource/adapter.rb', line 78

def prepared?
  @__client_options__.present?
end

#resolved?Boolean

Returns:

  • (Boolean)


74
75
76
# File 'lib/cardiac/resource/adapter.rb', line 74

def resolved?
  resource.present?
end