Class: Intrinio::API
Overview
Provides access to all the Intrinio API endpoints
Instance Attribute Summary collapse
-
#opts ⇒ Object
readonly
Returns the value of attribute opts.
Attributes inherited from WebAPI
#after_request_block, #base_url, #debug, #format, #last_error
Instance Method Summary collapse
- #get_csv(*args) ⇒ Object
-
#initialize(opts = {}) ⇒ API
constructor
A new instance of API.
- #save_csv(file, *args) ⇒ Object
Methods inherited from WebAPI
#after_request, #cache, #construct_url, #default_params, #get, #get!, #method_missing, #param, #save
Constructor Details
#initialize(opts = {}) ⇒ API
Returns a new instance of API.
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/intrinio/api.rb', line 9 def initialize(opts={}) if opts[:auth] opts[:username], opts[:password] = opts[:auth].split ':' opts.delete :auth end defaults = { username: nil, password: nil, use_cache: false, cache_dir: nil, cache_life: nil, base_url: "https://api.intrinio.com" } opts = defaults.merge! opts @opts = opts username, password = opts[:username], opts[:password] cache.disable unless opts[:use_cache] cache.dir = opts[:cache_dir] if opts[:cache_dir] cache.life = opts[:cache_life] if opts[:cache_life] cache.[:http_basic_authentication] = [username, password] after_request do |response| begin JSON.parse response, symbolize_names: true rescue JSON::ParserError response end end super opts[:base_url] end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method in the class Intrinio::WebAPI
Instance Attribute Details
#opts ⇒ Object (readonly)
Returns the value of attribute opts.
7 8 9 |
# File 'lib/intrinio/api.rb', line 7 def opts @opts end |
Instance Method Details
#get_csv(*args) ⇒ Object
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/intrinio/api.rb', line 45 def get_csv(*args) result = get *args raise Intrinio::BadResponse, "API said '#{result}'" if result.is_a? String raise Intrinio::BadResponse, "Got a #{result.class},expected a Hash" unless result.is_a? Hash raise Intrinio::IncompatibleResponse, "There is no data attribute in the response" unless result.has_key? :data data = result[:data] header = data.first.keys result = CSV.generate do |csv| csv << header data.each { |row| csv << row.values } end result end |
#save_csv(file, *args) ⇒ Object
63 64 65 66 |
# File 'lib/intrinio/api.rb', line 63 def save_csv(file, *args) data = get_csv *args File.write file, data end |