apicake 0.1.0 → 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
- SHA1:
3
- metadata.gz: 6e7c41c224b2b2435828e862a3047a50aaf7ea6e
4
- data.tar.gz: eacee61fff039351173484ea34db7d7d19a11b12
2
+ SHA256:
3
+ metadata.gz: 2016b0c4a2652010b263db2d6e6d85804c2cfb23f9c8a8c0d66e55f443e4dee6
4
+ data.tar.gz: 56475f108cc6bb2ad7637005af0f58a73b0bd4cb1587c22f49ec50a61f1aafa3
5
5
  SHA512:
6
- metadata.gz: e08bb8197889886c32400bc2bc035a924d730b71eb9c03f2bd4b74e91cfdda7678693c6de867d204d6efb3cd37011ff8e2513b237dcce23151f4a380ab8e8ebe
7
- data.tar.gz: c385af56680fbad9d96437f4ae9e9a24feea6a3250a9f83d6ec7e9dfd6eaa53f7fe93017e480e1fe64bea32d0effe37d3e9a0f623dc562659e65b265eb63d3ae
6
+ metadata.gz: 1345df60ac9a9cba25e27a846b14571c3c84e33fe124d4917ef198f0f8d471f2e59a855d5456b909739e29a1c37791f72a3455f30811ab03f70c338dddef0da4
7
+ data.tar.gz: c491ea7c9e46d9f028ae37723ffc3003226360714cf3d581a1448b54a981fc0392b488c2d28c71ee36abb9d072e34bc8cb3171d7c6f8b2f3d36862ebc9178e9a
data/README.md CHANGED
@@ -1,11 +1,9 @@
1
1
  API Cake - Build Dynamic API Wrappers
2
2
  ==================================================
3
3
 
