intrinio 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 55d5ef7008a5e1984530c0b3f2b47fa58453505c
4
+ data.tar.gz: 1a4a941a3b2218fb3f20e2193ecca822bc0717cd
5
+ SHA512:
6
+ metadata.gz: cbe11b914de9f015cf022bf076e0cce9de9a8cf1681c860be35981af8c978c6e8577fb1d72d995e6d9523edc883b0a2d732366bd951b226f77e5a8d2141bd3bb
7
+ data.tar.gz: 15e067f9bafbaedf8c51cb2c0b1f27e53574dfadadbd9f5219fe50eaf3f99b0b47966a04cd1380894201ea534cfdb43525e4af17a03bafb58c5b6898957a9e90
data/README.md ADDED
@@ -0,0 +1,149 @@
1
+ Intrinio API Library and Command Line
2
+ ==================================================
3
+
4
+ [![Gem](https://img.shields.io/gem/v/intrinio.svg?style=flat-square)](https://rubygems.org/gems/intrinio)
5
+ [![Travis](https://img.shields.io/travis/DannyBen/intrinio.svg?style=flat-square)](https://travis-ci.org/DannyBen/intrinio)
6
+ [![Code Climate](https://img.shields.io/codeclimate/github/DannyBen/intrinio.svg?style=flat-square)](https://codeclimate.com/github/DannyBen/intrinio)
7
+ [![Gemnasium](https://img.shields.io/gemnasium/DannyBen/intrinio.svg?style=flat-square)](https://gemnasium.com/DannyBen/intrinio)
8
+
9
+ ---
10
+
11
+ This gem provides both a Ruby library and a command line interface for the
12
+ [Intrinio][1] data service.
13
+
14
+ ---
15
+
16
+ Install
17
+ --------------------------------------------------
18
+
19
+ ```
20
+ $ gem install intrinio
21
+ ```
22
+
23
+ Or with bundler:
24
+
25
+ ```ruby
26
+ gem 'intrinio'
27
+ ```
28
+
29
+ Features
30
+ --------------------------------------------------
31
+
32
+ * Easy to use interface.
33
+ * Use as a library or through the command line.
34
+ * Access any Intrinio endpoint directly.
35
+ * Display output in various formats.
36
+ * Save output to a file.
37
+ * Includes a built in file cache (disabled by default).
38
+
39
+ Usage
40
+ --------------------------------------------------
41
+
42
+ First, require and initialize with your username and password.
43
+
44
+ ```ruby
45
+ require 'intrinio'
46
+ intrinio = Intrinio::API.new username: 'me', password: 'secret'
47
+ ```
48
+
49
+ Now, you can access any Intrinio endpoint with any optional parameter, like
50
+ this:
51
+
52
+ ```ruby
53
+ result = intrinio.get "indices", type: 'economic', page_size: 5
54
+ ```
55
+
56
+ In addition, for convenience, you can use the first part of the endpoint as
57
+ a method name, like this:
58
+
59
+ ```ruby
60
+ result = intrinio.indices type: 'economic', page_size: 5
61
+ ```
62
+
63
+ By default, you will get a ruby hash in return. If you wish to get the raw
64
+ output, you can use the `get!` method:
65
+
66
+ ```ruby
67
+ result = intrinio.get! "indices", type: 'economic', page_size: 5
68
+ # => JSON string
69
+ ```
70
+
71
+ To save the output directly to a file, use the `save` method:
72
+
73
+ ```ruby
74
+ intrinio.save "filename.json", "indices", type: 'economic', page_size: 5
75
+ ```
76
+
77
+ Debugging your request and adding "sticky" query parameters that stay with
78
+ you for the following requests is also easy:
79
+
80
+ ```ruby
81
+ intrinio.debug = true
82
+ intrinio.param page_size: 10, order_direction: 'asc'
83
+ puts intrinio.historical_data identifier: '$INTDSRUSM193N', item: 'level'
84
+ # => "https://api.intrinio.com/historical_data?page_size=10&order_direction=asc&identifier=%24INTDSRUSM193N&item=level
85
+
86
+ intrinio.param page_size: nil # remove param
87
+ ```
88
+
89
+ Command Line
90
+ --------------------------------------------------
91
+
92
+ The command line utility `intrinio` acts in a similar way. To use your
93
+ Intrinio authentication, simply set it in the environment variables
94
+ `INTRINIO_AUTH`:
95
+
96
+ `$ export INTRINIO_AUTH=username:password`
97
+
98
+ These commands are available:
99
+
100
+ `$ intrinio get PATH [PARAMS...]` - print the output.
101
+ `$ intrinio pretty PATH [PARAMS...]` - print a pretty JSON.
102
+ `$ intrinio see PATH [PARAMS...]` - print a colored output.
103
+ `$ intrinio url PATH [PARAMS...]` - show the constructed URL.
104
+ `$ intrinio save FILE PATH [PARAMS...]` - save the output to a file.
105
+
106
+ Run `intrinio --help` for more information, or view the [full usage help][2].
107
+
108
+ Caching
109
+ --------------------------------------------------
110
+
111
+ We are using the [WebCache][3] gem for automatic HTTP caching.
112
+ To take the path of least surprises, caching is disabled by default.
113
+
114
+ You can enable and customize it by either passing options on
115
+ initialization, or by accessing the `WebCache` object directly at
116
+ a later stage.
117
+
118
+ ```ruby
119
+ intrinio = Intrinio::API.new username: user, password: pass,
120
+ use_cache: true
121
+
122
+ intrinio = Intrinio::API.new username: user, password: pass,
123
+ use_cache: true, cache_dir: 'tmp'
124
+
125
+ intrinio = Intrinio::API.new username: user, password: pass,
126
+ use_cache: true, cache_life: 120
127
+
128
+ # or
129
+
130
+ intrinio = Intrinio::API.new username: user, password: pass
131
+ intrinio.cache.enable
132
+ intrinio.cache.dir = 'tmp/cache' # Change cache folder
133
+ intrinio.cache.life = 120 # Change cache life to 2 minutes
134
+ ```
135
+
136
+ To enable caching for the command line, simply set one or both of
137
+ these environment variables:
138
+
139
+ ```
140
+ $ export INTRINIO_CACHE_DIR=cache # default: 'cache'
141
+ $ export INTRINIO_CACHE_LIFE=120 # default: 3600 (1 hour)
142
+ $ intrinio get indices
143
+ # => This call will be cached
144
+ ```
145
+
146
+
147
+ [1]: https://www.intrinio.com
148
+ [2]: https://github.com/DannyBen/intrinio/blob/master/lib/intrinio/docopt.txt
149
+ [3]: https://github.com/DannyBen/webcache
data/bin/intrinio ADDED
@@ -0,0 +1,4 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'intrinio'
4
+ Intrinio::CommandLine.instance.execute ARGV
data/lib/intrinio.rb ADDED
@@ -0,0 +1,4 @@
1
+ require 'intrinio/version'
2
+ require 'intrinio/web_api'
3
+ require 'intrinio/api'
4
+ require 'intrinio/command_line'
@@ -0,0 +1,39 @@
1
+ require 'json'
2
+
3
+ module Intrinio
4
+ # Provides access to all the Intrinio API endpoints
5
+ class API < WebAPI
6
+ attr_reader :opts
7
+
8
+ def initialize(opts={})
9
+ defaults = {
10
+ username: nil,
11
+ password: nil,
12
+ use_cache: false,
13
+ cache_dir: nil,
14
+ cache_life: nil,
15
+ base_url: "https://api.intrinio.com"
16
+ }
17
+
18
+ opts = defaults.merge! opts
19
+ @opts = opts
20
+
21
+ username, password = opts[:username], opts[:password]
22
+
23
+ cache.disable unless opts[:use_cache]
24
+ cache.dir = opts[:cache_dir] if opts[:cache_dir]
25
+ cache.life = opts[:cache_life] if opts[:cache_life]
26
+ cache.options[:http_basic_authentication] = [username, password]
27
+
28
+ after_request do |response|
29
+ begin
30
+ JSON.parse response, symbolize_names: true
31
+ rescue JSON::ParserError
32
+ response
33
+ end
34
+ end
35
+
36
+ super opts[:base_url]
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,116 @@
1
+ require 'singleton'
2
+ require 'docopt'
3
+ require 'json'
4
+ require 'awesome_print'
5
+
6
+ module Intrinio
7
+
8
+ # Handles the command line interface
9
+ class CommandLine
10
+ include Singleton
11
+
12
+ # Gets an array of arguments (e.g. ARGV), executes the command if valid
13
+ # and shows usage patterns / help otherwise.
14
+ def execute(argv=[])
15
+ doc = File.read File.dirname(__FILE__) + '/docopt.txt'
16
+ begin
17
+ args = Docopt::docopt(doc, argv: argv, version: VERSION)
18
+ handle args
19
+ rescue Docopt::Exit => e
20
+ puts e.message
21
+ end
22
+ end
23
+
24
+ def intrinio
25
+ @intrinio ||= intrinio!
26
+ end
27
+
28
+ private
29
+
30
+ def intrinio!
31
+ Intrinio::API.new options
32
+ end
33
+
34
+ # Called when the arguments match one of the usage patterns. Will
35
+ # delegate action to other, more specialized methods.
36
+ def handle(args)
37
+ path = args['PATH']
38
+ params = args['PARAMS']
39
+ file = args['FILE']
40
+
41
+ return get(path, params) if args['get']
42
+ return pretty(path, params) if args['pretty']
43
+ return see(path, params) if args['see']
44
+ return url(path, params) if args['url']
45
+ return save(file, path, params) if args['save']
46
+ end
47
+
48
+ def get(path, params)
49
+ puts intrinio.get! path, translate_params(params)
50
+ end
51
+
52
+ def save(file, path, params)
53
+ success = intrinio.save file, path, translate_params(params)
54
+ puts success ? "Saved #{file}" : "Saving failed"
55
+ end
56
+
57
+ def pretty(path, params)
58
+ intrinio.format = :json
59
+ response = intrinio.get path, translate_params(params)
60
+ puts JSON.pretty_generate response
61
+ end
62
+
63
+ def see(path, params)
64
+ ap intrinio.get path, translate_params(params)
65
+ end
66
+
67
+ def url(path, params)
68
+ intrinio.debug = true
69
+ puts intrinio.get path, translate_params(params)
70
+ intrinio.debug = false
71
+ end
72
+
73
+ # Convert a params array like [key:value, key:value] to a hash like
74
+ # {key: value, key: value}
75
+ def translate_params(params)
76
+ return nil if params.empty?
77
+ result = {}
78
+ params.each do |param|
79
+ key, value = param.split ':'
80
+ result[key] = value
81
+ end
82
+ result
83
+ end
84
+
85
+ def options
86
+ result = { username: username, password: password }
87
+ return result unless cache_dir || cache_life
88
+
89
+ result[:use_cache] = true
90
+ result[:cache_dir] = cache_dir if cache_dir
91
+ result[:cache_life] = cache_life.to_i if cache_life
92
+ result
93
+ end
94
+
95
+ def username
96
+ @username ||= intrinio_auth.split(':').first
97
+ end
98
+
99
+ def password
100
+ @password ||= intrinio_auth.split(':').last
101
+ end
102
+
103
+ def intrinio_auth
104
+ ENV['INTRINIO_AUTH'] || ''
105
+ end
106
+
107
+ def cache_dir
108
+ ENV['INTRINIO_CACHE_DIR']
109
+ end
110
+
111
+ def cache_life
112
+ ENV['INTRINIO_CACHE_LIFE']
113
+ end
114
+
115
+ end
116
+ end
@@ -0,0 +1,38 @@
1
+ Intrinio
2
+
3
+ Usage:
4
+ intrinio get PATH [PARAMS...]
5
+ intrinio pretty PATH [PARAMS...]
6
+ intrinio see PATH [PARAMS...]
7
+ intrinio url PATH [PARAMS...]
8
+ intrinio save FILE PATH [PARAMS...]
9
+ intrinio (-h|--help|--version)
10
+
11
+ Commands:
12
+ get
13
+ Downloads data and prints it to screen as it.
14
+
15
+ pretty
16
+ Downloads data and prints it as a prettified JSON output.
17
+
18
+ see
19
+ Downloads data and awesome-prints it.
20
+
21
+ url
22
+ Shows the URL constructed from the request.
23
+
24
+ save
25
+ Downloads data and saves it to a file.
26
+
27
+ Parameters:
28
+ PATH:
29
+ This is the Intrinio API path, without the query string.
30
+ For example: `indices` or `historical_data`.
31
+
32
+ PARAMS:
33
+ An optional list of query string parameters, separated by a space, to
34
+ send with the request. Each parameter should be in the format of
35
+ key:value, for example: page:2 per_page:10
36
+
37
+ FILE:
38
+ Path to the output file.
@@ -0,0 +1,3 @@
1
+ module Intrinio
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,93 @@
1
+ require 'uri'
2
+ require 'open-uri'
3
+ require 'webcache'
4
+
5
+ module Intrinio
6
+
7
+ # A general purpose HTTP wrapper. This is poor man's HTTParty with
8
+ # dynamic methods.
9
+ class WebAPI
10
+ attr_reader :base_url, :after_request_block, :last_error
11
+ attr_accessor :debug, :format
12
+
13
+ def initialize(base_url)
14
+ @base_url = base_url
15
+ end
16
+
17
+ # Allow using any method as the first segment of the path
18
+ # object.user 'details' becomes object.get 'user/details'
19
+ def method_missing(method_sym, *arguments, &block)
20
+ get "/#{method_sym}", *arguments
21
+ end
22
+
23
+ # Add one or more parameter to the default query string. Good for
24
+ # adding keys that are always needed, like API keys.
25
+ def param(params)
26
+ params.each do |key, value|
27
+ default_params[key] = value
28
+ default_params.delete key if value.nil?
29
+ end
30
+ end
31
+
32
+ def cache
33
+ @cache ||= WebCache.new
34
+ end
35
+
36
+ # Set a block to be executed after the request. This is called only when
37
+ # using `get` and not when using `get!`. Good for JSON decoding, for
38
+ # example.
39
+ def after_request(&block)
40
+ @after_request_block = block
41
+ end
42
+
43
+ # Return the response from the API.
44
+ def get(path, extra=nil, params={})
45
+ response = get! path, extra, params
46
+ response = after_request_block.call(response) if after_request_block
47
+ response
48
+ end
49
+
50
+ # Return the response from the API, without executing the after_request
51
+ # block.
52
+ def get!(path, extra=nil, params={})
53
+ if extra.is_a?(Hash) and params.empty?
54
+ params = extra
55
+ extra = nil
56
+ end
57
+
58
+ path = "#{path}/#{extra}" if extra
59
+ url = construct_url path, params
60
+
61
+ return url if debug
62
+
63
+ response = cache.get(url)
64
+ @last_error = response.error
65
+ response.content
66
+ end
67
+
68
+ # Save the response from the API to a file.
69
+ def save(filename, path, params={})
70
+ response = get! path, nil, params
71
+ return response if debug
72
+ File.write filename, response.to_s
73
+ end
74
+
75
+ # Build a URL from all its explicit and implicit pieces.
76
+ def construct_url(path, params={})
77
+ path = "/#{path}" unless path[0] == '/'
78
+ all_params = default_params.merge params
79
+ result = "#{base_url}#{path}"
80
+ result = "#{result}.#{format}" if format && File.extname(result) == ''
81
+ unless all_params.empty?
82
+ all_params = URI.encode_www_form all_params
83
+ result = "#{result}?#{all_params}"
84
+ end
85
+ result
86
+ end
87
+
88
+ def default_params
89
+ @default_params ||= {}
90
+ end
91
+
92
+ end
93
+ end
metadata ADDED
@@ -0,0 +1,192 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: intrinio
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Danny Ben Shitrit
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-01-29 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: docopt
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '0.5'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '0.5'
27
+ - !ruby/object:Gem::Dependency
28
+ name: awesome_print
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.7'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.7'
41
+ - !ruby/object:Gem::Dependency
42
+ name: webcache
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '0.3'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '0.3'
55
+ - !ruby/object:Gem::Dependency
56
+ name: runfile
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '0.8'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '0.8'
69
+ - !ruby/object:Gem::Dependency
70
+ name: runfile-tasks
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '0.4'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '0.4'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rspec
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '3.5'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '3.5'
97
+ - !ruby/object:Gem::Dependency
98
+ name: rdoc
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: '4.2'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: '4.2'
111
+ - !ruby/object:Gem::Dependency
112
+ name: byebug
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - "~>"
116
+ - !ruby/object:Gem::Version
117
+ version: '9.0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - "~>"
123
+ - !ruby/object:Gem::Version
124
+ version: '9.0'
125
+ - !ruby/object:Gem::Dependency
126
+ name: simplecov
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - "~>"
130
+ - !ruby/object:Gem::Version
131
+ version: '0.13'
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - "~>"
137
+ - !ruby/object:Gem::Version
138
+ version: '0.13'
139
+ - !ruby/object:Gem::Dependency
140
+ name: yard
141
+ requirement: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - "~>"
144
+ - !ruby/object:Gem::Version
145
+ version: '0.8'
146
+ type: :development
147
+ prerelease: false
148
+ version_requirements: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - "~>"
151
+ - !ruby/object:Gem::Version
152
+ version: '0.8'
153
+ description: Easy to use API for Intrinio data service with a command line interface
154
+ email: db@dannyben.com
155
+ executables:
156
+ - intrinio
157
+ extensions: []
158
+ extra_rdoc_files: []
159
+ files:
160
+ - README.md
161
+ - bin/intrinio
162
+ - lib/intrinio.rb
163
+ - lib/intrinio/api.rb
164
+ - lib/intrinio/command_line.rb
165
+ - lib/intrinio/docopt.txt
166
+ - lib/intrinio/version.rb
167
+ - lib/intrinio/web_api.rb
168
+ homepage: https://github.com/DannyBen/intrinio
169
+ licenses:
170
+ - MIT
171
+ metadata: {}
172
+ post_install_message:
173
+ rdoc_options: []
174
+ require_paths:
175
+ - lib
176
+ required_ruby_version: !ruby/object:Gem::Requirement
177
+ requirements:
178
+ - - ">="
179
+ - !ruby/object:Gem::Version
180
+ version: 2.0.0
181
+ required_rubygems_version: !ruby/object:Gem::Requirement
182
+ requirements:
183
+ - - ">="
184
+ - !ruby/object:Gem::Version
185
+ version: '0'
186
+ requirements: []
187
+ rubyforge_project:
188
+ rubygems_version: 2.6.6
189
+ signing_key:
190
+ specification_version: 4
191
+ summary: Intrinio API Library and Command Line
192
+ test_files: []