apicake 0.0.2 → 0.0.3
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 +4 -4
- data/README.md +11 -1
- data/lib/apicake/base.rb +40 -9
- data/lib/apicake/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 40e714671fc06714a55011b766e49b9b9864f844
|
4
|
+
data.tar.gz: 44de7eae53d8dc0de613d8a50b7a1ec69b6d66b0
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 72f08448fd1f559f837dfd6d689a86b4832d8b75e3259fabc82c7c2a23c2d6c512b9e9bce6e19921b39fab0726d6b8f409bba15938461d5a61e8c2cc31bf2c10
|
7
|
+
data.tar.gz: 6e5d65bd2804c5b325c96a3f459408f239f3b3f7b3984ed1c844b41d10a380f70aa33f6bab7cebb949001bc18d11bbad9cd0fa94b280bad36b2ff7912ef2af7d
|
data/README.md
CHANGED
@@ -93,11 +93,16 @@ recipies.cakes 'chocolate'
|
|
93
93
|
recipies.cakes 'chocolate', layers: 3
|
94
94
|
```
|
95
95
|
|
96
|
+
See the [Examples folder][1] for more examples.
|
97
|
+
|
96
98
|
|
97
99
|
Caching
|
98
100
|
--------------------------------------------------
|
99
101
|
|
100
|
-
|
102
|
+
APICake uses [Lightly][2] for caching. By default, cached objects are stored
|
103
|
+
in the `./cache` directory for 3600 seconds.
|
104
|
+
|
105
|
+
See the [caching example][3].
|
101
106
|
|
102
107
|
|
103
108
|
Method Reference
|
@@ -106,3 +111,8 @@ Method Reference
|
|
106
111
|
Documentation to be completed
|
107
112
|
|
108
113
|
|
114
|
+
---
|
115
|
+
|
116
|
+
[1]: https://github.com/DannyBen/apicake/tree/master/examples
|
117
|
+
[2]: https://github.com/DannyBen/lightly
|
118
|
+
[3]: https://github.com/DannyBen/apicake/blob/master/examples/04-caching.rb
|
data/lib/apicake/base.rb
CHANGED
@@ -5,37 +5,55 @@ module APICake
|
|
5
5
|
class Base
|
6
6
|
include HTTParty
|
7
7
|
|
8
|
-
attr_reader :last_payload
|
8
|
+
attr_reader :last_payload, :last_url
|
9
9
|
|
10
|
+
# Any undefined method will be delegated to the #get method
|
10
11
|
def method_missing(method_sym, *arguments, &_block)
|
11
12
|
get "/#{method_sym}", *arguments
|
12
13
|
end
|
13
14
|
|
15
|
+
# This is the Lightly cache object. You can access or modify cache
|
16
|
+
# settings with this object.
|
14
17
|
def cache
|
15
18
|
@cache ||= Lightly.new
|
16
19
|
end
|
17
20
|
|
18
|
-
#
|
19
|
-
|
21
|
+
# Override this method in order to merge parameters into the query
|
22
|
+
# string.
|
20
23
|
def default_query; {}; end
|
21
24
|
|
25
|
+
# 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
|
28
|
+
def default_params; {}; end
|
29
|
+
|
30
|
+
# Make a request or get it from cache, and return the parsed response.
|
22
31
|
def get(path, extra=nil, params={})
|
23
32
|
get!(path, extra, params).parsed_response
|
24
33
|
end
|
25
34
|
|
35
|
+
# Make a request or get it from cache, and return the entire payload
|
36
|
+
# objkect.
|
26
37
|
def get!(path, extra=nil, params={})
|
27
|
-
|
38
|
+
path, extra, params = normalize path, extra, params
|
39
|
+
key = cache_key path, extra, params
|
28
40
|
|
29
41
|
@last_payload = cache.get key do
|
30
42
|
http_get(path, extra, params)
|
31
43
|
end
|
44
|
+
|
45
|
+
@last_url = @last_payload.request.last_uri.to_s
|
46
|
+
@last_payload
|
32
47
|
end
|
33
48
|
|
49
|
+
# A shortcut to just get the constructed URL. Note that this call will
|
50
|
+
# make the request (unless it is already cached).
|
34
51
|
def url(path, extra=nil, params={})
|
35
|
-
|
36
|
-
|
52
|
+
get! path, extra, params
|
53
|
+
last_url
|
37
54
|
end
|
38
55
|
|
56
|
+
# Save the response body to a file
|
39
57
|
def save(filename, path, params={})
|
40
58
|
payload = get! path, nil, params
|
41
59
|
File.write filename, payload.response.body
|
@@ -43,7 +61,14 @@ module APICake
|
|
43
61
|
|
44
62
|
private
|
45
63
|
|
64
|
+
# Make a call with HTTParty and return a payload object.
|
46
65
|
def http_get(path, extra=nil, params={})
|
66
|
+
payload = self.class.get path, params
|
67
|
+
APICake::Payload.new payload
|
68
|
+
end
|
69
|
+
|
70
|
+
# Normalize the three input parameters
|
71
|
+
def normalize(path, extra=nil, params={})
|
47
72
|
if extra.is_a?(Hash) and params.empty?
|
48
73
|
params = extra
|
49
74
|
extra = nil
|
@@ -52,11 +77,17 @@ module APICake
|
|
52
77
|
path = "#{path}/#{extra}" if extra
|
53
78
|
path = "/#{path}" unless path[0] == '/'
|
54
79
|
|
55
|
-
|
80
|
+
query = default_query.merge params
|
81
|
+
|
82
|
+
params[:query] = query unless query.empty?
|
56
83
|
params = default_params.merge params
|
57
84
|
|
58
|
-
|
59
|
-
|
85
|
+
[path, extra, params]
|
86
|
+
end
|
87
|
+
|
88
|
+
# The key for the cache object
|
89
|
+
def cache_key(path, extra, params)
|
90
|
+
"#{self.class.base_uri}+#{path}+#{extra}+#{params}"
|
60
91
|
end
|
61
92
|
end
|
62
93
|
end
|
data/lib/apicake/version.rb
CHANGED
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.0.
|
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
|
+
date: 2017-02-17 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: lightly
|