Class: Anoubis::RequestService
- Inherits:
-
Object
- Object
- Anoubis::RequestService
- Defined in:
- app/services/anoubis/request_service.rb
Overview
Request service
Instance Attribute Summary collapse
-
#cookies ⇒ Hash
Returns cookies data for current session.
-
#log ⇒ Object
Log service LogService.
-
#redis ⇒ Object
Redis database variable.
-
#redis_prefix ⇒ String
Returns Redis prefix for storing cache data.
Instance Method Summary collapse
-
#cookie_path ⇒ String
Returns redis path for storing cookies data.
-
#initialize(log = nil) ⇒ RequestService
constructor
Setups basic initialization parameters.
-
#parse_cookie(str) ⇒ Hash
Transform cookies string to hash.
-
#store(file_name, text) ⇒ Object
Store data to file.
-
#store_cookies ⇒ Object
Store current cookies to Redis cache.
-
#unzip(response) ⇒ String
Unzip RestClient response data if returned data is GZipped.
Constructor Details
#initialize(log = nil) ⇒ RequestService
Setups basic initialization parameters.
19 20 21 22 23 |
# File 'app/services/anoubis/request_service.rb', line 19 def initialize(log = nil) self.redis = Redis.new @cookies = nil self.log = log ? log : Anoubis::LogService.new end |
Instance Attribute Details
#cookies ⇒ Hash
Returns cookies data for current session
14 15 16 |
# File 'app/services/anoubis/request_service.rb', line 14 def @cookies end |
#log ⇒ Object
Log service LogService
11 12 13 |
# File 'app/services/anoubis/request_service.rb', line 11 def log @log end |
#redis ⇒ Object
Redis database variable
5 6 7 |
# File 'app/services/anoubis/request_service.rb', line 5 def redis @redis end |
#redis_prefix ⇒ String
Returns Redis prefix for storing cache data. Prefix can be set in Rails.configuration.anoubis_redis_prefix configuration parameter.
8 9 10 |
# File 'app/services/anoubis/request_service.rb', line 8 def redis_prefix @redis_prefix end |
Instance Method Details
#cookie_path ⇒ String
Returns redis path for storing cookies data
56 57 58 |
# File 'app/services/anoubis/request_service.rb', line 56 def redis_prefix + 'cookies' end |
#parse_cookie(str) ⇒ Hash
Transform cookies string to hash
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'app/services/anoubis/request_service.rb', line 102 def (str) = { } arr = str.split('; ') arr.each do |line| index = line.index '=' if index key = line[0..(index - 1)] value = line[(index + 1)..(line.length - 1)] end [key.to_s.to_sym] = value.to_s end end |
#store(file_name, text) ⇒ Object
Store data to file
92 93 94 95 96 |
# File 'app/services/anoubis/request_service.rb', line 92 def store(file_name, text) file = File.open(file_name, "w") file.write(text) file.close end |
#store_cookies ⇒ Object
Store current cookies to Redis cache
62 63 64 |
# File 'app/services/anoubis/request_service.rb', line 62 def redis.set , @cookies.to_json end |
#unzip(response) ⇒ String
Unzip RestClient response data if returned data is GZipped
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'app/services/anoubis/request_service.rb', line 70 def unzip(response) result = response.body begin if response.headers.key? :content_encoding if response.headers[:content_encoding] == 'gzip' sio = StringIO.new( response.body ) gz = Zlib::GzipReader.new( sio ) result = gz.read() end end rescue => e self.log 'Error was received when page encoded. ' + e.to_s, 'debug' result = nil end result end |