lib/munson/agent.rb in munson-0.2.0 vs lib/munson/agent.rb in munson-0.3.0
- old
+ new
@@ -1,129 +1,95 @@
module Munson
class Agent
- extend Forwardable
- def_delegators :query, :includes, :sort, :filter, :fields, :fetch, :page
-
- attr_writer :connection
-
- attr_accessor :type
- attr_accessor :query_builder
-
- attr_reader :paginator
- attr_accessor :paginator_options
-
# Creates a new Munson::Agent
#
- # @param [Hash] opts={} describe opts={}
- # @option opts [Munson::Connection] :connection to use
- # @option opts [#to_s, Munson::Paginator] :paginator to use on query builder
- # @option opts [Class] :query_builder provide a custom query builder, defaults to {Munson::QueryBuilder}
- # @option opts [#to_s] :type JSON Spec type. Type will be added to the base path set in the Faraday::Connection
- def initialize(opts={})
- @connection = opts[:connection]
- @type = opts[:type]
-
- @query_builder = opts[:query_builder].is_a?(Class) ?
- opts[:query_builder] : Munson::QueryBuilder
-
- self.paginator = opts[:paginator]
- @paginator_options = opts[:paginator_options]
+ # @param [#to_s] path to JSON API Resource. Path will be added to the base path set in the Faraday::Connection
+ # @param [Munson::Connection] connection to use
+ def initialize(path, connection: nil)
+ @path = path
+ @connection = connection
end
- def paginator=(pager)
- if pager.is_a?(Symbol)
- @paginator = Munson.lookup_paginator(pager)
- else
- @paginator = pager
- end
- end
-
- # Munson::QueryBuilder factory
- #
- # @example creating a query
- # @agent.includes('user').sort(age: :desc)
- #
- # @return [Munson::QueryBuilder] a query builder
- def query
- if paginator
- query_pager = paginator.new(paginator_options || {})
- @query_builder.new paginator: query_pager, agent: self
- else
- @query_builder.new agent: self
- end
- end
-
# Connection that will be used for HTTP requests
#
# @return [Munson::Connection] current connection of Munson::Agent or Munson.default_connection if not set
def connection
return @connection if @connection
Munson.default_connection
end
- def find(id, headers: nil, params: nil)
- path = [type, id].join('/')
- response = get(path: path, headers: headers, params: params)
- ResponseMapper.new(response).resource
- end
-
# JSON API Spec GET request
#
# @option [Hash,nil] params: nil query params
- # @option [String] path: nil path to GET, defaults to Faraday::Connection url + Agent#type
+ # @option [String] path: nil path to GET, defaults to Faraday::Connection url + Agent#default_path
# @option [Hash] headers: nil HTTP Headers
+ # @option [String,Fixnum] id: nil ID to append to @path (provided in #new) when accessing a resource. If :path and :id are both specified, :path wins
# @return [Faraday::Response]
- def get(params: nil, path: nil, headers: nil)
+ def get(params: nil, path: nil, headers: nil, id: nil)
connection.get(
- path: (path || type),
+ path: negotiate_path(path, id),
params: params,
headers: headers
)
end
+ def negotiate_path(path = nil, id = nil)
+ if path
+ path
+ elsif id
+ [@path, id].join('/')
+ else
+ @path
+ end
+ end
+
# JSON API Spec POST request
#
# @option [Hash,nil] body: {} query params
- # @option [String] path: nil path to GET, defaults to Faraday::Connection url + Agent#type
+ # @option [String] path: nil path to GET, defaults to Faraday::Connection url + Agent#default_path
# @option [Hash] headers: nil HTTP Headers
# @option [Type] http_method: :post describe http_method: :post
+ # @option [String,Fixnum] id: nil ID to append to default path when accessing a resource. If :path and :id are both specified, :path wins
# @return [Faraday::Response]
- def post(body: {}, path: nil, headers: nil, http_method: :post)
+ def post(body: {}, path: nil, headers: nil, http_method: :post, id: nil)
connection.post(
- path: (path || type),
+ path: negotiate_path(path, id),
body: body,
headers: headers,
http_method: http_method
)
end
# JSON API Spec PATCH request
#
# @option [Hash,nil] body: nil query params
- # @option [String] path: nil path to GET, defaults to Faraday::Connection url + Agent#type
+ # @option [String] path: nil path to GET, defaults to Faraday::Connection url + Agent#default_path
# @option [Hash] headers: nil HTTP Headers
+ # @option [String,Fixnum] id: nil ID to append to default path when accessing a resource. If :path and :id are both specified, :path wins
# @return [Faraday::Response]
- def patch(body: nil, path: nil, headers: nil)
- post(body, path: path, headers: headers, http_method: :patch)
+ def patch(body: nil, path: nil, headers: nil, id: nil)
+ post(body: body, path: path, headers: headers, http_method: :patch, id: id)
end
# JSON API Spec PUT request
#
# @option [Hash,nil] body: nil query params
- # @option [String] path: nil path to GET, defaults to Faraday::Connection url + Agent#type
+ # @option [String] path: nil path to GET, defaults to Faraday::Connection url + Agent#default_path
# @option [Hash] headers: nil HTTP Headers
+ # @option [String,Fixnum] id: nil ID to append to default path when accessing a resource. If :path and :id are both specified, :path wins
# @return [Faraday::Response]
- def put(body: nil, path: nil, headers: nil)
- post(body, path: path, headers: headers, http_method: :put)
+ def put(body: nil, path: nil, headers: nil, id: nil)
+ post(body: body, path: path, headers: headers, http_method: :put, id: id)
end
# JSON API Spec DELETE request
#
# @option [Hash,nil] body: nil query params
- # @option [String] path: nil path to GET, defaults to Faraday::Connection url + Agent#type
+ # @option [String] path: nil path to GET, defaults to Faraday::Connection url + Agent#default_path
# @option [Hash] headers: nil HTTP Headers
+ # @option [String,Fixnum] id: nil ID to append to default path when accessing a resource. If :path and :id are both specified, :path wins
# @return [Faraday::Response]
- def delete(body: nil, path: nil, headers: nil)
- post(body, path: path, headers: headers, http_method: :delete)
+ def delete(body: nil, path: nil, headers: nil, id: nil)
+ post(body: body, path: path, headers: headers, http_method: :delete, id: id)
end
end
end