quandl_client 2.7.11 → 2.7.12
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +7 -7
- data/.rspec +1 -1
- data/.travis.yml +20 -20
- data/.yardopts +2 -2
- data/Gemfile +12 -12
- data/Guardfile +8 -8
- data/LICENSE +7 -7
- data/README.md +303 -303
- data/Rakefile +31 -31
- data/UPGRADE.md +220 -214
- data/VERSION +1 -1
- data/examples/create.rb +32 -32
- data/examples/find.rb +17 -17
- data/examples/login.rb +12 -12
- data/examples/search.rb +12 -12
- data/examples/trims.rb +15 -15
- data/lib/quandl/client/base/attributes.rb +15 -15
- data/lib/quandl/client/base/model.rb +40 -40
- data/lib/quandl/client/base/search.rb +74 -74
- data/lib/quandl/client/base/validation.rb +103 -101
- data/lib/quandl/client/base.rb +91 -91
- data/lib/quandl/client/middleware/parse_json.rb +87 -85
- data/lib/quandl/client/middleware.rb +9 -9
- data/lib/quandl/client/models/dataset/data.rb +57 -57
- data/lib/quandl/client/models/dataset.rb +269 -269
- data/lib/quandl/client/models/location.rb +10 -10
- data/lib/quandl/client/models/report.rb +14 -14
- data/lib/quandl/client/models/scraper.rb +16 -16
- data/lib/quandl/client/models/sheet.rb +50 -50
- data/lib/quandl/client/models/source.rb +48 -48
- data/lib/quandl/client/models/superset.rb +65 -65
- data/lib/quandl/client/models/user.rb +7 -7
- data/lib/quandl/client/version.rb +14 -14
- data/lib/quandl/client.rb +54 -53
- data/lib/quandl/her/collection.rb +18 -0
- data/lib/quandl/her/remove_method_data.rb +8 -8
- data/lib/quandl/pattern/client.rb +8 -8
- data/lib/quandl/pattern.rb +37 -37
- data/quandl_client.gemspec +33 -33
- data/spec/factories/dataset.rb +10 -10
- data/spec/factories/sheet.rb +7 -7
- data/spec/factories/source.rb +9 -9
- data/spec/fixtures/scraper.rb +5 -5
- data/spec/lib/quandl/client/dataset/attributes_spec.rb +63 -63
- data/spec/lib/quandl/client/dataset/data_spec.rb +92 -92
- data/spec/lib/quandl/client/dataset/location_spec.rb +65 -65
- data/spec/lib/quandl/client/dataset/persistence_spec.rb +104 -104
- data/spec/lib/quandl/client/dataset/search_spec.rb +19 -19
- data/spec/lib/quandl/client/dataset/source_spec.rb +47 -47
- data/spec/lib/quandl/client/dataset/trim_spec.rb +35 -35
- data/spec/lib/quandl/client/dataset/validation_spec.rb +68 -68
- data/spec/lib/quandl/client/dataset_spec.rb +57 -57
- data/spec/lib/quandl/client/scraper_spec.rb +71 -71
- data/spec/lib/quandl/client/sheet_spec.rb +37 -37
- data/spec/lib/quandl/client/source_spec.rb +51 -51
- data/spec/spec_helper.rb +30 -30
- metadata +28 -4
@@ -1,86 +1,88 @@
|
|
1
|
-
require 'json'
|
2
|
-
|
3
|
-
module Quandl
|
4
|
-
module Client
|
5
|
-
module Middleware
|
6
|
-
|
7
|
-
class ParseJSON < Faraday::Response::Middleware
|
8
|
-
|
9
|
-
def on_complete(env)
|
10
|
-
env[:body] = case env[:status]
|
11
|
-
when 204
|
12
|
-
parse('{}', env)
|
13
|
-
else
|
14
|
-
parse(env[:body], env)
|
15
|
-
end
|
16
|
-
end
|
17
|
-
|
18
|
-
def parse(body, env)
|
19
|
-
json = parse_json(body, env)
|
20
|
-
json.has_key?(:docs) ? format_collection( json, env ) : format_record( json, env )
|
21
|
-
end
|
22
|
-
|
23
|
-
def format_record(json, env)
|
24
|
-
errors = json.delete(:errors) || {}
|
25
|
-
metadata = json.delete(:metadata) || {}
|
26
|
-
# collect some response data
|
27
|
-
metadata.merge!({
|
28
|
-
status: env[:status],
|
29
|
-
headers: env[:response_headers],
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
:
|
35
|
-
:
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
#
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
:
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
end
|
83
|
-
|
84
|
-
end
|
85
|
-
|
1
|
+
require 'json'
|
2
|
+
|
3
|
+
module Quandl
|
4
|
+
module Client
|
5
|
+
module Middleware
|
6
|
+
|
7
|
+
class ParseJSON < Faraday::Response::Middleware
|
8
|
+
|
9
|
+
def on_complete(env)
|
10
|
+
env[:body] = case env[:status]
|
11
|
+
when 204
|
12
|
+
parse('{}', env)
|
13
|
+
else
|
14
|
+
parse(env[:body], env)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
def parse(body, env)
|
19
|
+
json = parse_json(body, env)
|
20
|
+
json.has_key?(:docs) ? format_collection( json, env ) : format_record( json, env )
|
21
|
+
end
|
22
|
+
|
23
|
+
def format_record(json, env)
|
24
|
+
errors = json.delete(:errors) || {}
|
25
|
+
metadata = json.delete(:metadata) || {}
|
26
|
+
# collect some response data
|
27
|
+
metadata.merge!({
|
28
|
+
status: env[:status],
|
29
|
+
headers: env[:response_headers],
|
30
|
+
method: env[:method],
|
31
|
+
})
|
32
|
+
# return object
|
33
|
+
object = {
|
34
|
+
:data => json,
|
35
|
+
:errors => errors,
|
36
|
+
:metadata => metadata
|
37
|
+
}
|
38
|
+
env[:status] = 200
|
39
|
+
object
|
40
|
+
end
|
41
|
+
|
42
|
+
def format_collection(json, env)
|
43
|
+
errors = json.delete(:errors) || {}
|
44
|
+
metadata = json.delete(:metadata) || {}
|
45
|
+
docs = json.delete(:docs)
|
46
|
+
# collect some response data
|
47
|
+
metadata.merge!(json).merge!({
|
48
|
+
status: env[:status],
|
49
|
+
headers: env[:response_headers],
|
50
|
+
method: env[:method],
|
51
|
+
})
|
52
|
+
# each doc metadata references metadata
|
53
|
+
docs.each{|d| d[:metadata] = metadata }
|
54
|
+
# return object
|
55
|
+
object = {
|
56
|
+
:data => docs,
|
57
|
+
:errors => errors,
|
58
|
+
:metadata => metadata
|
59
|
+
}
|
60
|
+
env[:status] = 200
|
61
|
+
object
|
62
|
+
end
|
63
|
+
|
64
|
+
def parse_json(body = nil, env)
|
65
|
+
body ||= '{}'
|
66
|
+
json = begin
|
67
|
+
JSON.parse(body).symbolize_keys!
|
68
|
+
rescue JSON::ParserError
|
69
|
+
nil
|
70
|
+
end
|
71
|
+
# invalid json body?
|
72
|
+
if json.blank?
|
73
|
+
# fallback to error message
|
74
|
+
json = {
|
75
|
+
id: 1,
|
76
|
+
errors: {
|
77
|
+
parse_errors: [ "Quandl::Client::ParseJSON error. status: #{env[:status]}, body: #{body.inspect}" ]
|
78
|
+
}
|
79
|
+
}
|
80
|
+
end
|
81
|
+
json
|
82
|
+
end
|
83
|
+
|
84
|
+
end
|
85
|
+
|
86
|
+
end
|
87
|
+
end
|
86
88
|
end
|
@@ -1,10 +1,10 @@
|
|
1
|
-
require 'faraday'
|
2
|
-
require 'quandl/client/middleware/parse_json'
|
3
|
-
|
4
|
-
module Quandl
|
5
|
-
module Client
|
6
|
-
module Middleware
|
7
|
-
|
8
|
-
end
|
9
|
-
end
|
1
|
+
require 'faraday'
|
2
|
+
require 'quandl/client/middleware/parse_json'
|
3
|
+
|
4
|
+
module Quandl
|
5
|
+
module Client
|
6
|
+
module Middleware
|
7
|
+
|
8
|
+
end
|
9
|
+
end
|
10
10
|
end
|
@@ -1,58 +1,58 @@
|
|
1
|
-
class Quandl::Client::Dataset::Data < Quandl::Client::Base
|
2
|
-
|
3
|
-
@_her_resource_path = "datasets/:id/data"
|
4
|
-
|
5
|
-
has_scope_composer
|
6
|
-
|
7
|
-
scope.class_eval do
|
8
|
-
delegate *Quandl::Client::Dataset::Data.forwardable_scope_methods, :to_h, to: :to_table, allow_nil: true
|
9
|
-
delegate *Quandl::Data.forwardable_methods, to: :to_table, allow_nil: true
|
10
|
-
end
|
11
|
-
|
12
|
-
scope *[:row, :rows, :limit, :offset, :accuracy, :column, :order,
|
13
|
-
:transform, :collapse, :exclude_headers]
|
14
|
-
|
15
|
-
scope :trim_start, ->(date){ date = parse_date(date); where( trim_start: date ) if date }
|
16
|
-
scope :trim_end, ->(date){ date = parse_date(date); where( trim_end: date ) if date }
|
17
|
-
|
18
|
-
scope_helper :parse_date, ->( value ){
|
19
|
-
begin
|
20
|
-
date = Date.jd(value.to_i) if value.kind_of?(String) && value.numeric?
|
21
|
-
date = Date.jd(value) if value.is_a?(Integer)
|
22
|
-
date = Date.parse(value) if value.is_a?(String) && value =~ /^[0-9]{4}\-[0-9]{2}\-[0-9]{2}$/
|
23
|
-
date = value if value.is_a?(Date)
|
24
|
-
date = value.to_date if value.respond_to?(:to_date)
|
25
|
-
date.to_s
|
26
|
-
rescue
|
27
|
-
nil
|
28
|
-
end
|
29
|
-
}
|
30
|
-
|
31
|
-
scope_helper :to_table, -> {
|
32
|
-
data = fetch_once.data
|
33
|
-
data.headers = fetch_once.column_names
|
34
|
-
data
|
35
|
-
}
|
36
|
-
|
37
|
-
attributes :id, :limit, :collapse, :transformation, :trim_start, :trim_end,
|
38
|
-
:rows, :row, :frequency, :data, :from_date, :to_date, :column_names
|
39
|
-
|
40
|
-
def data
|
41
|
-
read_data
|
42
|
-
end
|
43
|
-
|
44
|
-
def data=(value)
|
45
|
-
write_data(value)
|
46
|
-
end
|
47
|
-
|
48
|
-
protected
|
49
|
-
|
50
|
-
def read_data
|
51
|
-
Quandl::Data.new( read_attribute(:data) )
|
52
|
-
end
|
53
|
-
|
54
|
-
def write_data(value )
|
55
|
-
write_attribute(:data, Quandl::Data.new(value).to_csv )
|
56
|
-
end
|
57
|
-
|
1
|
+
class Quandl::Client::Dataset::Data < Quandl::Client::Base
|
2
|
+
|
3
|
+
@_her_resource_path = "datasets/:id/data"
|
4
|
+
|
5
|
+
has_scope_composer
|
6
|
+
|
7
|
+
scope.class_eval do
|
8
|
+
delegate *Quandl::Client::Dataset::Data.forwardable_scope_methods, :to_h, to: :to_table, allow_nil: true
|
9
|
+
delegate *Quandl::Data.forwardable_methods, to: :to_table, allow_nil: true
|
10
|
+
end
|
11
|
+
|
12
|
+
scope *[:row, :rows, :limit, :offset, :accuracy, :column, :order,
|
13
|
+
:transform, :collapse, :exclude_headers]
|
14
|
+
|
15
|
+
scope :trim_start, ->(date){ date = parse_date(date); where( trim_start: date ) if date }
|
16
|
+
scope :trim_end, ->(date){ date = parse_date(date); where( trim_end: date ) if date }
|
17
|
+
|
18
|
+
scope_helper :parse_date, ->( value ){
|
19
|
+
begin
|
20
|
+
date = Date.jd(value.to_i) if value.kind_of?(String) && value.numeric?
|
21
|
+
date = Date.jd(value) if value.is_a?(Integer)
|
22
|
+
date = Date.parse(value) if value.is_a?(String) && value =~ /^[0-9]{4}\-[0-9]{2}\-[0-9]{2}$/
|
23
|
+
date = value if value.is_a?(Date)
|
24
|
+
date = value.to_date if value.respond_to?(:to_date)
|
25
|
+
date.to_s
|
26
|
+
rescue
|
27
|
+
nil
|
28
|
+
end
|
29
|
+
}
|
30
|
+
|
31
|
+
scope_helper :to_table, -> {
|
32
|
+
data = fetch_once.data
|
33
|
+
data.headers = fetch_once.column_names
|
34
|
+
data
|
35
|
+
}
|
36
|
+
|
37
|
+
attributes :id, :limit, :collapse, :transformation, :trim_start, :trim_end,
|
38
|
+
:rows, :row, :frequency, :data, :from_date, :to_date, :column_names
|
39
|
+
|
40
|
+
def data
|
41
|
+
read_data
|
42
|
+
end
|
43
|
+
|
44
|
+
def data=(value)
|
45
|
+
write_data(value)
|
46
|
+
end
|
47
|
+
|
48
|
+
protected
|
49
|
+
|
50
|
+
def read_data
|
51
|
+
Quandl::Data.new( read_attribute(:data) )
|
52
|
+
end
|
53
|
+
|
54
|
+
def write_data(value )
|
55
|
+
write_attribute(:data, Quandl::Data.new(value).to_csv )
|
56
|
+
end
|
57
|
+
|
58
58
|
end
|