intrinio 0.1.3 → 0.1.4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: c39293234adfdcffde5539f0dcdf68385155f258
4
- data.tar.gz: 3ee010eb81eb115abcb1da60fcc5e69fcfa2bf5d
3
+ metadata.gz: cf2eaeb36817a3f28f086932851aa82640d66649
4
+ data.tar.gz: e41ba73df6877e6031bafa0fce2e655aff79096a
5
5
  SHA512:
6
- metadata.gz: 19ac629eda3c065f569160148cbafc86d9963fbcea6c6a767a75cda675a17532a5634220cd80b019cc94f17b5a8c09e28a126caf0a6c84d2f98399d416061e44
7
- data.tar.gz: 6004cedc1ccf6b5b5848fce25f735a3052c41024aa15307b98fa0d92313f44e87b6a6a7d0e5afef068d12433ca908424789ca6654960dae523612d1557e7cf11
6
+ metadata.gz: 428dcc7137db5a06459c20b385496b6c529d69e18c085f6bcf29d5a2f42d1c1a2310159bb474d485e09524ca54984200b2e2a6dd92c2dbf45e551f0691db7a26
7
+ data.tar.gz: 86a3c2604b6bb99c8443ee9eaebedec15cdf261dc4f9a49643f89ad03624c9c95da51520a49ebde8e3d587f64b28063e99464d11c3be98367beec988e519aaa7
data/README.md CHANGED
@@ -71,22 +71,57 @@ intrinio.get 'endpoint', param: value
71
71
  intrinio.endpoint, param: value
72
72
  ```
73
73
 
74
- By default, you will get a ruby hash in return. If you wish to get the raw
75
- output, you can use the `get!` method:
74
+ as well as these two:
75
+
76
+ ```ruby
77
+ intrinio.get 'endpoint/sub', param: value
78
+ intrinio.endpoint 'sub', param: value
79
+ ```
80
+
81
+ By default, you will get a ruby hash in return. If you wish to have more
82
+ control over the response, use the `get!` method instead:
76
83
 
77
84
  ```ruby
78
85
  result = intrinio.get! "indices", type: 'economic', page_size: 5
86
+
87
+ # Request Object
88
+ p payload.request.class
89
+ # => HTTParty::Request
90
+
91
+ # Response Object
92
+ p payload.response.class
93
+ # => Net::HTTPOK
94
+
95
+ p payload.response.body
79
96
  # => JSON string
97
+
98
+ p payload.response.code
99
+ # => 200
100
+
101
+ p payload.response.msg
102
+ # => OK
103
+
104
+ # Headers Object
105
+ p payload.headers
106
+ # => Hash with headers
107
+
108
+ # Parsed Response Object
109
+ p payload.parsed_response
110
+ # => Hash with HTTParty parsed response
111
+ # (this is the content returned with #get)
80
112
  ```
81
113
 
82
- When calling any endpoint that returns a `data` attribute, you can also
83
- call `get_csv` in order to convert the data to a CSV string.
114
+ You can get the response as CSV by calling `get_csv`:
84
115
 
85
116
  ```ruby
86
117
  result = intrinio.get_csv "indices", page_size: 5
87
118
  # => CSV string
88
119
  ```
89
120
 
121
+ Intrinio automatically decides which part of the data to convert to CSV.
122
+ When there is an array in the response, it will be used as the CSV data.
123
+ Otherwise, the entire response will be treated as a single-row CSV.
124
+
90
125
  To save the output directly to a file, use the `save` method:
91
126
 
92
127
  ```ruby
@@ -96,20 +131,9 @@ intrinio.save "filename.json", "indices", type: 'economic', page_size: 5
96
131
  Or, to save CSV, use the `save_csv` method:
97
132
 
98
133
  ```ruby
99
- intrinio.save_csv "filename.csv" "indices", page_size: 5
134
+ intrinio.save_csv "filename.csv", "indices", page_size: 5
100
135
  ```
