Class: TradingviewScreener::Client
- Inherits:
-
Object
- Object
- TradingviewScreener::Client
- Defined in:
- lib/tradingview_screener/client.rb
Constant Summary collapse
- DEFAULT_HEADERS =
{ "accept" => "application/json, text/plain, */*; q=0.01", "content-type" => "text/plain;charset=UTF-8", "origin" => "https://www.tradingview.com", "referer" => "https://www.tradingview.com/", "user-agent" => "tradingview-screener-rb/#{VERSION}" }.freeze
Instance Attribute Summary collapse
-
#cookies ⇒ Object
readonly
Returns the value of attribute cookies.
-
#headers ⇒ Object
readonly
Returns the value of attribute headers.
-
#label_product ⇒ Object
readonly
Returns the value of attribute label_product.
-
#proxy ⇒ Object
readonly
Returns the value of attribute proxy.
-
#timeout ⇒ Object
readonly
Returns the value of attribute timeout.
Instance Method Summary collapse
- #get(url) ⇒ Object
-
#initialize(timeout: 20, cookies: nil, headers: nil, label_product: "screener-stock", proxy: nil) ⇒ Client
constructor
A new instance of Client.
- #perform(uri, request) ⇒ Object
- #post(url, payload) ⇒ Object
Constructor Details
#initialize(timeout: 20, cookies: nil, headers: nil, label_product: "screener-stock", proxy: nil) ⇒ Client
Returns a new instance of Client.
31 32 33 34 35 36 37 |
# File 'lib/tradingview_screener/client.rb', line 31 def initialize(timeout: 20, cookies: nil, headers: nil, label_product: "screener-stock", proxy: nil) @timeout = timeout @cookies = () @headers = DEFAULT_HEADERS.merge(stringify_keys(headers || {})) @label_product = label_product @proxy = normalize_proxy(proxy) end |
Instance Attribute Details
#cookies ⇒ Object (readonly)
Returns the value of attribute cookies.
29 30 31 |
# File 'lib/tradingview_screener/client.rb', line 29 def @cookies end |
#headers ⇒ Object (readonly)
Returns the value of attribute headers.
29 30 31 |
# File 'lib/tradingview_screener/client.rb', line 29 def headers @headers end |
#label_product ⇒ Object (readonly)
Returns the value of attribute label_product.
29 30 31 |
# File 'lib/tradingview_screener/client.rb', line 29 def label_product @label_product end |
#proxy ⇒ Object (readonly)
Returns the value of attribute proxy.
29 30 31 |
# File 'lib/tradingview_screener/client.rb', line 29 def proxy @proxy end |
#timeout ⇒ Object (readonly)
Returns the value of attribute timeout.
29 30 31 |
# File 'lib/tradingview_screener/client.rb', line 29 def timeout @timeout end |
Instance Method Details
#get(url) ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/tradingview_screener/client.rb', line 39 def get(url) uri = URI(url) request = Net::HTTP::Get.new(uri) headers.each { |key, value| request[key] = value } # HTML pages prefer browser-like accept; keep caller override if provided. = request["cookie"] = unless .nil? || .empty? response = perform(uri, request) body = response.body.to_s unless response.is_a?(Net::HTTPSuccess) raise RequestError.new( "request failed: HTTP #{response.code} #{response.}", status: response.code.to_i, body: body ) end body end |
#perform(uri, request) ⇒ Object
82 83 84 85 86 87 88 |
# File 'lib/tradingview_screener/client.rb', line 82 def perform(uri, request) if socks_proxy? request_via_socks(uri, request) else build_http(uri).request(request) end end |
#post(url, payload) ⇒ Object
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/tradingview_screener/client.rb', line 59 def post(url, payload) uri = build_uri(url) request = Net::HTTP::Post.new(uri) headers.each { |key, value| request[key] = value } = request["cookie"] = unless .nil? || .empty? request.body = JSON.generate(payload) response = perform(uri, request) body = response.body.to_s unless response.is_a?(Net::HTTPSuccess) raise RequestError.new( "scanner request failed: HTTP #{response.code} #{response.}", status: response.code.to_i, body: body ) end JSON.parse(body) rescue JSON::ParserError => e raise RequestError.new("invalid scanner JSON: #{e.}", status: response&.code&.to_i, body: body) end |