Class: Anoubis::RequestService

Inherits:
Object
  • Object
show all
Defined in:
app/services/anoubis/request_service.rb

Overview

Request service

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(log = nil) ⇒ RequestService

Setups basic initialization parameters.

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

#cookiesHash

Returns cookies data for current session

Returns:

  • (Hash)

    Cookies data



14
15
16
# File 'app/services/anoubis/request_service.rb', line 14

def cookies
  @cookies
end

#logObject

Log service LogService



11
12
13
# File 'app/services/anoubis/request_service.rb', line 11

def log
  @log
end

#redisObject

Redis database variable



5
6
7
# File 'app/services/anoubis/request_service.rb', line 5

def redis
  @redis
end

#redis_prefixString

Returns Redis prefix for storing cache data. Prefix can be set in Rails.configuration.anoubis_redis_prefix configuration parameter.

Returns:

  • (String)

    Redis prefix



8
9
10
# File 'app/services/anoubis/request_service.rb', line 8

def redis_prefix
  @redis_prefix
end

Instance Method Details

Returns redis path for storing cookies data

Returns:

  • (String)

    Redis storing path



56
57
58
# File 'app/services/anoubis/request_service.rb', line 56

def cookie_path
  redis_prefix + 'cookies'
end

Transform cookies string to hash

Parameters:

  • str (String)

    Cookies string

Returns:

  • (Hash)

    Cookies data



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 parse_cookie(str)
  cookies = { }

  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
    cookies[key.to_s.to_sym] = value.to_s
  end

  cookies
end

#store(file_name, text) ⇒ Object

Store data to file

Parameters:

  • file_name (String)

    Name of file

  • text (String)

    Saved text



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_cookiesObject

Store current cookies to Redis cache



62
63
64
# File 'app/services/anoubis/request_service.rb', line 62

def store_cookies
  redis.set cookie_path, @cookies.to_json
end

#unzip(response) ⇒ String

Unzip RestClient response data if returned data is GZipped

Parameters:

Returns:

  • (String)

    Unzipped string



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