class RubyJsonApiClient::RestAdapter

Attributes

hostname[RW]
http_client[RW]
namespace[RW]
port[RW]
required_query_params[RW]
secure[RW]
url_root[RW]

Public Class Methods

new(options = {}) click to toggle source
# 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

Public Instance Methods

accept_header() click to toggle source
# File lib/ruby_json_api_client/adapters/rest_adapter.rb, line 41
def accept_header
  'application/json'
end
collection_path(klass, params) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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
headers() click to toggle source
# 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 = {}) click to toggle source
# 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) click to toggle source
# 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_agent() click to toggle source
# File lib/ruby_json_api_client/adapters/rest_adapter.rb, line 45
def user_agent
  'RubyJsonApiClient'
end

Protected Instance Methods

http_request(method, url, params) click to toggle source
# File lib/ruby_json_api_client/adapters/rest_adapter.rb, line 127
def http_request(method, url, params)
  uri = Addressable::URI.parse(url)

  proto = uri.scheme || (@secure ? "https" : "http")
  hostname = uri.host || @hostname
  port = uri.port || @port || (@secure ? 443 : 80)
  path = uri.path

  query_params = (required_query_params || {})
    .merge(uri.query_values || {})
    .merge(params)

  conn = Faraday.new("#{proto}://#{hostname}:#{port}", {
    headers: headers
  }) do |f|
    f.request :json
    f.adapter @http_client
  end

  response = conn.send(method, path, query_params)
  [response.status, response.headers, response.body]
end