Class: DAV4Rack::Carddav::ContactResource

Inherits:
Resource
  • Object
show all
Defined in:
lib/dav4rack_ext/carddav/resources/contact_resource.rb

Constant Summary

Constants inherited from Resource

Resource::CARDAV_NS, Resource::PRIVILEGES

Instance Method Summary collapse

Methods inherited from Resource

#children, #current_user, #get_property, #initialize, #is_self?, #properties, #router_params, #user_agent

Methods included from Helpers::Properties

#define_properties, extended

Constructor Details

This class inherits a constructor from DAV4Rack::Carddav::Resource

Instance Method Details

#collection?Boolean

Returns:

  • (Boolean)


46
47
48
# File 'lib/dav4rack_ext/carddav/resources/contact_resource.rb', line 46

def collection?
  false
end

#deleteObject



135
136
137
138
# File 'lib/dav4rack_ext/carddav/resources/contact_resource.rb', line 135

def delete
  @contact.destroy
  NoContent
end

#exist?Boolean

Returns:

  • (Boolean)


50
51
52
53
# File 'lib/dav4rack_ext/carddav/resources/contact_resource.rb', line 50

def exist?
  Logger.info "ContactR::exist?(#{public_path});"
  @contact != nil
end

#get(request, response) ⇒ Object



130
131
132
133
# File 'lib/dav4rack_ext/carddav/resources/contact_resource.rb', line 130

def get(request, response)
  response.headers['Etag'] = @contact.etag
  response.body = @contact.vcard.vcard
end

#parentObject



118
119
120
# File 'lib/dav4rack_ext/carddav/resources/contact_resource.rb', line 118

def parent
  @address_book
end

#parent_collection?Boolean

Returns:

  • (Boolean)


126
127
128
# File 'lib/dav4rack_ext/carddav/resources/contact_resource.rb', line 126

def parent_collection?
  true
end

#parent_exists?Boolean

Returns:

  • (Boolean)


122
123
124
# File 'lib/dav4rack_ext/carddav/resources/contact_resource.rb', line 122

def parent_exists?
  @address_book != nil
end

#put(request, response) ⇒ Object

Raises:

  • (BadRequest)


61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/dav4rack_ext/carddav/resources/contact_resource.rb', line 61

def put(request, response)
  b = request.body.read

  # Ensure we only have one vcard per request
  # Section 5.1:
  # Address object resources contained in address book collections MUST
  # contain a single vCard component only.
  vcards = VCardParser::VCard.parse(b)
  raise BadRequest if vcards.size != 1
  vcf = vcards[0]

  uid = vcf['UID'].value

  # [6.3.2] Check for If-None-Match: *
  # If set, client does want to create a new contact only, raise an error
  # if contact already exists
  want_new_contact = (request.env['HTTP_IF_NONE_MATCH'] == '*')
  
  @contact = @address_book.find_contact(uid)

  # If the client has explicitly stated they want a new contact
  raise Conflict if (want_new_contact and @contact)

  if if_match = request.env['HTTP_IF_MATCH']
    # client wants to update a contact, return an error if no
    # contact was found
    if (if_match == '*') || !@contact
      raise PreconditionFailed unless @contact
    
    # client wants to update the contact with specific etag,
    # return an error if the contact was updated by someone else
    elsif (if_match != @contact.etag)
      raise PreconditionFailed
      
    end
  end
  
  if @contact
    Logger.debug("Updating contact #{uid} (#{@contact.object_id})")
  else
    Logger.debug("Creating new contact ! (#{uid})")
    @contact = @address_book.create_contact(uid)
  end

  @contact.update_from_vcard(vcf)

  if @contact.save(user_agent)
    new_public = @public_path.split('/')[0...-1]
    new_public << @contact.uid.to_s

    @public_path = new_public.join('/')
    response['ETag'] = @contact.etag
  end
  
  Created
end

#setupObject



55
56
57
58
59
# File 'lib/dav4rack_ext/carddav/resources/contact_resource.rb', line 55

def setup
  super
  @address_book = @options[:_parent_] || current_user.current_addressbook()
  @contact = @options[:_object_] || current_user.current_contact()
end