Class: Datapathy::Adapters::HttpAdapter

Inherits:
AbstractAdapter
  • Object
show all
Defined in:
lib/datapathy/adapters/http_adapter.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ HttpAdapter

Returns a new instance of HttpAdapter.



11
12
13
14
15
16
17
18
19
20
# File 'lib/datapathy/adapters/http_adapter.rb', line 11

def initialize(options = {})
  super

  @services_uri = @options[:services_uri]

  @http = Resourceful::HttpAccessor.new
  @http.logger = @options[:logger] if @options[:logger]
  @http.cache_manager = Resourceful::InMemoryCacheManager.new
  #@http.add_authenticator Resourceful::LSAuthenticator.new(@username, @password) # Add a custom authenticator
end

Instance Attribute Details

#httpObject (readonly)

Returns the value of attribute http.



9
10
11
# File 'lib/datapathy/adapters/http_adapter.rb', line 9

def http
  @http
end

#services_uriObject (readonly)

Returns the value of attribute services_uri.



9
10
11
# File 'lib/datapathy/adapters/http_adapter.rb', line 9

def services_uri
  @services_uri
end

Instance Method Details

#create(collection) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/datapathy/adapters/http_adapter.rb', line 22

def create(collection)
  query = collection.query
  http_resource = http_resource_for(query)

  collection.each do |resource|
    record = serialize(resource)
    content_type = "application/vnd.ls.v1+json"

    begin
      response = http_resource.post(record, "Content-Type" => content_type)
      resource.key = response.header['Location']
      resource.merge!(deserialize(response)) unless response.body.blank?
    rescue Resourceful::UnsuccessfulHttpRequestError => e
      if e.http_response.code == 403
        set_errors(resource, e)
      else
        raise e
      end
    end
  end
end

#read(collection) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/datapathy/adapters/http_adapter.rb', line 44

def read(collection)
  query = collection.query
  if query.key_lookup?
    response = http.resource(query.key, default_headers).get
    Array.wrap(deserialize(response))
  else
    response = http_resource_for(query).get
    records = deserialize(response)[:items]
    records.map! { |r| r.symbolize_keys! }
    records
  end
end

#update(attributes, collection) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/datapathy/adapters/http_adapter.rb', line 57

def update(attributes, collection)
  collection.each do |resource|
    content = serialize(resource, attributes)
    content_type = content_type_for(resource)

    begin
      response = http.resource(resource.href, default_headers).put(content, "Content-Type" => content_type)
      resource.merge!(deserialize(response)) unless response.body.blank?
    rescue Resourceful::UnsuccessfulHttpRequestError => e
      if e.http_response.code == 403
        set_errors(resource, e)
      else
        raise e
      end
    end
  end
end