fredric 0.0.2 → 0.0.3

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: aeded696850394a227db2c59808569535a5d330f
4
- data.tar.gz: f27002ee98c5af408210938274f60f3980fc79b8
3
+ metadata.gz: 96f4e8baa0924d813a417ee8a53b4803880e6702
4
+ data.tar.gz: 808a86c0fa40ec9efba531f567c6ddf254a95af4
5
5
  SHA512:
6
- metadata.gz: f23601ce059eecc8db1eb283a25a00df26228f61894449d155dcdbc9e1277ca0a8bd425ebc4d6f59b849c46b35f4e0539c878ca6555a43e7381cbaeb57481cd4
7
- data.tar.gz: fe256721ce32f02248f9d5118bac89213d7a25ac62f1c1ab26a8708d56836366cfd8f4bb1ba99e028ed3f1247fcc90e42564131df1734842456a6be8ee109191
6
+ metadata.gz: 4e61e43908040861fbb839837875d54fc2f58aed0b08790cb7961b20dbd79ed89b52ee44985f9bdc0e9b2d966e89b2307a760e75609612137ff9ba38ac03ef70
7
+ data.tar.gz: 013046475b9fb0471b7b70170bb8e7c6c8e2bd23260a35d7ec19280c56901b5514ff4fcda369976a0684f13853bd1aab50cd0e71039ef1d938848ceb4bc605ac
data/README.md CHANGED
@@ -77,12 +77,37 @@ fredric.get 'endpoint/sub', param: value
77
77
  fredric.endpoint 'sub', param: value
78
78
  ```
79
79
 
80
- By default, you will get a ruby hash in return. If you wish to get the raw
81
- output, you can use the `get!` method:
80
+ By default, you will get a ruby hash in return. If you wish to have more
81
+ control over the response, use the `get!` method instead:
82
82
 
83
83
  ```ruby
84
84
  result = fredric.get! "series/ovservations", series_id: 'GNPCA'
85
+
86
+ # Request Object
87
+ p payload.request.class
88
+ # => HTTParty::Request
89
+
90
+ # Response Object
91
+ p payload.response.class
92
+ # => Net::HTTPOK
93
+
94
+ p payload.response.body
85
95
  # => JSON string
96
+
97
+ p payload.response.code
98
+ # => 200
99
+
100
+ p payload.response.msg
101
+ # => OK
102
+
103
+ # Headers Object
104
+ p payload.headers
105
+ # => Hash with headers
106
+
107
+ # Parsed Response Object
108
+ p payload.parsed_response
109
+ # => Hash with HTTParty parsed response
110
+ # (this is the content returned with #get)
86
111
  ```
87
112
 
88
113
  You can get the response as CSV by calling `get_csv`:
@@ -109,17 +134,6 @@ Or, to save CSV, use the `save_csv` method:
109
134
  fredric.save_csv "filename.csv", "series/overvations", series_id: 'GNPCA'
110
135
  ```
111
136
 
112
- Debugging your request and adding "sticky" query parameters that stay with
113
- you for the following requests is also easy:
114
-
115
- ```ruby
116
- fredric.debug = true
117
- fredric.param limit: 10, sort_order: :asc, file_type: :xml
118
- puts fredric.series 'observations', series_id: 'GNPCA'
119
- # => https://api.stlouisfed.org/fred/series/observations?api_key=...&file_type=xml&limit=10&sort_order=asc&series_id=GNPCA
120
-
121
- fredric.param sort_order: nil # remove param
122
- ```
123
137
 
124
138
 
125
139
  Command Line
@@ -166,11 +180,11 @@ $ fred url series/observations query:interest limit:5
166
180
  Caching
167
181
  --------------------------------------------------
168
182
 
169
- We are using the [WebCache][3] gem for automatic HTTP caching.
183
+ We are using the [Lightly][3] gem for automatic HTTP caching.
170
184
  To take the path of least surprises, caching is disabled by default.
171
185
 
172
186
  You can enable and customize it by either passing options on
173
- initialization, or by accessing the `WebCache` object directly at
187
+ initialization, or by accessing the `Lightly` object directly at
174
188
  a later stage.
175
189
 
