Class: Acapela::Response

Inherits:
Object
  • Object
show all
Defined in:
lib/acapela/response.rb,
lib/acapela/mocks/response.rb

Constant Summary collapse

RESPONSE_STATE =
'res'
RESPONSE_STATE_REGEXP =
/#{RESPONSE_STATE}=/
RESPONSE_SOUND_URL =
'snd_url'
RESPONSE_STATE_OK =
'OK'
RESPONSE_ERROR_MESSAGE =
'err_msg'
RESPONSE_ERROR_CODE =
'err_code'
ERROR_UNEXPECTED_RESPONSE =
ServiceError.new("Unexpected response.")
ERROR_MISSING_SOUND_URL =
ServiceError.new("Parameters are missing sound url.")

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(response) ⇒ Response

Returns a new instance of Response.



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/acapela/response.rb', line 23

def initialize(response)
  if RESPONSE_STATE_REGEXP.match(response)
    params = CGI::parse(response)
  else
    raise ERROR_UNEXPECTED_RESPONSE
  end

  if RESPONSE_STATE_OK == params[RESPONSE_STATE].first
    if url = params[RESPONSE_SOUND_URL]
      @url = URI.parse(url.first)
      @id = params['snd_id'].first
      @time = params['snd_time'].first.to_f
      @size = params['snd_size'].first.to_i
    else
      raise ERROR_MISSING_SOUND_URL
    end
  else
    raise ServiceError.new(params[RESPONSE_ERROR_MESSAGE].first, params[RESPONSE_ERROR_CODE].first)
  end
end

Instance Attribute Details

#idObject (readonly)

Returns the value of attribute id.



8
9
10
# File 'lib/acapela/response.rb', line 8

def id
  @id
end

#sizeObject (readonly)

Returns the value of attribute size.



8
9
10
# File 'lib/acapela/response.rb', line 8

def size
  @size
end

#timeObject (readonly)

Returns the value of attribute time.



8
9
10
# File 'lib/acapela/response.rb', line 8

def time
  @time
end

#urlObject (readonly)

Returns the value of attribute url.



8
9
10
# File 'lib/acapela/response.rb', line 8

def url
  @url
end

Class Method Details

.mock_offObject



12
13
14
15
16
17
18
# File 'lib/acapela/mocks/response.rb', line 12

def self.mock_off
  self.class_eval(<<-EVAL, __FILE__, __LINE__)
   def fetch_file_from_url
     fetch_file_from_url_without_mock
   end
  EVAL
end

.mock_onObject



4
5
6
7
8
9
10
# File 'lib/acapela/mocks/response.rb', line 4

def self.mock_on
  self.class_eval(<<-EVAL, __FILE__, __LINE__)
   def fetch_file_from_url
     fetch_file_from_url_with_mock
   end
  EVAL
end

Instance Method Details

#download_to_tempfileObject



44
45
46
47
48
49
50
# File 'lib/acapela/response.rb', line 44

def download_to_tempfile
  content = fetch_file_from_url
  file = Tempfile.new(self.id)
  file.write(content)
  file.flush
  file # Leaving open. Will be closed once object is finalized.
end