Class: Munson::Agent

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

Instance Method Summary collapse

Constructor Details

#initialize(path, connection: nil) ⇒ Agent

Creates a new Munson::Agent

Parameters:

  • path (#to_s)

    to JSON API Resource. Path will be added to the base path set in the Faraday::Connection

  • connection (Munson::Connection) (defaults to: nil)

    to use



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

def initialize(path, connection: nil)
  @path       = path
  @connection = connection
end

Instance Method 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



15
16
17
18
# File 'lib/munson/agent.rb', line 15

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

#delete(body: nil, path: nil, headers: nil, id: 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

  • [String,Fixnum] (Hash)

    a customizable set of options

Returns:

  • (Faraday::Response)


91
92
93
# File 'lib/munson/agent.rb', line 91

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

#get(params: nil, path: nil, headers: nil, id: 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

  • [String,Fixnum] (Hash)

    a customizable set of options

Returns:

  • (Faraday::Response)


27
28
29
30
31
32
33
# File 'lib/munson/agent.rb', line 27

def get(params: nil, path: nil, headers: nil, id: nil)
  connection.get(
    path: negotiate_path(path, id),
    params: params,
    headers: headers
  )
end

#negotiate_path(path = nil, id = nil) ⇒ Object



35
36
37
38
39
40
41
42
43
# File 'lib/munson/agent.rb', line 35

def negotiate_path(path = nil, id = nil)
  if path
    path
  elsif id
    [@path, id].join('/')
  else
    @path
  end
end

#patch(body: nil, path: nil, headers: nil, id: 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

  • [String,Fixnum] (Hash)

    a customizable set of options

Returns:

  • (Faraday::Response)


69
70
71
# File 'lib/munson/agent.rb', line 69

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

#post(body: {}, path: nil, headers: nil, http_method: :post, id: nil) ⇒ 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

  • [String,Fixnum] (Hash)

    a customizable set of options

Returns:

  • (Faraday::Response)


53
54
55
56
57
58
59
60
# File 'lib/munson/agent.rb', line 53

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

#put(body: nil, path: nil, headers: nil, id: 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

  • [String,Fixnum] (Hash)

    a customizable set of options

Returns:

  • (Faraday::Response)


80
81
82
# File 'lib/munson/agent.rb', line 80

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