apicake 0.1.0 → 0.1.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 6e7c41c224b2b2435828e862a3047a50aaf7ea6e
4
- data.tar.gz: eacee61fff039351173484ea34db7d7d19a11b12
3
+ metadata.gz: c1d09e6b00d7fb1d19163aa2e7a6912dbf571053
4
+ data.tar.gz: 48c802fe5b974895e65fd2808b2cdf711087087a
5
5
  SHA512:
6
- metadata.gz: e08bb8197889886c32400bc2bc035a924d730b71eb9c03f2bd4b74e91cfdda7678693c6de867d204d6efb3cd37011ff8e2513b237dcce23151f4a380ab8e8ebe
7
- data.tar.gz: c385af56680fbad9d96437f4ae9e9a24feea6a3250a9f83d6ec7e9dfd6eaa53f7fe93017e480e1fe64bea32d0effe37d3e9a0f623dc562659e65b265eb63d3ae
6
+ metadata.gz: c95806cee75f5e4574ece5ce97bae1c51c345506ec42d7ae5c6b1dd0ea12100269bbce230b725942eb603f7f80028cf40740e453113faa95f6ce314401a25b25
7
+ data.tar.gz: d324e21a09236ea45bcd9cbef8e7e702e92b52a519b877534a3c1d5f1ba3c0030fe074f106f2d525ef5b76d69848072d36e7243f00399857fb89e92f5970ce15
data/README.md CHANGED
@@ -109,7 +109,8 @@ See the [caching example][3].
109
109
  Method Reference
110
110
  --------------------------------------------------
111
111
 
112
- Documentation to be completed
112
+ For a detailed explanation of the services and methods you get when inheriting
113
+ from `APICake::Base`, see the [class documentation][4].
113
114
 
114
115
 
115
116
  ---
@@ -117,3 +118,4 @@ Documentation to be completed
117
118
  [1]: https://github.com/DannyBen/apicake/tree/master/examples
118
119
  [2]: https://github.com/DannyBen/lightly
119
120
  [3]: https://github.com/DannyBen/apicake/blob/master/examples/04-caching.rb
121
+ [4]: http://www.rubydoc.info/gems/apicake/APICake/Base
data/lib/apicake/base.rb CHANGED
@@ -2,38 +2,142 @@ require 'httparty'
2
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
30
+
9
31
 
10
- # Any undefined method will be delegated to the #get method
32
+ # Holds the last URL that was downloaded by the last call to
33
+ # {#get}, {#get!}, {#get_csv} or {#save_csv}.
34
+ #
35
+ # === Example
36
+ #
37
+ # client = Client.new
38
+ # client.some_path, param: value
39
+ # p client.last_url
40
+ # # => "http://some.api.com/v3/some_path?param=value"
41
+ #
42
+ attr_reader :last_url
43
+
44
+ # Any undefined method call will be delegated to the {#get} method.
45
+ #
46
+ # === Example
47
+ #
48
+ # This:
49
+ #
50
+ # client = Client.new
51
+ # client.path 'optional_sub_path', optional_param: value, optional_param: value
52
+ #
53
+ # Is equivalent to this:
54
+ #
55
+ # client.get 'path/optional_sub_path', optional_param: value, optional_param: value
56
+ #
11
57
  def method_missing(method_sym, *arguments, &_block)
12
58
  get "/#{method_sym}", *arguments
13
59
  end
14
60
 
15
- # This is the Lightly cache object. You can access or modify cache
16
- # settings with this object.
61
+ # This is the {https://github.com/DannyBen/lightly Lightly} cache object.
62
+ # You can access or modify cache settings with this object.
63
+ #
64
+ # === Example
65
+ #
66
+ # client = Client.new
67
+ # client.cache.life = 3600
68
+ # client.cache.dir = './cache'
69
+ # client.cache.disable
70
+ # client.cache.enable
71
+ #
17
72
  def cache
18
73
  @cache ||= Lightly.new
19
74
  end
20
75
 
21
76
  # Override this method in order to merge parameters into the query
22
- # string.
77
+ # string before each call.
78
+ #
79
+ # === Example
80
+ #
81
+ # class Client < APICake::Base
82
+ # base_uri: 'http://some.api.com/v3'
83
+ #
84
+ # def initialize(api_key)
85
+ # @api_key = api_key
86
+ # end
87
+ #
88
+ # def default_query
89
+ # { api_key: @api_key }
90
+ # end
91
+ # end
92
+ #
93
+ # client = Client.new 'secret'
94
+ # client.some_path param: 'value'
95
+ # p client.last_url
96
+ # # => "http://some.api.com/v3/some_path?api_key=secret&param=value"
97
+ #
23
98
  def default_query; {}; end
