Class: DataMapperRest::Adapter

Inherits:
DataMapper::Adapters::AbstractAdapter
  • Object
show all
Defined in:
lib/dm-rest-adapter/adapter.rb

Overview

All http_“verb” (http_post) method calls use method missing in connection class which uses run_verb

Instance Method Summary collapse

Instance Method Details

#create(resources) ⇒ Object



7
8
9
10
11
12
13
14
15
# File 'lib/dm-rest-adapter/adapter.rb', line 7

def create(resources)
  resources.each do |resource|
    model = resource.model

    response = connection.http_post("#{resource_name(model)}", resource.to_xml)

    update_with_response(resource, response)
  end
end

#delete(collection) ⇒ Object



49
50
51
52
53
54
55
56
57
58
# File 'lib/dm-rest-adapter/adapter.rb', line 49

def delete(collection)
  collection.select do |resource|
    model = resource.model
    key   = model.key
    id    = key.get(resource).join

    response = connection.http_delete("#{resource_name(model)}/#{id}")
    response.kind_of?(Net::HTTPSuccess)
  end.size
end

#read(query) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/dm-rest-adapter/adapter.rb', line 17

def read(query)
  model = query.model

  records = if id = extract_id_from_query(query)
    response = connection.http_get("#{resource_name(model)}/#{id}")
    [ parse_resource(response.body, model) ]
  else
    query_string = if (params = extract_params_from_query(query)).any?
      params.map { |k,v| "#{CGI.escape(k.to_s)}=#{CGI.escape(v.to_s)}" }.join('&')
    end

    response = connection.http_get("#{resource_name(model)}#{'?' << query_string if query_string}")
    parse_resources(response.body, model)
  end

  query.filter_records(records)
end

#update(dirty_attributes, collection) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/dm-rest-adapter/adapter.rb', line 35

def update(dirty_attributes, collection)
  collection.select do |resource|
    model = resource.model
    key   = model.key
    id    = key.get(resource).join

    dirty_attributes.each { |p, v| p.set!(resource, v) }

    response = connection.http_put("#{resource_name(model)}/#{id}", resource.to_xml)

    update_with_response(resource, response)
  end.size
end