Module: Yfinrb::YfConnection
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
- #get(url, headers = nil, params = nil) ⇒ Object (also: #cache_get)
- #get_raw_json(url, user_agent_headers = nil, params = nil) ⇒ Object
- #yfconn_initialize ⇒ Object
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? , crumb, strategy = () 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? = ::HTTParty::CookieHash.new .(@@cookie) = { headers: headers.dup.merge(@@user_agent_headers).merge({ 'cookie' => ., '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, ) # 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_initialize ⇒ Object
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 |