Class: DataMapper::Adapters::SsbeAdapter

Inherits:
HttpAdapter
  • Object
show all
Defined in:
lib/dm-ssbe-adapter.rb

Constant Summary collapse

SSJ =
'application/vnd.absperf.ssbe+json'

Instance Attribute Summary collapse

Attributes inherited from HttpAdapter

#http

Instance Method Summary collapse

Methods inherited from HttpAdapter

#logger

Constructor Details

#initialize(name, options = {}) ⇒ SsbeAdapter

Returns a new instance of SsbeAdapter.



36
37
38
39
40
41
42
43
44
# File 'lib/dm-ssbe-adapter.rb', line 36

def initialize(name, options = {})
  super

  username, password = options[:username], options[:password]

  http.add_authenticator(Resourceful::SSBEAuthenticator.new(username, password))

  @services_uri = options[:services_uri]
end

Instance Attribute Details

#services_uriObject (readonly)

Returns the value of attribute services_uri.



32
33
34
# File 'lib/dm-ssbe-adapter.rb', line 32

def services_uri
  @services_uri
end

Instance Method Details

#create(resources) ⇒ Object



46
47
48
49
50
51
52
53
54
55
# File 'lib/dm-ssbe-adapter.rb', line 46

def create(resources)
  resources.each do |resource|
    http_resource = collection_resource_for(resource.model)
    document = serialize(resource)

    response = http_resource.post(document, :content_type => SSJ)

    update_attributes(resource, deserialize(response.body))
  end
end

#delete(collection) ⇒ Object



105
106
107
108
109
110
111
112
# File 'lib/dm-ssbe-adapter.rb', line 105

def delete(collection)
  collection.each do |resource|
    http_resource = http.resource(resource.href, :accept => SSJ)
    response = http_resource.delete

    update_attributes(resource, deserialize(response.body))
  end
end

#read(query) ⇒ Object



57
58
59
60
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
# File 'lib/dm-ssbe-adapter.rb', line 57

def read(query)
  ## [dm-core] need an easy way to determine if we're 
  # looking up a single record by key
  if querying_on_href?(query)
    href = if query.respond_to?(:location)
             query.location
           else
             operand = query.conditions.operands.first
             operand.value
           end

    http_resource = http.resource(href, :accept => SSJ)
    begin
      response = http_resource.get
    rescue Resourceful::UnsuccessfulHttpRequestError => e
      if e.http_response.code == 404
        return []
      else
        raise e
      end
    end
    record = deserialize(response.body)
    if record.has_key?(:items) 
      query.filter_records(record[:items])
    else
      [record]
    end
  else 
    resource = collection_resource_for(query)
    opts = {}
    opts.merge(:cache_control => 'no-cache') if query.reload?

    response = resource.get(opts)

    records = deserialize(response.body)
    query.filter_records(records[:items])
  end
end

#update(attributes, collection) ⇒ Object



96
97
98
99
100
101
102
103
# File 'lib/dm-ssbe-adapter.rb', line 96

def update(attributes, collection)
  collection.each do |resource|
    http_resource = http.resource(resource.href, :accept => SSJ)
    response = http_resource.put(serialize(attributes), :content_type => SSJ)

    update_attributes(resource, deserialize(response.body))
  end
end