Skip to content
This repository has been archived by the owner on Aug 2, 2021. It is now read-only.

[stock-search] IEX API Update #24

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 22 additions & 18 deletions stock-search/stock.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,18 @@
require 'uri'
require 'net/http'
require 'colorize'
require 'iex-ruby-client'

def stock_search

symbol = ARGV.join(" ")
token = ENV["IEX_API_TOKEN"]

if token.nil?
puts "Please export your IEX Cloud publishable API Token"
puts "See: https://iexcloud.io/docs/api/#authentication"
puts "export IEX_API_TOKEN='foo'"
exit(1)
end

# Takes user input and generates ticker symbol.
begin
Expand All @@ -21,26 +29,22 @@ def stock_search
exit(1)
end

# Use ticker symbol to fetch quote data
uri = URI("https://api.iextrading.com/1.0/stock/#{parsed_symbol}/quote")
response = Net::HTTP.get(uri)
if response == "Unknown symbol"
puts "Stock not found"
exit(1)
end
quote = JSON.parse(response)

client = IEX::Api::Client.new(
publishable_token: "#{token}",
Copy link

@almokhtarbr almokhtarbr Sep 13, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also here just like that publishable_token: token.to_s,

)
quote = client.quote("#{parsed_symbol}")

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can just make quote = client.quote(parsed_symbol)


# Shorten exchange string
case quote['primaryExchange']
when "Nasdaq Global Select"
case quote['primary_exchange']
when "NASDAQ"
exchange = "NASDAQ"
when "New York Stock Exchange"
exchange = "NYSE"
end

puts "=========================="
puts "Symbol: #{quote['symbol']}"
puts "Price: $#{quote['latestPrice']}"
puts "Price: $#{quote['latest_price']}"

# Color output for price change
if quote['change'].positive?
Expand All @@ -50,14 +54,14 @@ def stock_search
end

# Color output for percent price change
if quote['changePercent'].positive?
puts "Chg %: " + "+#{quote['changePercent']}%".green
if quote['change_percent'].positive?
puts "Chg %: " + "+#{quote['change_percent']}%".green
else
puts "Chg %: " + "#{quote['changePercent']}%".red
puts "Chg %: " + "#{quote['change_percent']}%".red
end

puts "PE Ratio: #{quote['peRatio']}"
puts "Mkt Cap: $#{quote['marketCap']}"
puts "PE Ratio: #{quote['pe_ratio']}"
puts "Mkt Cap: $#{quote['market_cap']}"
puts "Exchange: #{exchange}"
puts "=========================="
end
Expand Down