Class: Pushover::Response

Inherits:
Struct
  • Object
show all
Defined in:
lib/pushover/response.rb

Overview

Response encapsulates the response back from pushover. Class to wrap the user and basic functions related to it.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#attributesString

Returns any extra k/v pairs from the server.

Returns:

  • (String)

    any extra k/v pairs from the server.



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/pushover/response.rb', line 16

Response = Struct.new(:status, :request, :errors, :receipt, :headers, :attributes, keyword_init: true) do
  # New response object, from an excon_response
  #   @return [Response] populated response object
  def self.create_from_excon_response(excon_response)
    json = Oj.load excon_response[:body]
    values = {}
    attributes = {}
    json.each { |k, v| members.include?(k.to_sym) ? values.store(k, v) : attributes.store(k, v) }

    Response.new values.merge(headers: excon_response.headers, attributes: attributes)
  end

  # purty.
  def to_s
    "#{errors ? 'errors: ' + errors.join("\n") : 'status: ok'}, #{limits}"
  end

  private

  # :nocov:
  # Application limits
  # @return [Array] 0: Remaining Calls, 1: Total Limit, 2: Limit Reset
  def limits
    return '' unless headers.include? 'X-Limit-App-Limit'

    output = [headers['X-Limit-App-Remaining'], headers['X-Limit-App-Limit'], headers['X-Limit-App-Reset']]
    output.define_singleton_method(:to_s) { "requests #{self[0]} of #{self[1]}, reset on #{Time.at(self[2].to_f)}" }
    output
  end
end

#errorsArray

Returns errors includes any errors made.

Returns:

  • (Array)

    errors includes any errors made



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/pushover/response.rb', line 16

Response = Struct.new(:status, :request, :errors, :receipt, :headers, :attributes, keyword_init: true) do
  # New response object, from an excon_response
  #   @return [Response] populated response object
  def self.create_from_excon_response(excon_response)
    json = Oj.load excon_response[:body]
    values = {}
    attributes = {}
    json.each { |k, v| members.include?(k.to_sym) ? values.store(k, v) : attributes.store(k, v) }

    Response.new values.merge(headers: excon_response.headers, attributes: attributes)
  end

  # purty.
  def to_s
    "#{errors ? 'errors: ' + errors.join("\n") : 'status: ok'}, #{limits}"
  end

  private

  # :nocov:
  # Application limits
  # @return [Array] 0: Remaining Calls, 1: Total Limit, 2: Limit Reset
  def limits
    return '' unless headers.include? 'X-Limit-App-Limit'

    output = [headers['X-Limit-App-Remaining'], headers['X-Limit-App-Limit'], headers['X-Limit-App-Reset']]
    output.define_singleton_method(:to_s) { "requests #{self[0]} of #{self[1]}, reset on #{Time.at(self[2].to_f)}" }
    output
  end
end

#headersHash

Returns headers is the headers returned from the call.

Returns:

  • (Hash)

    headers is the headers returned from the call.



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/pushover/response.rb', line 16

Response = Struct.new(:status, :request, :errors, :receipt, :headers, :attributes, keyword_init: true) do
  # New response object, from an excon_response
  #   @return [Response] populated response object
  def self.create_from_excon_response(excon_response)
    json = Oj.load excon_response[:body]
    values = {}
    attributes = {}
    json.each { |k, v| members.include?(k.to_sym) ? values.store(k, v) : attributes.store(k, v) }

    Response.new values.merge(headers: excon_response.headers, attributes: attributes)
  end

  # purty.
  def to_s
    "#{errors ? 'errors: ' + errors.join("\n") : 'status: ok'}, #{limits}"
  end

  private

  # :nocov:
  # Application limits
  # @return [Array] 0: Remaining Calls, 1: Total Limit, 2: Limit Reset
  def limits
    return '' unless headers.include? 'X-Limit-App-Limit'

    output = [headers['X-Limit-App-Remaining'], headers['X-Limit-App-Limit'], headers['X-Limit-App-Reset']]
    output.define_singleton_method(:to_s) { "requests #{self[0]} of #{self[1]}, reset on #{Time.at(self[2].to_f)}" }
    output
  end
end

#receiptString

Returns receipt returns a receipt if requested.

Returns:

  • (String)

    receipt returns a receipt if requested



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/pushover/response.rb', line 16

