Class: RubyJsonApiClient::RestAdapter
- Inherits:
-
Object
- Object
- RubyJsonApiClient::RestAdapter
- Defined in:
- lib/ruby_json_api_client/adapters/rest_adapter.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#hostname ⇒ Object
Returns the value of attribute hostname.
-
#namespace ⇒ Object
Returns the value of attribute namespace.
-
#port ⇒ Object
Returns the value of attribute port.
-
#secure ⇒ Object
Returns the value of attribute secure.
-
#url_root ⇒ Object
Returns the value of attribute url_root.
Instance Method Summary collapse
- #collection_path(klass, params) ⇒ Object
- #find(klass, id) ⇒ Object
- #find_many(klass, params) ⇒ Object
- #get(url) ⇒ Object
-
#initialize(options = {}) ⇒ RestAdapter
constructor
A new instance of RestAdapter.
- #single_path(klass, params = {}) ⇒ Object
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( = {}) .each do |(field, value)| send("#{field}=", value) end end |
Instance Attribute Details
#hostname ⇒ Object
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 |
#namespace ⇒ Object
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 |
#port ⇒ Object
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 |
#secure ⇒ Object
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_root ⇒ Object
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 |