Class: FbRuby::Utils::Session

Inherits:
Object
  • Object
show all
Defined in:
lib/FbRuby/utils.rb

Overview

requests Session

Constant Summary collapse

@@default_user_agent =
"Mozilla/5.0 (Linux; Android 9; SM-N976V) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.89 Mobile Safari/537.36"
@@headers =
{'Accept-Encoding'=> 'gzip, deflate', 'Accept'=> '*/*', 'Connection'=> 'keep-alive'}
@@facebook_exceptions =
true

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(headers = {}, cookies = {}) ⇒ Session

Returns a new instance of Session.



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/FbRuby/utils.rb', line 107

def initialize(headers = {}, cookies = {})
  @headers = headers.empty? ? @@headers : headers
  
  if cookies.instance_of?(Hash)
    @cookies = cookies
  else
    @cookies = FbRuby::Utils::parse_cookies_header(cookies)
  end
  
  @options = {
  verify_ssl: true,
  max_redirects: 10,
  timeout: 30,
  open_timeout: 30,
  user_agent: @headers.member?('user-agent') ? @headers['user-agent'] : @@default_user_agent,
  follow_redirect: true}
end

Instance Attribute Details

#cookiesObject (readonly)

Returns the value of attribute cookies.



105
106
107
# File 'lib/FbRuby/utils.rb', line 105

def cookies
  @cookies
end

#headersObject (readonly)

Returns the value of attribute headers.



105
106
107
# File 'lib/FbRuby/utils.rb', line 105

def headers
  @headers
end

#optionsObject (readonly)

Returns the value of attribute options.



105
106
107
# File 'lib/FbRuby/utils.rb', line 105

def options
  @options
end

Class Method Details

.default_headersObject



96
97
98
# File 'lib/FbRuby/utils.rb', line 96

def default_headers
  return @@headers
end

.default_user_agentObject



92
93
94
# File 'lib/FbRuby/utils.rb', line 92

def default_user_agent
  return @@default_user_agent
end

.facebook_exceptionsObject



100
101
102
# File 'lib/FbRuby/utils.rb', line 100

def facebook_exceptions
  return @@facebook_exceptions
end

Instance Method Details

#delete(url, headers = {}, options = {}) ⇒ Object



142
143
144
# File 'lib/FbRuby/utils.rb', line 142

def delete(url, headers = {}, options = {})
  request(:delete, url, {}, headers, nil, options)
end

#get(url, params = {}, headers = {}, options = {}) ⇒ Object



125
126
127
128
129
130
131
132
# File 'lib/FbRuby/utils.rb', line 125

def get(url, params = {}, headers = {}, options = {})
  if params.length > 0
    url = URI(url.to_s)
    url.query = URI.encode_www_form(params.to_a)
  end

  request(:get, url, {}, headers, nil, options)
end


166
167
168
169
170
171
172
# File 'lib/FbRuby/utils.rb', line 166

def get_cookie_dict
  if @cookies.instance_of? (Hash)
    return @cookies
  else
    return FbRuby::Utils::parse_cookies_header(@cookies)
  end
end


174
175
176
# File 'lib/FbRuby/utils.rb', line 174

def get_cookie_hash
  return get_cookie_dict
end


162
163
164
# File 'lib/FbRuby/utils.rb', line 162

def get_cookie_str
  return FbRuby::Utils::cookie_hash_to_str(@cookies)
end

#get_without_sessions(url) ⇒ Object



150
151
152
# File 'lib/FbRuby/utils.rb', line 150

def get_without_sessions(url)
  return RestClient.get(url.to_s, cookies: @cookies)
end

#head(url, headers = {}, options = {}) ⇒ Object



146
147
148
# File 'lib/FbRuby/utils.rb', line 146

def head(url, headers = {}, options = {})
  request(:head, url, {}, headers, nil, options)
end

#post(url, data = {}, headers = {}, options = {}) ⇒ Object



134
135
136
# File 'lib/FbRuby/utils.rb', line 134

def post(url, data = {}, headers = {}, options = {})
  request(:post, url, {}, headers, data, options)
end

#post_without_sessions(url, data = {}) ⇒ Object



154
155
156
157
158
159
160
# File 'lib/FbRuby/utils.rb', line 154

def post_without_sessions(url, data = {})
  begin 
    return RestClient.post(url.to_s, data, cookies: @cookies)
  rescue RestClient::Found => err
    return get_without_sessions(err.response.headers[:location])
  end
end

#put(url, data = {}, headers = {}, options = {}) ⇒ Object



138
139
140
# File 'lib/FbRuby/utils.rb', line 138

def put(url, data = {}, headers = {}, options = {})
  request(:put, url, {}, headers, data, options)
end