176
190
  ```ruby
@@ -199,6 +213,6 @@ $ fred see category/children
199
213
 
200
214
  [1]: https://research.stlouisfed.org/docs/api/fred/
201
215
  [2]: https://github.com/DannyBen/fredric/blob/master/lib/fredric/docopt.txt
202
- [3]: https://github.com/DannyBen/webcache
216
+ [3]: https://github.com/DannyBen/lightly
203
217
  [4]: https://research.stlouisfed.org/docs/api/api_key.html
204
218
 
data/bin/fred CHANGED
@@ -4,6 +4,6 @@ require 'fredric'
4
4
 
5
5
  begin
6
6
  Fredric::CommandLine.instance.execute ARGV
7
- rescue Fredric::BadResponse => e
7
+ rescue APICake::BadResponse => e
8
8
  STDERR.puts "#{e.class} - #{e.message}"
9
9
  end
data/lib/fredric.rb CHANGED
@@ -1,5 +1,4 @@
1
1
  require 'fredric/version'
2
2
  require 'fredric/exceptions'
3
- require 'fredric/web_api'
4
3
  require 'fredric/api'
5
4
  require 'fredric/command_line'
data/lib/fredric/api.rb CHANGED
@@ -1,74 +1,22 @@
1
- require 'json'
2
- require 'csv'
1
+ require 'apicake'
3
2
 
4
3
  module Fredric
5
- # Provides access to all the FRED API endpoints
6
- class API < WebAPI
7
- attr_reader :api_key, :opts
4
+ # Provides access to all the FRED API endpoints with dynamic methods
5
+ # anc caching.
6
+ class API < APICake::Base
7
+ base_uri 'https://api.stlouisfed.org/fred'
8
+
9
+ attr_reader :api_key
8
10
 
9
11
  def initialize(api_key, opts={})
10
12
  @api_key = api_key
11
-
12
- defaults = {
13
- use_cache: false,
14
- cache_dir: nil,
15
- cache_life: nil,
16
- base_url: "https://api.stlouisfed.org/fred"
17
- }
18
-
19
- opts = defaults.merge! opts
20
- @opts = opts
21
-
22
13
  cache.disable unless opts[:use_cache]
23
14
  cache.dir = opts[:cache_dir] if opts[:cache_dir]
24
15
  cache.life = opts[:cache_life] if opts[:cache_life]
25
-
26
- param api_key: api_key if api_key
27
- param file_type: :json
28
-
29
- after_request do |response|
30
- begin
31
- JSON.parse response, symbolize_names: true
32
- rescue JSON::ParserError
33
- response
34
- end
35
- end
36
-
37
- super opts[:base_url]
38
- end
39
-
40
- def get_csv(*args)
41
- response = get(*args)
42
-
43
- raise Fredric::BadResponse, "API said '#{response}'" if response.is_a? String
44
- raise Fredric::BadResponse, "Got a #{response.class}, expected a Hash" unless response.is_a? Hash
45
-
46
- data = csv_node response
47
-
48
- header = data.first.keys
49
- result = CSV.generate do |csv|
50
- csv << header
51
- data.each { |row| csv << row.values }
52
- end
53
-
54
- result
55
16
  end
56
17
 
57
- def save_csv(file, *args)
58
- data = get_csv(*args)
59
- File.write file, data
60
- end
61
-
62
- private
63
-
64
- # Determins which part of the data is best suited to be displayed
65
- # as CSV.
66
- # - In case there is an array in the data (like 'observations' or
67
- # 'seriess'), it will be returned
68
- # - Otherwise, we will use the entire response as a single row CSV
69
- def csv_node(data)
70
- arrays = data.keys.select { |key| data[key].is_a? Array }
71
- arrays.empty? ? [data] : data[arrays.first]
18
+ def default_query
19
+ { api_key: api_key, file_type: :json }
72
20
  end
73
21
  end
74
22
  end
@@ -56,7 +56,8 @@ module Fredric
56
56
  if csv
57
57
  puts fredric.get_csv path, params
58
58
  else
59
- puts fredric.get! path, params
59
+ payload = fredric.get! path, params
60
+ puts payload.response.body
60
61
  end
61
62
  end
62
63
 
@@ -70,8 +71,8 @@ module Fredric
70
71
  end
71
72
 
72
73
  def pretty
73
- response = fredric.get path, params
74
- puts JSON.pretty_generate response
74
+ payload = fredric.get path, params
75
+ puts JSON.pretty_generate payload
75
76
  end
76
77
 
77
78
  def see
@@ -79,9 +80,7 @@ module Fredric
79
80
  end
80
81
 
81
82
  def url
82
- fredric.debug = true
83
- puts fredric.get path, params
84
- fredric.debug = false
83
+ puts fredric.url path, params
85
84
  end
86
85
 
87
86
  # Convert a params array like [key:value, key:value] to a hash like
@@ -1,3 +1,3 @@
1
1
  module Fredric
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fredric
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
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-11 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
@@ -166,7 +166,6 @@ files:
166
166
  - lib/fredric/docopt.txt
167
167
  - lib/fredric/exceptions.rb
168
168
  - lib/fredric/version.rb
169
- - lib/fredric/web_api.rb
170
169
  homepage: https://github.com/DannyBen/fredric
171
170
  licenses:
172
171
  - MIT
@@ -1,93 +0,0 @@
1
- require 'uri'
2
- require 'open-uri'
3
- require 'webcache'
4
-
5
- module Fredric
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