24
99
 
25
100
  # Override this method in order to merge parameters into the HTTParty
26
101
  # get request.
27
- # See http://www.rubydoc.info/github/jnunemaker/httparty/HTTParty/ClassMethods
102
+ #
103
+ # === Eexample
104
+ #
105
+ # class Client < APICake::Base
106
+ # base_uri: 'http://some.api.com/v3'
107
+ #
108
+ # def initialize(user, pass)
109
+ # @user, @pass = user, pass
110
+ # end
111
+ #
112
+ # def default_params
113
+ # { basic_auth: { username: user, password: pass }
114
+ # end
115
+ # end
116
+ #
117
+ # @see http://www.rubydoc.info/github/jnunemaker/httparty/HTTParty/ClassMethods HTTParty Class Methods documentation
118
+ #
28
119
  def default_params; {}; end
29
120
 
30
121
  # Make a request or get it from cache, and return the parsed response.
122
+ #
123
+ # This is the same as calling {#get!} and gettings its +parsed_response+
124
+ # value.
125
+ #
126
+ # Normally, you should not have the need to use this method, since all
127
+ # unhandled method calls are handled by {#method_missing} and are
128
+ # delegated here.
129
+ #
130
+ # === Example
131
+ #
132
+ # client = Client.new
133
+ # client.get 'path/to/resource', param: value, param: value
134
+ #
31
135
  def get(path, extra=nil, params={})
32
136
  get!(path, extra, params).parsed_response
33
137
  end
34
138
 
35
- # Make a request or get it from cache, and return the entire payload
36
- # objkect.
139
+ # Make a request or get it from cache, and return the entire {Payload}
140
+ # object.
37
141
  def get!(path, extra=nil, params={})
38
142
  path, extra, params = normalize path, extra, params
39
143
  key = cache_key path, extra, params
@@ -46,24 +150,35 @@ module APICake
46
150
  @last_payload
47
151
  end
48
152
 
49
- # A shortcut to just get the constructed URL. Note that this call will
50
- # make the request (unless it is already cached).
153
+ # A shortcut to just get the constructed URL of the request.
154
+ # Note that this call will make the HTTP request (unless it is already
155
+ # cached).
156
+ #
157
+ # If you have already made the request, you can use {#last_url} instead.
51
158
  def url(path, extra=nil, params={})
52
159
  get! path, extra, params
53
160
  last_url
54
161
  end
55
162
 
56
163
  # Save the response body to a file
164
+ #
165
+ # === Example
166
+ #
167
+ # client = Client.new
168
+ # client.save 'out.json', 'some/to/resource', param: value
169
+ #
57
170
  def save(filename, path, params={})
58
171
  payload = get! path, nil, params
59
172
  File.write filename, payload.response.body
60
173
  end
61
174
 
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.
175
+ # This method uses {#get!} to download and parse the content. It then
176
+ # makes the best effort to convert the right part of the data to a
177
+ # CSV string.
178
+ #
65
179
  # You can override this method if you wish to provide a different
66
180
  # behavior.
181
+ #
67
182
  def get_csv(*args)
68
183
  payload = get!(*args)
69
184
 
@@ -88,16 +203,22 @@ module APICake
88
203
  result
89
204
  end
90
205
 
91
- # Send a request, convert it to CSV and save it to a file.
206
+ # Same as {#save}, only use the output of {#get_csv} instead of the
207
+ # response body.
92
208
  def save_csv(file, *args)
93
209
  File.write file, get_csv(*args)
94
210
  end
95
211
 
96
212
  # Determins which part of the data is best suited to be displayed
97
213
  # 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.
214
+ #
215
+ # - If the response contains one or more arrays, the first array will
216
+ # be the CSV output
217
+ # - Otherwise, if the response was parsed to a ruby object, the response
218
+ # itself will be used as a single-row CSV output.
219
+ #
100
220
  # Override this if you want to have a different decision process.
221
+ #
101
222
  def csv_node(data)
102
223
  arrays = data.keys.select { |key| data[key].is_a? Array }
103
224
  arrays.empty? ? [data] : data[arrays.first]
@@ -1,3 +1,3 @@
1
1
  module APICake
2
- VERSION = "0.1.0"
2
+ VERSION = "0.1.1"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
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.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Danny Ben Shitrit