Class: RubyJsonApiClient::RestAdapter

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby_json_api_client/adapters/rest_adapter.rb

Direct Known Subclasses

AmsAdapter, JsonApiAdapter

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ RestAdapter

Returns a new instance of RestAdapter.



13
14
15
16
17
# File 'lib/ruby_json_api_client/adapters/rest_adapter.rb', line 13

def initialize(options = {})
  options.each do |(field, value)|
    send("#{field}=", value)
  end
end

Instance Attribute Details

#hostnameObject

Returns the value of attribute hostname.



8
9
10
# File 'lib/ruby_json_api_client/adapters/rest_adapter.rb', line 8

def hostname
  @hostname
end

#namespaceObject

Returns the value of attribute namespace.



9
10
11
# File 'lib/ruby_json_api_client/adapters/rest_adapter.rb', line 9

def namespace
  @namespace
end

#portObject

Returns the value of attribute port.



10
11
12
# File 'lib/ruby_json_api_client/adapters/rest_adapter.rb', line 10

def port
  @port
end

#secureObject

Returns the value of attribute secure.



7
8
9
# File 'lib/ruby_json_api_client/adapters/rest_adapter.rb', line 7

def secure
  @secure
end

#url_rootObject

Returns the value of attribute url_root.



11
12
13
# File 'lib/ruby_json_api_client/adapters/rest_adapter.rb', line 11

def url_root
  @url_root
end

Instance Method Details

#collection_path(klass, params) ⇒ Object



27
28
29
30
31
# File 'lib/ruby_json_api_client/adapters/rest_adapter.rb', line 27

def collection_path(klass, params)
  name = klass.name
  plural = ActiveSupport::Inflector.pluralize(name)
  "#{@namespace}/#{plural.underscore}"
end

#find(klass, id) ⇒ Object



33
34
35
36
37
38
39
40
41
42
# File 'lib/ruby_json_api_client/adapters/rest_adapter.rb', line 33

def find(klass, id)
  path = single_path(klass, id: id)
  status, _, body = http_request(:get, path, {})

  if status >= 200 && status <= 299
    body
  else
    raise "Could not find #{klass.name} with id #{id}"
  end
end

#find_many(klass, params) ⇒ Object



44
45
46
47
48
49
50
51
52
53
# File 'lib/ruby_json_api_client/adapters/rest_adapter.rb', line 44

def find_many(klass, params)
  path = collection_path(klass, params)
  status, _, body = http_request(:get, path, params)

  if status >= 200 && status <= 299
    body
  else
    raise "Could not query #{klass.name}"
  end
end

#get(url) ⇒ Object



55
56
57
58
59
60
61
62
63
# File 'lib/ruby_json_api_client/adapters/rest_adapter.rb', line 55

def get(url)
  status, _, body = http_request(:get, url, {})

  if status >= 200 && status <= 299
    body
  else
    raise "Could not query #{path}"
  end
end

#single_path(klass, params = {}) ⇒ Object



19
20
21
22
23
24
25
# File 'lib/ruby_json_api_client/adapters/rest_adapter.rb', line 19

def single_path(klass, params = {})
  name = klass.name
  plural = ActiveSupport::Inflector.pluralize(name)
  path = plural.underscore
  id = params[:id]
  "#{@namespace}/#{path}/#{id}"
end