Class: EodData::WebService
- Inherits:
-
Object
- Object
- EodData::WebService
- Defined in:
- lib/eod_data/web_service.rb
Constant Summary collapse
- URI_BASE =
'http://ws.eoddata.com/data.asmx/'
Instance Method Summary collapse
-
#exchange_list ⇒ Object
Answers an array of exchanges represented as hashes.
-
#exchange_months(exchange) ⇒ Object
Answers the number of months of history that EODData authorises for the user to download.
-
#login ⇒ Object
Expects the environment to define a user name and password for use with the EODData API.
- #membership ⇒ Object
-
#post(uri_path, params = {}) {|response| ... } ⇒ Object
Sends a POST request to EODData.
-
#post_with_token(uri_path, params = {}) ⇒ Object
Sends an authenticated POST request to EODData web services.
-
#quote_list(exchange, symbols = nil) ⇒ Object
Answers end-of-day quotes either for an entire exchange or for a given list of symbols at a given exchange.
- #quote_list_by_date(exchange, date) ⇒ Object
-
#symbol_changes_by_exchange(exchange) ⇒ Object
Symbol changes have keys:.
- #symbol_list(exchange) ⇒ Object
-
#token ⇒ Object
Answers the current EODData authentication token or obtains a new token.
Instance Method Details
#exchange_list ⇒ Object
Answers an array of exchanges represented as hashes. Each hash has the following symbol keys, with their values automatically converted to appropriate types: date-times, integers and booleans.
:code
:name
:last_trade_date_time
:country
:currency
:advances
:declines
:suffix
:time_zone
:is_intraday
:intraday_start_date
:has_intraday_product
76 77 78 79 80 81 82 83 84 |
# File 'lib/eod_data/web_service.rb', line 76 def exchange_list post_with_token('ExchangeList', &raise_block_unless_success).exchanges.map do |exchange| exchange.convert_string_values!( date_time: %w(last_trade_date_time intraday_start_date).map(&:to_sym), integer: %w(advances declines).map(&:to_sym), boolean: %w(is_intraday has_intraday_product).map(&:to_sym), ) end end |
#exchange_months(exchange) ⇒ Object
Answers the number of months of history that EODData authorises for the user to download. Use the following block of Ruby code to collect the number of authorised months for all the available exchanges, where service is an EodData::WebService instance.
service.exchange_list.collect do |exchange|
exchange[:code]
end.collect do |code|
service.exchange_months(code)
end
51 52 53 54 55 56 57 |
# File 'lib/eod_data/web_service.rb', line 51 def exchange_months(exchange) post_with_token('ExchangeMonths', { 'Exchange' => exchange }, &raise_block_unless_success).months_text.to_i end |
#login ⇒ Object
Expects the environment to define a user name and password for use with the EODData API. However, when running within a Rails application, login first checks for an eod_data.yml configuration file in your Rails config folder; loading the user name and password from it if the file exists.
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 |
# File 'lib/eod_data/web_service.rb', line 160 def login if defined?(Rails) path = File.join(Rails.configuration.paths['config'].[0], 'eod_data.yml') if File.exist?(path) hash = YAML.load(File.open(path)) username = hash['username'] password = hash['password'] end end username ||= ENV['EODDATA_USERNAME'] password ||= ENV['EODDATA_PASSWORD'] post('Login', 'Username' => username, 'Password' => password) do |response| raise WebError, "Invalid login response #{response.name}" unless response.name == 'LOGINRESPONSE' end.token_string end |
#membership ⇒ Object
36 37 38 |
# File 'lib/eod_data/web_service.rb', line 36 def membership post_with_token('Membership', &raise_block_unless_success).membership_text end |
#post(uri_path, params = {}) {|response| ... } ⇒ Object
Sends a POST request to EODData. Answers a WebResponse object encapsulating the XML root element taken from the response body. This assumes that the body contains XML. Raises an exception if the HTML response code is not 200. After parsing the XML, sends the result to a block if given. Use this to validate the response and raise exceptions when invalid.
195 196 197 198 199 200 201 202 203 |
# File 'lib/eod_data/web_service.rb', line 195 def post(uri_path, params={}) response = Net::HTTP.post_form(URI.join(URI_BASE, uri_path), params) raise WebError, "Invalid response code #{response.code}" unless response.code.to_i == 200 response = WebResponse.new(Nokogiri::XML(response.body) do |config| config.strict.nonet end.root) yield response if block_given? response end |
#post_with_token(uri_path, params = {}) ⇒ Object
Sends an authenticated POST request to EODData web services. Answers an XML root element encapsulated by a WebResponse object if successful.
185 186 187 |
# File 'lib/eod_data/web_service.rb', line 185 def post_with_token(uri_path, params={}) post(uri_path, params.merge({'Token' => token})) end |
#quote_list(exchange, symbols = nil) ⇒ Object
Answers end-of-day quotes either for an entire exchange or for a given list of symbols at a given exchange. Answers an array of hashes, each representing an individual quote with keys as follows.
:symbol
:description
:name
:date_time
:open
:high
:low
:close
:volume
:open_interest
:previous
:change
:bid
:ask
:previous_close
:next_open
:modified
136 137 138 139 140 141 142 143 144 145 |
# File 'lib/eod_data/web_service.rb', line 136 def quote_list(exchange, symbols=nil) uri_path = 'QuoteList' params = {'Exchange' => exchange} symbols = [] if symbols.nil? unless symbols.empty? uri_path << '2' params['Symbols'] = symbols.join(',') end post_with_token(uri_path, params, &raise_block_unless_success).quotes end |
#quote_list_by_date(exchange, date) ⇒ Object
147 148 149 150 151 152 153 154 |
# File 'lib/eod_data/web_service.rb', line 147 def quote_list_by_date(exchange, date) post_with_token('QuoteListByDate', { 'Exchange' => exchange, 'QuoteDate' => date.strftime('%Y%m%d') }, &raise_block_unless_success).quotes end |
#symbol_changes_by_exchange(exchange) ⇒ Object
Symbol changes have keys:
:date_time
:old_symbol
:new_symbol
:exchange_code
:new_exchange_code
106 107 108 109 110 111 112 |
# File 'lib/eod_data/web_service.rb', line 106 def symbol_changes_by_exchange(exchange) post_with_token('SymbolChangesByExchange', { 'Exchange' => exchange }, &raise_block_unless_success).symbol_changes end |
#symbol_list(exchange) ⇒ Object
86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/eod_data/web_service.rb', line 86 def symbol_list(exchange) post_with_token('SymbolList', { 'Exchange' => exchange }, &raise_block_unless_success).symbols.map do |symbol| symbol.convert_string_values!( date_time: %w(date_time).map(&:to_sym) ) end end |
#token ⇒ Object
Answers the current EODData authentication token or obtains a new token.
179 180 181 |
# File 'lib/eod_data/web_service.rb', line 179 def token @token ||= login end |