101
136
 
102
- Debugging your request and adding "sticky" query parameters that stay with
103
- you for the following requests is also easy:
104
-
105
- ```ruby
106
- intrinio.debug = true
107
- intrinio.param page_size: 10, order_direction: 'asc'
108
- puts intrinio.historical_data identifier: '$INTDSRUSM193N', item: 'level'
109
- # => "https://api.intrinio.com/historical_data?page_size=10&order_direction=asc&identifier=%24INTDSRUSM193N&item=level
110
-
111
- intrinio.param page_size: nil # remove param
112
- ```
113
137
 
114
138
 
115
139
  Command Line
@@ -154,7 +178,7 @@ $ intrinio url indices query:interest page_size:5
154
178
  Caching
155
179
  --------------------------------------------------
156
180
 
157
- We are using the [WebCache][3] gem for automatic HTTP caching.
181
+ We are using the [Lightly][3] gem for automatic HTTP caching.
158
182
  To take the path of least surprises, caching is disabled by default.
159
183
 
160
184
  You can enable and customize it by either passing options on
@@ -192,4 +216,4 @@ $ intrinio get indices
192
216
 
193
217
  [1]: https://www.intrinio.com
194
218
  [2]: https://github.com/DannyBen/intrinio/blob/master/lib/intrinio/docopt.txt
195
- [3]: https://github.com/DannyBen/webcache
219
+ [3]: https://github.com/DannyBen/lightly
@@ -4,6 +4,6 @@ require 'intrinio'
4
4
 
5
5
  begin
6
6
  Intrinio::CommandLine.instance.execute ARGV
7
- rescue Intrinio::BadResponse, Intrinio::IncompatibleResponse => e
7
+ rescue APICake::BadResponse => e
8
8
  STDERR.puts "#{e.class} - #{e.message}"
9
9
  end
@@ -1,5 +1,4 @@
1
1
  require 'intrinio/version'
2
2
  require 'intrinio/exceptions'
3
- require 'intrinio/web_api'
4
3
  require 'intrinio/api'
5
4
  require 'intrinio/command_line'
@@ -1,10 +1,12 @@
1
- require 'json'
2
- require 'csv'
1
+ require 'apicake'
3
2
 
4
3
  module Intrinio
5
- # Provides access to all the Intrinio API endpoints
6
- class API < WebAPI
7
- attr_reader :opts
4
+ # Provides access to all the Intrinio API endpoints with dynamic methods
5
+ # anc caching.
6
+ class API < APICake::Base
7
+ base_uri 'https://api.intrinio.com'
8
+
9
+ attr_reader :username, :password
8
10
 
9
11
  def initialize(opts={})
10
12
  if opts[:auth]
@@ -12,58 +14,15 @@ module Intrinio
12
14
  opts.delete :auth
13
15
  end
14
16
 
15
- defaults = {
16
- username: nil,
17
- password: nil,
18
- use_cache: false,
19
- cache_dir: nil,
20
- cache_life: nil,
21
- base_url: "https://api.intrinio.com"
22
- }
23
-
24
- opts = defaults.merge! opts
25
- @opts = opts
26
-
27
- username, password = opts[:username], opts[:password]
17
+ @username, @password = opts[:username], opts[:password]
28
18
 
29
19
  cache.disable unless opts[:use_cache]
30
20
  cache.dir = opts[:cache_dir] if opts[:cache_dir]
31
21
  cache.life = opts[:cache_life] if opts[:cache_life]
