Class: OptaSD::Core

Inherits:
Object show all
Defined in:
lib/opta_sd/core.rb

Direct Known Subclasses

Soccer::SoccerCore

Constant Summary collapse

PARAMETERS =
YAML::load(File.open(File.expand_path('../../../config/parameters.yml', __FILE__)))

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCore

The Initialize



9
10
11
12
# File 'lib/opta_sd/core.rb', line 9

def initialize
  set_configuration
  @params = Hash.new
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



6
7
8
# File 'lib/opta_sd/core.rb', line 6

def data
  @data
end

Instance Method Details

#getObject

Build the Request and get the Data



15
16
17
18
19
# File 'lib/opta_sd/core.rb', line 15

def get
  response = Net::HTTP.get(build_uri)
  @data = parse_data(response)
  self
end

#parse_data(response) ⇒ Object

Parse The Response



22
23
24
25
26
27
# File 'lib/opta_sd/core.rb', line 22

def parse_data(response)
  case @params[:_fmt]
  when 'json' then parse_json(response)
  when 'xml'  then parse_xml(response)
  else response end
end

#parse_json(response) ⇒ Object

Parse JSON Response



30
31
32
33
34
# File 'lib/opta_sd/core.rb', line 30

def parse_json(response)
  data = JSON.parse(response)
  fail OptaSD::Error.new(data), ErrorMessage.get_message(data['errorCode'].to_i) if data['errorCode']
  data
end

#parse_xml(response) ⇒ Object

Parse XML Response



37
38
39
40
41
# File 'lib/opta_sd/core.rb', line 37

def parse_xml(response)
  data = Nokogiri::XML(response) do |config| config.strict.noblanks end
  fail OptaSD::Error.new(xml_error_to_hast(data)), ErrorMessage.get_message(data.children.first.content.to_i) if data.css('errorCode').first.present?
  data
end