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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 2e430833ce1d205a599a9790cf91c76c752f950b
4
- data.tar.gz: 7329dbbb5fab3f75498ae43c2c93e74557a92ef1
3
+ metadata.gz: 40e714671fc06714a55011b766e49b9b9864f844
4
+ data.tar.gz: 44de7eae53d8dc0de613d8a50b7a1ec69b6d66b0
5
5
  SHA512:
6
- metadata.gz: 772410ef64c4a957267f2b33e37e21565bc7e684e05cf5e7f695abdaeca5cf73b86237b213bf3ad41bfa530f0725bec7e1e2a20beea9b41bc02131828f82b447
7
- data.tar.gz: e0bd4acf2f34a15aee3879468aef787ab7d958aa02342a6ed5e047459a5b1c810d4a0c37e90bd98da6c1b06345f12b4731038726c317b5380d839a99156120bb
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
- Documentation to be completed
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
- # Overridables
19
- def default_params; {}; end
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
- key = "#{self.class.base_uri}+#{path}+#{extra}+#{params}"
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
- payload = get! path, extra, params
36
- payload.request.last_uri.to_s
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
- params[:query] = default_query.merge params
80
+ query = default_query.merge params
81
+
82
+ params[:query] = query unless query.empty?
56
83
  params = default_params.merge params
57
84
 
58
- payload = self.class.get path, params
59
- APICake::Payload.new payload
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
@@ -1,3 +1,3 @@
1
1
  module APICake
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: apicake
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-16 00:00:00.000000000 Z
11
+ date: 2017-02-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: lightly