Class: Munson::Agent

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/munson/agent.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Agent

Creates a new Munson::Agent

Parameters:

  • opts={} (Hash)

    describe opts={}

  • opts (Hash) (defaults to: {})

    a customizable set of options

Options Hash (opts):

  • :connection (Munson::Connection)

    to use

  • :paginator (#to_s, Munson::Paginator)

    to use on query builder

  • :query_builder (Class)

    provide a custom query builder, defaults to QueryBuilder

  • :type (#to_s)

    JSON Spec type. Type will be added to the base path set in the Faraday::Connection



21
22
23
24
25
26
27
28
29
30
# File 'lib/munson/agent.rb', line 21

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]
end

Instance Attribute Details

#connectionMunson::Connection

Connection that will be used for HTTP requests

Returns:

  • (Munson::Connection)

    current connection of Munson::Agent or Munson.default_connection if not set



58
59
60
61
# File 'lib/munson/agent.rb', line 58

def connection
  return @connection if @connection
  Munson.default_connection
end

#paginatorObject

Returns the value of attribute paginator.



11
12
13
# File 'lib/munson/agent.rb', line 11

def paginator
  @paginator
end

#paginator_optionsObject

Returns the value of attribute paginator_options.



12
13
14
# File 'lib/munson/agent.rb', line 12

def paginator_options
  @paginator_options
end

#query_builderObject

Returns the value of attribute query_builder.



9
10
11
# File 'lib/munson/agent.rb', line 9

def query_builder
  @query_builder
end

#typeObject

Returns the value of attribute type.



8
9
10
# File 'lib/munson/agent.rb', line 8

def type
  @type
end

Instance Method Details

#delete(body: nil, path: nil, headers: nil) ⇒ Faraday::Response

JSON API Spec DELETE request

Parameters:

  • [Hash,nil] (Hash)

    a customizable set of options

  • [String] (Hash)

    a customizable set of options

  • [Hash] (Hash)

    a customizable set of options

Returns:

  • (Faraday::Response)


125
126
127
# File 'lib/munson/agent.rb', line 125

def delete(body: nil, path: nil, headers: nil)
  post(body, path: path, headers: headers, http_method: :delete)
end

#find(id, headers: nil, params: nil) ⇒ Object



63
64
65
66
67
# File 'lib/munson/agent.rb', line 63

def find(id, headers: nil, params: nil)
  path = [type, id].join('/')
  response = get(path: path, headers: headers, params: params)
  ResponseMapper.new(response).resource
end

#get(params: nil, path: nil, headers: nil) ⇒ Faraday::Response

JSON API Spec GET request

Parameters:

  • [Hash,nil] (Hash)

    a customizable set of options

  • [String] (Hash)

    a customizable set of options

  • [Hash] (Hash)

    a customizable set of options

Returns:

  • (Faraday::Response)


75
76
77
78
79
80
81
# File 'lib/munson/agent.rb', line 75

def get(params: nil, path: nil, headers: nil)
  connection.get(
    path: (path || type),
    params: params,
    headers: headers
  )
end

#patch(body: nil, path: nil, headers: nil) ⇒ Faraday::Response

JSON API Spec PATCH request

Parameters:

  • [Hash,nil] (Hash)

    a customizable set of options

  • [String] (Hash)

    a customizable set of options

  • [Hash] (Hash)

    a customizable set of options

Returns:

  • (Faraday::Response)


105
106
107
# File 'lib/munson/agent.rb', line 105

def patch(body: nil, path: nil, headers: nil)
  post(body, path: path, headers: headers, http_method: :patch)
end

#post(body: {}, path: nil, headers: nil, http_method: :post) ⇒ Faraday::Response

JSON API Spec POST request

Parameters:

  • [Hash,nil] (Hash)

    a customizable set of options

  • [String] (Hash)

    a customizable set of options

  • [Hash] (Hash)

    a customizable set of options

  • [Type] (Hash)

    a customizable set of options

Returns:

  • (Faraday::Response)


90
91
92
93
94
95
96
97
# File 'lib/munson/agent.rb', line 90

def post(body: {}, path: nil, headers: nil, http_method: :post)
  connection.post(
    path: (path || type),
    body: body,
    headers: headers,
    http_method: http_method
  )
end

#put(body: nil, path: nil, headers: nil) ⇒ Faraday::Response

JSON API Spec PUT request

Parameters:

  • [Hash,nil] (Hash)

    a customizable set of options

  • [String] (Hash)

    a customizable set of options

  • [Hash] (Hash)

    a customizable set of options

Returns:

  • (Faraday::Response)


115
116
117
# File 'lib/munson/agent.rb', line 115

def put(body: nil, path: nil, headers: nil)
  post(body, path: path, headers: headers, http_method: :put)
end

#queryMunson::QueryBuilder

Munson::QueryBuilder factory

Examples:

creating a query

@agent.includes('user').sort(age: :desc)

Returns:



46
47
48
49
50
51
52
53
# File 'lib/munson/agent.rb', line 46

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