Response = Struct.new(:status, :request, :errors, :receipt, :headers, :attributes, keyword_init: true) do
  # New response object, from an excon_response
  #   @return [Response] populated response object
  def self.create_from_excon_response(excon_response)
    json = Oj.load excon_response[:body]
    values = {}
    attributes = {}
    json.each { |k, v| members.include?(k.to_sym) ? values.store(k, v) : attributes.store(k, v) }

    Response.new values.merge(headers: excon_response.headers, attributes: attributes)
  end

  # purty.
  def to_s
    "#{errors ? 'errors: ' + errors.join("\n") : 'status: ok'}, #{limits}"
  end

  private

  # :nocov:
  # Application limits
  # @return [Array] 0: Remaining Calls, 1: Total Limit, 2: Limit Reset
  def limits
    return '' unless headers.include? 'X-Limit-App-Limit'

    output = [headers['X-Limit-App-Remaining'], headers['X-Limit-App-Limit'], headers['X-Limit-App-Reset']]
    output.define_singleton_method(:to_s) { "requests #{self[0]} of #{self[1]}, reset on #{Time.at(self[2].to_f)}" }
    output
  end
end

#requestString

Returns request is a UUID representing the specific call.

Returns:

  • (String)

    request is a UUID representing the specific call



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/pushover/response.rb', line 16

Response = Struct.new(:status, :request, :errors, :receipt, :headers, :attributes, keyword_init: true) do
  # New response object, from an excon_response
  #   @return [Response] populated response object
  def self.create_from_excon_response(excon_response)
    json = Oj.load excon_response[:body]
    values = {}
    attributes = {}
    json.each { |k, v| members.include?(k.to_sym) ? values.store(k, v) : attributes.store(k, v) }

    Response.new values.merge(headers: excon_response.headers, attributes: attributes)
  end

  # purty.
  def to_s
    "#{errors ? 'errors: ' + errors.join("\n") : 'status: ok'}, #{limits}"
  end

  private

  # :nocov:
  # Application limits
  # @return [Array] 0: Remaining Calls, 1: Total Limit, 2: Limit Reset
  def limits
    return '' unless headers.include? 'X-Limit-App-Limit'

    output = [headers['X-Limit-App-Remaining'], headers['X-Limit-App-Limit'], headers['X-Limit-App-Reset']]
    output.define_singleton_method(:to_s) { "requests #{self[0]} of #{self[1]}, reset on #{Time.at(self[2].to_f)}" }
    output
  end
end

#statusNumeric

Returns status of the response.

Returns:

  • (Numeric)

    status of the response



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/pushover/response.rb', line 16

Response = Struct.new(:status, :request, :errors, :receipt, :headers, :attributes, keyword_init: true) do
  # New response object, from an excon_response
  #   @return [Response] populated response object
  def self.create_from_excon_response(excon_response)
    json = Oj.load excon_response[:body]
    values = {}
    attributes = {}
    json.each { |k, v| members.include?(k.to_sym) ? values.store(k, v) : attributes.store(k, v) }

    Response.new values.merge(headers: excon_response.headers, attributes: attributes)
  end

  # purty.
  def to_s
    "#{errors ? 'errors: ' + errors.join("\n") : 'status: ok'}, #{limits}"
  end

  private

  # :nocov:
  # Application limits
  # @return [Array] 0: Remaining Calls, 1: Total Limit, 2: Limit Reset
  def limits
    return '' unless headers.include? 'X-Limit-App-Limit'

    output = [headers['X-Limit-App-Remaining'], headers['X-Limit-App-Limit'], headers['X-Limit-App-Reset']]
    output.define_singleton_method(:to_s) { "requests #{self[0]} of #{self[1]}, reset on #{Time.at(self[2].to_f)}" }
    output
  end
end

Class Method Details

.create_from_excon_response(excon_response) ⇒ Object

New response object, from an excon_response

@return [Response] populated response object


19
20
21
22
23
24
25
26
# File 'lib/pushover/response.rb', line 19

def self.create_from_excon_response(excon_response)
  json = Oj.load excon_response[:body]
  values = {}
  attributes = {}
  json.each { |k, v| members.include?(k.to_sym) ? values.store(k, v) : attributes.store(k, v) }

  Response.new values.merge(headers: excon_response.headers, attributes: attributes)
end

Instance Method Details

#to_sObject

purty.



29
30
31
# File 'lib/pushover/response.rb', line 29

def to_s
  "#{errors ? 'errors: ' + errors.join("\n") : 'status: ok'}, #{limits}"
end