32
- cache.options[:http_basic_authentication] = [username, password]
33
-
34
- after_request do |response|
35
- begin
36
- JSON.parse response, symbolize_names: true
37
- rescue JSON::ParserError
38
- response
39
- end
40
- end
41
-
42
- super opts[:base_url]
43
- end
44
-
45
- def get_csv(*args)
46
- result = get *args
47
-
48
- raise Intrinio::BadResponse, "API said '#{result}'" if result.is_a? String
49
- raise Intrinio::BadResponse, "Got a #{result.class}, expected a Hash" unless result.is_a? Hash
50
- raise Intrinio::IncompatibleResponse, "There is no data attribute in the response" unless result.has_key? :data
51
- raise Intrinio::IncompatibleResponse, "The data attribute in the response is empty" unless result[:data]
52
-
53
- data = result[:data]
54
-
55
- header = data.first.keys
56
- result = CSV.generate do |csv|
57
- csv << header
58
- data.each { |row| csv << row.values }
59
- end
60
-
61
- result
62
22
  end
63
23
 
64
- def save_csv(file, *args)
65
- data = get_csv *args
66
- File.write file, data
24
+ def default_params
25
+ { basic_auth: { username: username, password: password } }
67
26
  end
68
27
  end
69
28
  end
@@ -56,7 +56,8 @@ module Intrinio
56
56
  if csv
57
57
  puts intrinio.get_csv path, params
58
58
  else
59
- puts intrinio.get! path, params
59
+ payload = intrinio.get! path, params
60
+ puts payload.response.body
60
61
  end
61
62
  end
62
63
 
@@ -70,8 +71,8 @@ module Intrinio
70
71
  end
71
72
 
72
73
  def pretty
73
- response = intrinio.get path, params
74
- puts JSON.pretty_generate response
74
+ payload = intrinio.get path, params
75
+ puts JSON.pretty_generate payload
75
76
  end
76
77
 
77
78
  def see
@@ -79,19 +80,17 @@ module Intrinio
79
80
  end
80
81
 
81
82
  def url
82
- intrinio.debug = true
83
- puts intrinio.get path, params
84
- intrinio.debug = false
83
+ puts intrinio.url path, params
85
84
  end
86
85
 
87
86
  # Convert a params array like [key:value, key:value] to a hash like
88
87
  # {key: value, key: value}
89
88
  def translate_params(pairs)
90
- return nil if pairs.empty?
91
89
  result = {}
90
+ return result if pairs.empty?
92
91
  pairs.each do |pair|
93
92
  key, value = pair.split ':'
94
- result[key] = value
93
+ result[key.to_sym] = value
95
94
  end
96
95
  result
97
96
  end
@@ -1,5 +1,3 @@
1
1
  module Intrinio
2
- class BadResponse < StandardError; end
3
- class IncompatibleResponse < StandardError; end
4
2
  class MissingAuth < StandardError; end
5
3
  end
@@ -1,3 +1,3 @@
1
1
  module Intrinio
2
- VERSION = "0.1.3"
2
+ VERSION = "0.1.4"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: intrinio
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.1.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Danny Ben Shitrit
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-02-03 00:00:00.000000000 Z
11
+ date: 2017-02-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: docopt
@@ -39,19 +39,19 @@ dependencies:
39
39
  - !ruby/object:Gem::Version
40
40
  version: '1.7'
41
41
  - !ruby/object:Gem::Dependency
42
- name: webcache
42
+ name: apicake
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
45
  - - "~>"
46
46
  - !ruby/object:Gem::Version
47
- version: '0.3'
47
+ version: '0.1'
48
48
  type: :runtime
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
52
  - - "~>"
53
53
  - !ruby/object:Gem::Version
54
- version: '0.3'
54
+ version: '0.1'
55
55
  - !ruby/object:Gem::Dependency
56
56
  name: runfile
57
57
  requirement: !ruby/object:Gem::Requirement
@@ -165,7 +165,6 @@ files:
165
165
  - lib/intrinio/docopt.txt
166
166
  - lib/intrinio/exceptions.rb
167
167
  - lib/intrinio/version.rb
168
- - lib/intrinio/web_api.rb
169
168
  homepage: https://github.com/DannyBen/intrinio
170
169
  licenses:
171
170
  - MIT
@@ -1,93 +0,0 @@
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