Module: Yfinrb::YfConnection

Extended by:
ActiveSupport::Concern
Included in:
Ticker
Defined in:
lib/yfinrb/yf_connection.rb

Constant Summary collapse

@@user_agent_headers =

“”“ Have one place to retrieve data from Yahoo API in order to ease caching and speed up operations. ”“”

{
  'User-Agent' => 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36'
  # 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/125.0.0.0 Safari/537.36'
}
@@proxy =

‘Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/125.0.0.0 Safari/537.36’

nil

Instance Method Summary collapse

Instance Method Details

#get(url, headers = nil, params = nil) ⇒ Object Also known as: cache_get



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/yfinrb/yf_connection.rb', line 42

def get(url, headers=nil, params=nil)
  # Important: treat input arguments as immutable.
  # Rails.logger.info { "#{__FILE__}:#{__LINE__} url = #{url}, headers = #{headers}, params=#{params.inspect}" }

  headers ||= {}
  params ||= {}
  params.merge!(crumb: @@crumb) unless @@crumb.nil?
  cookie, crumb, strategy = _get_cookie_and_crumb()
  crumbs = !crumb.nil? ? {'crumb' => crumb} : {}

  request_args = {
    url: url,
    params: params.merge(crumbs),
    headers: headers || {}
  }

  proxy = _get_proxy
  ::HTTParty.http_proxy(addr = proxy.split(':').first, port = proxy.split(':').second.split('/').first) unless proxy.nil?

  cookie_hash = ::HTTParty::CookieHash.new
  cookie_hash.add_cookies(@@cookie)
  options = { headers: headers.dup.merge(@@user_agent_headers).merge({ 'cookie' => cookie_hash.to_cookie_string, 'crumb' => crumb })} #,  debug_output: STDOUT }

  u = (request_args[:url]).dup.to_s
  joiner = ('?'.in?(request_args[:url]) ? '&' : '?')
  u += (joiner + CGI.unescape(request_args[:params].to_query)) unless request_args[:params].empty?

  # Rails.logger.info { "#{__FILE__}:#{__LINE__} u=#{u}, options = #{options.inspect}" }
  response = ::HTTParty.get(u, options)
  # Rails.logger.info { "#{__FILE__}:#{__LINE__} response=#{response.inspect}" }

  return response
end

#get_raw_json(url, user_agent_headers = nil, params = nil) ⇒ Object



79
80
81
82
83
84
85
# File 'lib/yfinrb/yf_connection.rb', line 79

def get_raw_json(url, user_agent_headers=nil, params=nil)
  # Rails.logger.info { "#{__FILE__}:#{__LINE__} url = #{url.inspect}" }
  response = get(url, user_agent_headers, params)
  # Rails.logger.info { "#{__FILE__}:#{__LINE__} response = #{response.inspect}" }
  # response.raise_for_status()
  return response   #.json()
end

#yfconn_initializeObject



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/yfinrb/yf_connection.rb', line 25

def yfconn_initialize
  # Rails.logger.info { "#{__FILE__}:#{__LINE__} here"}
  begin
    @@zache = ::Zache.new
    @@session_is_caching = true
  rescue NoMethodError
    # Not caching
    @@session_is_caching = false
  end

  @@crumb = nil
  @@cookie = nil
  @@cookie_strategy = 'basic'
  @@cookie_lock = ::Mutex.new()
end