Class: FootStats::Response

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

Constant Summary collapse

REGEX_PARSER =
/\{.*}/m

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Response

Returns a new instance of Response.



6
7
8
9
10
11
12
# File 'lib/foot_stats/response.rb', line 6

def initialize(options={})
  @resource_key    = options.fetch(:resource_key)
  @body            = options.fetch(:body)
  @stream_key      = options[:stream_key]
  parse_response
  check_stream
end

Instance Attribute Details

#bodyObject

Returns the value of attribute body.



3
4
5
# File 'lib/foot_stats/response.rb', line 3

def body
  @body
end

#parsed_responseObject

Returns the value of attribute parsed_response.



3
4
5
# File 'lib/foot_stats/response.rb', line 3

def parsed_response
  @parsed_response
end

#payloadObject

Returns the value of attribute payload.



3
4
5
# File 'lib/foot_stats/response.rb', line 3

def payload
  @payload
end

#resource_keyObject

Returns the value of attribute resource_key.



3
4
5
# File 'lib/foot_stats/response.rb', line 3

def resource_key
  @resource_key
end

Instance Method Details

#[](value) ⇒ Object

Attempt to fetch a key from resource.

Returns:

  • (Object)


67
68
69
# File 'lib/foot_stats/response.rb', line 67

def [](value)
  resource[value]
end

#collectArray Also known as: map

Collect all the resources evaluate from FootStats

Returns:

  • (Array)


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

def collect
  if resource
    resource.collect { |resource_value| yield(resource_value) }
  else
    []
  end
end

#errorErrorResponse

Return the error response object with the message if had errors.

Returns:



27
28
29
# File 'lib/foot_stats/response.rb', line 27

def error
  ErrorResponse.new(@parsed_response['Erro']['@Mensagem']) if error?
end

#error?True, False

Verifies if the response had errors.

Returns:

  • (True, False)


47
48
49
# File 'lib/foot_stats/response.rb', line 47

def error?
  @parsed_response['Erro'] != nil
end

#readedObject

Mark response as readed, storing payload at payload store



15
16
17
18
19
20
21
# File 'lib/foot_stats/response.rb', line 15

def readed
  if @stream_key
    stream.store @payload
  else
    true
  end
end

#resourceHash

Return the resource match by the resource key.

Returns:

  • (Hash)


55
56
57
58
59
60
61
# File 'lib/foot_stats/response.rb', line 55

def resource
  if @resource_key
    @parsed_response[@resource_key]
  else
    @parsed_response
  end
end

#updated?Boolean

Verifies if response is up-to-date

Returns:

  • (Boolean)


35
36
37
38
39
40
41
# File 'lib/foot_stats/response.rb', line 35

def updated?
  if @stream_key
    stream.updated?(@payload)
  else
    true
  end
end