4
- [![Gem](https://img.shields.io/gem/v/apicake.svg?style=flat-square)](https://rubygems.org/gems/apicake)
5
- [![Travis](https://img.shields.io/travis/DannyBen/apicake.svg?style=flat-square)](https://travis-ci.org/DannyBen/apicake)
6
- [![Code Climate](https://img.shields.io/codeclimate/github/DannyBen/apicake.svg?style=flat-square)](https://codeclimate.com/github/DannyBen/apicake)
7
- [![Gemnasium](https://img.shields.io/gemnasium/DannyBen/apicake.svg?style=flat-square)](https://gemnasium.com/DannyBen/apicake)
8
-
4
+ [![Gem Version](https://badge.fury.io/rb/apicake.svg)](https://badge.fury.io/rb/apicake)
5
+ [![Build Status](https://github.com/DannyBen/apicake/workflows/Test/badge.svg)](https://github.com/DannyBen/apicake/actions?query=workflow%3ATest)
6
+ [![Maintainability](https://api.codeclimate.com/v1/badges/07bd0f8653914ce703a6/maintainability)](https://codeclimate.com/github/DannyBen/apicake/maintainability)
9
7
 
10
8
  ---
11
9
 
@@ -36,20 +34,21 @@ TL;DR
36
34
  Turn this hypothetical API URL:
37
35
 
38
36
  ```
39
- http://api.recipies.com/cakes?layers=3
37
+ http://api.recipes.com/cakes?layers=3
40
38
  ```
41
39
 
42
40
  To this:
43
41
 
44
42
  ```ruby
45
- Recipies.cakes layers:3
43
+ recipes = Recipes.new
44
+ recipes.cakes layers:3
46
45
  ```
47
46
 
48
47
  Using this code only:
49
48
 
50
49
  ```ruby
51
- class Recipies < APICake::Base
52
- base_url 'api.recipies.com'
50
+ class Recipes < APICake::Base
51
+ base_uri 'api.recipes.com'
53
52
  end
54
53
  ```
55
54
 
@@ -78,20 +77,20 @@ URL.
78
77
  For example:
79
78
 
80
79
  ```ruby
81
- class Recipies << APICake::Base
82
- base_url 'api.recipies.com/v1'
80
+ class Recipes << APICake::Base
81
+ base_uri 'api.recipes.com/v1'
83
82
  end
84
83
 
85
- recipies = Rcipies.new
84
+ recipes = Recipes.new
86
85
 
87
- # This will access http://api.recipies.com/v1/cakes
88
- recipies.cakes
86
+ # This will access http://api.recipes.com/v1/cakes
87
+ recipes.cakes
89
88
 
90
- # This will access http://api.recipies.com/v1/cakes/chocolate
91
- recipies.cakes 'chocolate'
89
+ # This will access http://api.recipes.com/v1/cakes/chocolate
90
+ recipes.cakes 'chocolate'
92
91
 
93
- # This will access http://api.recipies.com/v1/cakes/chocolate?layers=3
94
- recipies.cakes 'chocolate', layers: 3
92
+ # This will access http://api.recipes.com/v1/cakes/chocolate?layers=3
93
+ recipes.cakes 'chocolate', layers: 3
95
94
  ```
96
95
 
97
96
  See the [Examples folder][1] for more examples.
@@ -109,7 +108,18 @@ See the [caching example][3].
109
108
  Method Reference
110
109
  --------------------------------------------------
111
110
 
112
- Documentation to be completed
111
+ For a detailed explanation of the services and methods you get when inheriting
112
+ from `APICake::Base`, see the [class documentation][4].
113
+
114
+
115
+ Real World Examples
116
+ --------------------------------------------------
117
+
118
+ These gems use APICake:
119
+
120
+ - [Fredric][5] - API wrapper for the FRED database ([go straight to API class][8])
121
+ - [Intrinio][6] - API wrapper for the Intrinio data service ([go straight to API class][9])
122
+ - [Quata][7] - API wrapper for the Quandl data service ([go straight to API class][10])
113
123
 
114
124
 
115
125
  ---
@@ -117,3 +127,10 @@ Documentation to be completed
117
127
  [1]: https://github.com/DannyBen/apicake/tree/master/examples
118
128
  [2]: https://github.com/DannyBen/lightly
119
129
  [3]: https://github.com/DannyBen/apicake/blob/master/examples/04-caching.rb
130
+ [4]: http://www.rubydoc.info/gems/apicake/0.1.1/APICake/Base
131
+ [5]: https://github.com/DannyBen/fredric
132
+ [6]: https://github.com/DannyBen/intrinio
133
+ [7]: https://github.com/DannyBen/quata
134
+ [8]: https://github.com/DannyBen/fredric/blob/master/lib/fredric/api.rb
135
+ [9]: https://github.com/DannyBen/intrinio/blob/master/lib/intrinio/api.rb
136
+ [10]: https://github.com/DannyBen/quata/blob/master/lib/quata/api.rb
data/lib/apicake/base.rb CHANGED
@@ -1,40 +1,143 @@
1
- require 'httparty'
2
- require 'lightly'
1
+ require "httparty"
2
+ require "lightly"
3
3
 
4
4
  module APICake
5
+ # To create your API wrapper, make a class that inherit from this class.
6
+ #
7
+ # This class includes the HTTParty module, and the only requirement,
8
+ # is that you call +base_uri+ to define the base URI of the API.
9
+ #
10
+ # === Example
11
+ #
12
+ # class Client < APICake::Base
13
+ # base_uri: 'http://some.api.com/v3'
14
+ # end
15
+ #
5
16
  class Base
6
17
  include HTTParty
7
18
 
8
- attr_reader :last_payload, :last_url
19
+ # Holds the last {Payload} object that was obtained by the last call to
20
+ # {#get}, {#get!}, {#get_csv} or {#save_csv}
21
+ #
22
+ # === Example
23
+ #
24
+ # client = Client.new
25
+ # client.some_path, param: value
26
+ # p client.last_payload
27
+ # # => a Payload object
28
+ #
29
+ attr_reader :last_payload
9
30
 
10
- # Any undefined method will be delegated to the #get method
31
+ # Holds the last URL that was downloaded by the last call to
32
+ # {#get}, {#get!}, {#get_csv} or {#save_csv}.
33
+ #
34
+ # === Example
35
+ #
36
+ # client = Client.new
37
+ # client.some_path, param: value
38
+ # p client.last_url
39
+ # # => "http://some.api.com/v3/some_path?param=value"
40
+ #
41
+ attr_reader :last_url
42
+
43
+ # Any undefined method call will be delegated to the {#get} method.
44
+ #
45
+ # === Example
46
+ #
47
+ # This:
48
+ #
49
+ # client = Client.new
50
+ # client.path 'optional_sub_path', optional_param: value, optional_param: value
51
+ #
52
+ # Is equivalent to this:
53
+ #
54
+ # client.get 'path/optional_sub_path', optional_param: value, optional_param: value
55
+ #
11
56
  def method_missing(method_sym, *arguments, &_block)
12
57
  get "/#{method_sym}", *arguments
13
58
  end
14
59
 
15
- # This is the Lightly cache object. You can access or modify cache
16
- # settings with this object.
60
+ # This is the {https://github.com/DannyBen/lightly Lightly} cache object.
61
+ # You can access or modify cache settings with this object.
62
+ #
63
+ # === Example
64
+ #
65
+ # client = Client.new
66
+ # client.cache.life = 3600
67
+ # client.cache.dir = './cache'
68
+ # client.cache.disable
69
+ # client.cache.enable
70
+ #
17
71
  def cache
18
72
  @cache ||= Lightly.new
19
73
  end
20
74
 
21
- # Override this method in order to merge parameters into the query
22
- # string.
23
- def default_query; {}; end
75
+ # Override this method in order to merge parameters into the query
76
+ # string before each call.
77
+ #
78
+ # === Example
79
+ #
80
+ # class Client < APICake::Base
81
+ # base_uri: 'http://some.api.com/v3'
82
+ #
83
+ # def initialize(api_key)
84
+ # @api_key = api_key
85
+ # end
86
+ #
87
+ # def default_query
88
+ # { api_key: @api_key }
89
+ # end
90
+ # end
91
+ #
92
+ # client = Client.new 'secret'
93
+ # client.some_path param: 'value'
94
+ # p client.last_url
95
+ # # => "http://some.api.com/v3/some_path?api_key=secret&param=value"
96
+ #
97
+ def default_query; {}; end
24
98
 
25
99
  # Override this method in order to merge parameters into the HTTParty
26
- # get request.
27
- # See http://www.rubydoc.info/github/jnunemaker/httparty/HTTParty/ClassMethods
100
+ # get request.
101
+ #
102
+ # === Eexample
103
+ #
104
+ # class Client < APICake::Base
105
+ # base_uri: 'http://some.api.com/v3'
106
+ #
107
+ # def initialize(user, pass)
108
+ # @user, @pass = user, pass
109
+ # end
110
+ #
111
+ # def default_params
112
+ # { basic_auth: { username: user, password: pass }
113
+ # end
114
+ # end
115
+ #
116
+ # @see http://www.rubydoc.info/github/jnunemaker/httparty/HTTParty/ClassMethods HTTParty Class Methods documentation
117
+ #
28
118
  def default_params; {}; end
29
119
 
30
120
  # Make a request or get it from cache, and return the parsed response.
31
- def get(path, extra=nil, params={})
32
- get!(path, extra, params).parsed_response
121
+ #
122
+ # This is the same as calling {#get!} and gettings its +parsed_response+
123
+ # value.
124
+ #
125
+ # Normally, you should not have the need to use this method, since all
126
+ # unhandled method calls are handled by {#method_missing} and are
127
+ # delegated here.
128
+ #
129
+ # === Example
130
+ #
131
+ # client = Client.new
132
+ # client.get 'path/to/resource', param: value, param: value
133
+ #
134
+ def get(path, extra = nil, params = {})
135
+ get!(path.clone, extra.clone, params.clone).parsed_response
33
136
  end
34
137
 
35
- # Make a request or get it from cache, and return the entire payload
36
- # objkect.
37
- def get!(path, extra=nil, params={})
138
+ # Make a request or get it from cache, and return the entire {Payload}
139
+ # object.
140
+ def get!(path, extra = nil, params = {})
38
141
  path, extra, params = normalize path, extra, params
39
142
  key = cache_key path, extra, params
40
143
 
@@ -46,28 +149,39 @@ module APICake
46
149
  @last_payload
47
150
  end
48
151
 
49
- # A shortcut to just get the constructed URL. Note that this call will
50
- # make the request (unless it is already cached).
51
- def url(path, extra=nil, params={})
152
+ # A shortcut to just get the constructed URL of the request.
153
+ # Note that this call will make the HTTP request (unless it is already
154
+ # cached).
155
+ #
156
+ # If you have already made the request, you can use {#last_url} instead.
157
+ def url(path, extra = nil, params = {})
52
158
  get! path, extra, params
53
159
  last_url
54
160
  end
55
161
 
56
162
  # Save the response body to a file
57
- def save(filename, path, params={})
163
+ #
164
+ # === Example
165
+ #
166
+ # client = Client.new
167
+ # client.save 'out.json', 'some/to/resource', param: value
168
+ #
169
+ def save(filename, path, params = {})
58
170
  payload = get! path, nil, params
59
171
  File.write filename, payload.response.body
60
172
  end
61
173
 
62
- # Forwards all arguments to #get! and converts its parsed response to
63
- # CSV. If the response contains one or more arrays, the first array will
64
- # be the CSV output, otherwise, the response itself will be used.
65
- # You can override this method if you wish to provide a different
174
+ # This method uses {#get!} to download and parse the content. It then
175
+ # makes the best effort to convert the right part of the data to a
176
+ # CSV string.
177
+ #
178
+ # You can override this method if you wish to provide a different
66
179
  # behavior.
180
+ #
67
181
  def get_csv(*args)
68
182
  payload = get!(*args)
69
183
 
70
- if payload.response.code != '200'
184
+ if payload.response.code != "200"
71
185
  raise BadResponse, "#{payload.response.code} #{payload.response.msg}"
72
186
  end
73
187
 
@@ -76,7 +190,7 @@ module APICake
76
190
  unless response.is_a? Hash
77
191
  raise BadResponse, "Cannot parse response"
78
192
  end
79
-
193
+
80
194
  data = csv_node response
81
195
 
82
196
  header = data.first.keys
@@ -88,16 +202,22 @@ module APICake
88
202
  result
89
203
  end
90
204
 
91
- # Send a request, convert it to CSV and save it to a file.
205
+ # Same as {#save}, only use the output of {#get_csv} instead of the
206
+ # response body.
92
207
  def save_csv(file, *args)
93
208
  File.write file, get_csv(*args)
94
209
  end
95
210
 
96
- # Determins which part of the data is best suited to be displayed
97
- # as CSV.
98
- # - In case there is an array in the data, it will be returned.
99
- # - Otherwise, we will use the entire response as a single row CSV.
211
+ # Determins which part of the data is best suited to be displayed
212
+ # as CSV.
213
+ #
214
+ # - If the response contains one or more arrays, the first array will
215
+ # be the CSV output
216
+ # - Otherwise, if the response was parsed to a ruby object, the response
217
+ # itself will be used as a single-row CSV output.
218
+ #
100
219
  # Override this if you want to have a different decision process.
220
+ #
101
221
  def csv_node(data)
102
222
  arrays = data.keys.select { |key| data[key].is_a? Array }
103
223
  arrays.empty? ? [data] : data[arrays.first]
@@ -106,20 +226,20 @@ module APICake
106
226
  private
107
227
 
108
228
  # Make a call with HTTParty and return a payload object.
109
- def http_get(path, extra=nil, params={})
229
+ def http_get(path, extra = nil, params = {})
110
230
  payload = self.class.get path, params
111
231
  APICake::Payload.new payload
112
232
  end
113
233
 
114
234
  # Normalize the three input parameters
115
- def normalize(path, extra=nil, params={})
235
+ def normalize(path, extra = nil, params = {})
116
236
  if extra.is_a?(Hash) and params.empty?
117
237
  params = extra
118
238
  extra = nil
119
239
  end
120
240
 
121
241
  path = "#{path}/#{extra}" if extra
122
- path = "/#{path}" unless path[0] == '/'
242
+ path = "/#{path}" unless path[0] == "/"
123
243
 
124
244
  query = default_query.merge params
125
245
 
@@ -1,3 +1,3 @@
1
1
  module APICake
2
2
  class BadResponse < StandardError; end
3
- end
3
+ end
@@ -3,14 +3,14 @@ module APICake
3
3
  attr_reader :request, :response, :headers, :parsed_response
4
4
 
5
5
  def initialize(response)
6
- @request = response.request
7
- @headers = response.headers
8
- @response = response.response
6
+ @request = response.request
7
+ @headers = response.headers
8
+ @response = response.response
9
9
  @parsed_response = response.parsed_response
10
10
  end
11
11
 
12
12
  def to_h
13
- { request: request, response: response, headers: headers,
13
+ { request: request, response: response, headers: headers,
14
14
  parsed_response: parsed_response }
15
15
  end
16
16
 
@@ -1,3 +1,3 @@
1
1
  module APICake
2
- VERSION = "0.1.0"
3
- end
2
+ VERSION = "0.1.4"
3
+ end
data/lib/apicake.rb CHANGED
@@ -1,4 +1,4 @@
1
- require 'apicake/version'
2
- require 'apicake/base'
3
- require 'apicake/payload'
4
- require 'apicake/exceptions'
1
+ require "apicake/version"
2
+ require "apicake/base"
3
+ require "apicake/payload"
4
+ require "apicake/exceptions"
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: apicake
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
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-18 00:00:00.000000000 Z
11
+ date: 2021-09-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: lightly
@@ -38,118 +38,6 @@ dependencies:
38
38
  - - "~>"
39
39
  - !ruby/object:Gem::Version
40
40
  version: '0.14'
41
- - !ruby/object:Gem::Dependency
42
- name: runfile
43
- requirement: !ruby/object:Gem::Requirement
44
- requirements:
45
- - - "~>"
46
- - !ruby/object:Gem::Version
47
- version: '0.8'
48
- type: :development
49
- prerelease: false
50
- version_requirements: !ruby/object:Gem::Requirement
51
- requirements:
52
- - - "~>"
53
- - !ruby/object:Gem::Version
54
- version: '0.8'
55
- - !ruby/object:Gem::Dependency
56
- name: runfile-tasks
57
- requirement: !ruby/object:Gem::Requirement
58
- requirements:
59
- - - "~>"
60
- - !ruby/object:Gem::Version
61
- version: '0.4'
62
- type: :development
63
- prerelease: false
64
- version_requirements: !ruby/object:Gem::Requirement
65
- requirements:
66
- - - "~>"
67
- - !ruby/object:Gem::Version
68
- version: '0.4'
69
- - !ruby/object:Gem::Dependency
70
- name: rspec
71
- requirement: !ruby/object:Gem::Requirement
72
- requirements:
73
- - - "~>"
74
- - !ruby/object:Gem::Version
75
- version: '3.5'
76
- type: :development
77
- prerelease: false
78
- version_requirements: !ruby/object:Gem::Requirement
79
- requirements:
80
- - - "~>"
81
- - !ruby/object:Gem::Version
82
- version: '3.5'
83
- - !ruby/object:Gem::Dependency
84
- name: rdoc
85
- requirement: !ruby/object:Gem::Requirement
86
- requirements:
87
- - - "~>"
88
- - !ruby/object:Gem::Version
89
- version: '5.0'
90
- type: :development
91
- prerelease: false
92
- version_requirements: !ruby/object:Gem::Requirement
93
- requirements:
94
- - - "~>"
95
- - !ruby/object:Gem::Version
96
- version: '5.0'
97
- - !ruby/object:Gem::Dependency
98
- name: byebug
99
- requirement: !ruby/object:Gem::Requirement
100
- requirements:
101
- - - "~>"
102
- - !ruby/object:Gem::Version
103
- version: '9.0'
104
- type: :development
105
- prerelease: false
106
- version_requirements: !ruby/object:Gem::Requirement
107
- requirements:
108
- - - "~>"
109
- - !ruby/object:Gem::Version
110
- version: '9.0'
111
- - !ruby/object:Gem::Dependency
112
- name: simplecov
113
- requirement: !ruby/object:Gem::Requirement
114
- requirements:
115
- - - "~>"
116
- - !ruby/object:Gem::Version
117
- version: '0.13'
118
- type: :development
119
- prerelease: false
120
- version_requirements: !ruby/object:Gem::Requirement
121
- requirements:
122
- - - "~>"
123
- - !ruby/object:Gem::Version
124
- version: '0.13'
125
- - !ruby/object:Gem::Dependency
126
- name: require_all
127
- requirement: !ruby/object:Gem::Requirement
128
- requirements:
129
- - - "~>"
130
- - !ruby/object:Gem::Version
131
- version: '1.4'
132
- type: :development
133
- prerelease: false
134
- version_requirements: !ruby/object:Gem::Requirement
135
- requirements:
136
- - - "~>"
137
- - !ruby/object:Gem::Version
138
- version: '1.4'
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
41
  description: Build Dynamic API Wrappers
154
42
  email: db@dannyben.com
155
43
  executables: []
@@ -165,7 +53,9 @@ files:
165
53
  homepage: https://github.com/DannyBen/apicake
166
54
  licenses:
167
55
  - MIT
168
- metadata: {}
56
+ metadata:
57
+ bug_tracker_uri: https://github.com/DannyBen/apicake/issues
58
+ source_code_uri: https://github.com/DannyBen/apicake
169
59
  post_install_message:
170
60
  rdoc_options: []
171
61
  require_paths:
@@ -174,15 +64,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
174
64
  requirements:
175
65
  - - ">="
176
66
  - !ruby/object:Gem::Version
177
- version: 2.0.0
67
+ version: '2.4'
178
68
  required_rubygems_version: !ruby/object:Gem::Requirement
179
69
  requirements:
180
70
  - - ">="
181
71
  - !ruby/object:Gem::Version
182
72
  version: '0'
183
73
  requirements: []
184
- rubyforge_project:
185
- rubygems_version: 2.6.6
74
+ rubygems_version: 3.2.25
186
75
  signing_key:
187
76
  specification_version: 4
188
77
  summary: API Building Toolkit