Class: FootStats::Request

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

Overview

Class responsible to make the request to the FootStats API

Examples:


Request.new('ListaCampeonatos')

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(resource, options = {}) ⇒ Request

Returns a new instance of Request.



11
12
13
14
15
16
# File 'lib/foot_stats/request.rb', line 11

def initialize(resource, options={})
  @resource_name = resource.resource_name
  @resource_key  = resource.resource_key
  @response_body = post(options)
  @logger        = Setup.logger
end

Instance Attribute Details

#loggerObject (readonly)

Returns the value of attribute logger.



9
10
11
# File 'lib/foot_stats/request.rb', line 9

def logger
  @logger
end

#resource_keyObject (readonly)

Returns the value of attribute resource_key.



9
10
11
# File 'lib/foot_stats/request.rb', line 9

def resource_key
  @resource_key
end

#resource_nameObject (readonly)

Returns the value of attribute resource_name.



9
10
11
# File 'lib/foot_stats/request.rb', line 9

def resource_name
  @resource_name
end

#response_bodyObject (readonly)

Returns the value of attribute response_body.



9
10
11
# File 'lib/foot_stats/request.rb', line 9

def response_body
  @response_body
end

Instance Method Details

#parse(options = {}) ⇒ Response

Parse the “XML”/ “JSON”.

Returns:



41
42
43
# File 'lib/foot_stats/request.rb', line 41

def parse(options = {})
  Response.new({resource_key: @resource_key, body: @response_body}.merge options)
end

#post(options) ⇒ String

Make the post request to the request url.

Returns:

  • (String)


30
31
32
33
34
35
# File 'lib/foot_stats/request.rb', line 30

def post(options)
  log "POST #{request_url}"
  response = RestClient.post(request_url, setup_params.merge(options))
  log "RESPONSE BODY:\n#{response}"
  response
end

#request_urlString

Return the request uri based on the resource.

Returns:

  • (String)


22
23
24
# File 'lib/foot_stats/request.rb', line 22

def request_url
  "#{Setup.base_url}/#{resource_name}"
end

#setup_paramsHash

Passing the setup params configured in your app.

Returns:

  • (Hash)


49
50
51
52
53
54
# File 'lib/foot_stats/request.rb', line 49

def setup_params
  {
    :Usuario => Setup.username,
    :Senha   => Setup.password
  }
end