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.



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

def initialize(options = {})
  if options[:http_client].nil?
    options[:http_client] = :net_http
  end

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

Instance Attribute Details

#hostnameObject

Returns the value of attribute hostname.



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

def hostname
  @hostname
end

#http_clientObject

Returns the value of attribute http_client.



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

def http_client
  @http_client
end

#namespaceObject

Returns the value of attribute namespace.



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

def namespace
  @namespace
end

#portObject

Returns the value of attribute port.



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

def port
  @port
end

#required_query_paramsObject

Returns the value of attribute required_query_params.



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

def required_query_params
  @required_query_params
end

#secureObject

Returns the value of attribute secure.



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

def secure
  @secure
end

#url_rootObject

Returns the value of attribute url_root.



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

def url_root
  @url_root
end

Instance Method Details

#accept_headerObject



41
42
43
# File 'lib/ruby_json_api_client/adapters/rest_adapter.rb', line 41

def accept_header
  'application/json'
end

#collection_path(klass, params) ⇒ Object



35
36
37
38
39
# File 'lib/ruby_json_api_client/adapters/rest_adapter.rb', line 35

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

#create(model, data) ⇒ Object



82
83
84
85
86
87
88
89
90
91
# File 'lib/ruby_json_api_client/adapters/rest_adapter.rb', line 82

def create(model, data)
  url = collection_path(model.class, {})
  status, _, body = http_request(:post, url, data)

  if status >= 200 && status <= 299
    body
  else
    raise "Could not post to #{url}"
  end
end

#delete(model) ⇒ Object



104
105
106
107
108
109
110
111
112
113
# File 'lib/ruby_json_api_client/adapters/rest_adapter.rb', line 104

def delete(model)
  url = single_path(model.class, { id: model.id })
  status, _, body = http_request(:delete, url, {})

  if status >= 200 && status <= 299
    body
  else
    raise "Could not delete to #{url}"
  end
end

#find(klass, id) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/ruby_json_api_client/adapters/rest_adapter.rb', line 56

def find(klass, id)
  raise "Cannot find nil id" if id.nil?

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

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

#find_many(klass, params) ⇒ Object



71
72
73
74
75
76
77
78
79
80
# File 'lib/ruby_json_api_client/adapters/rest_adapter.rb', line 71

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



115
116
117
118
119
120
121
122
123
# File 'lib/ruby_json_api_client/adapters/rest_adapter.rb', line 115

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

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

#headersObject



49
50
51
52
53
54
# File 'lib/ruby_json_api_client/adapters/rest_adapter.rb', line 49

def headers
  {
    accept: accept_header,
    user_user: user_agent
  }
end

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



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

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

#update(model, data) ⇒ Object



93
94
95
96
97
98
99
100
101
102
# File 'lib/ruby_json_api_client/adapters/rest_adapter.rb', line 93

def update(model, data)
  url = single_path(model.class, { id: model.id })
  status, _, body = http_request(:put, url, data)

  if status >= 200 && status <= 299
    body
  else
    raise "Could not put to #{url}"
  end
end

#user_agentObject



45
46
47
# File 'lib/ruby_json_api_client/adapters/rest_adapter.rb', line 45

def user_agent
  'RubyJsonApiClient'
end