apicake 0.0.1 → 0.0.2

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: 2e6b6cb8fc605e0694774ccfb447a645c22514a8
4
- data.tar.gz: e7331e6c9517f5eac47d26547ef944ec83fd5a4e
3
+ metadata.gz: 2e430833ce1d205a599a9790cf91c76c752f950b
4
+ data.tar.gz: 7329dbbb5fab3f75498ae43c2c93e74557a92ef1
5
5
  SHA512:
6
- metadata.gz: 90526475b421ac3b3914af56455de4376fc82d9f377880b4aa353a19ece6b0932b593109a07adf8ce54368d58098bd836fd457a1f979a35fc11e2d610dc063dd
7
- data.tar.gz: 2a43afac9912fd1ca3d9b0806993c74b0741acdaaff9b42e5a28bee9a71c627e3e63b22d7e5ae49b7a147f8f75257ef86ba2eb6877d699056084d03443bda5dc
6
+ metadata.gz: 772410ef64c4a957267f2b33e37e21565bc7e684e05cf5e7f695abdaeca5cf73b86237b213bf3ad41bfa530f0725bec7e1e2a20beea9b41bc02131828f82b447
7
+ data.tar.gz: e0bd4acf2f34a15aee3879468aef787ab7d958aa02342a6ed5e047459a5b1c810d4a0c37e90bd98da6c1b06345f12b4731038726c317b5380d839a99156120bb
data/README.md CHANGED
@@ -1,6 +1,21 @@
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
+
9
+
10
+ ---
11
+
12
+ This gem allows you to easily build rich API wrappers with minimal code.
13
+
14
+ It is HTTParty with a Cake.
15
+
16
+ ---
17
+
18
+
4
19
  Install
5
20
  --------------------------------------------------
6
21
 
@@ -13,3 +28,81 @@ Or with bundler:
13
28
  ```ruby
14
29
  gem 'apicake'
15
30
  ```
31
+
32
+
33
+ TL;DR
34
+ --------------------------------------------------
35
+
36
+ Turn this hypothetical API URL:
37
+
38
+ ```
39
+ http://api.recipies.com/cakes?layers=3
40
+ ```
41
+
42
+ To this:
43
+
44
+ ```ruby
45
+ Recipies.cakes layers:3
46
+ ```
47
+
48
+ Using this code only:
49
+
50
+ ```ruby
51
+ class Recipies < APICake::Base
52
+ base_url 'api.recipies.com'
53
+ end
54
+ ```
55
+
56
+
57
+ Features
58
+ --------------------------------------------------
59
+
60
+ - Uses HTTParty
61
+ - Built in caching
62
+ - Built in save to file
63
+ - Built in response parsing (part of HTTParty)
64
+ - Designed for GET-only APIs (e.g., data services)
65
+
66
+
67
+ Usage
68
+ --------------------------------------------------
69
+
70
+ Create a class and inherit from `APICake::Base`.
71
+
72
+ This class automatically includes HTTParty, so you can do whatever you do in
73
+ HTTParty. In addition, the `APICake::Base` class defines a `method_missing`
74
+ method, so any call to an undefined method, will simply be converted to a
75
+ URL.
76
+
77
+ For example:
78
+
79
+ ```ruby
80
+ class Recipies << APICake::Base
81
+ base_url 'api.recipies.com/v1'
82
+ end
83
+
84
+ recipies = Rcipies.new
85
+
86
+ # This will access http://api.recipies.com/v1/cakes
87
+ recipies.cakes
88
+
89
+ # This will access http://api.recipies.com/v1/cakes/chocolate
90
+ recipies.cakes 'chocolate'
91
+
92
+ # This will access http://api.recipies.com/v1/cakes/chocolate?layers=3
93
+ recipies.cakes 'chocolate', layers: 3
94
+ ```
95
+
96
+
97
+ Caching
98
+ --------------------------------------------------
99
+
100
+ Documentation to be completed
101
+
102
+
103
+ Method Reference
104
+ --------------------------------------------------
105
+
106
+ Documentation to be completed
107
+
108
+
data/lib/apicake/base.rb CHANGED
@@ -5,21 +5,10 @@ module APICake
5
5
  class Base
6
6
  include HTTParty
7
7
 
8
- @@disable_dynamic_methods = false
9
-
10
- # Disable dynamic methods. This is enabled by default.
11
- # The main reason you may want to disable it sometimes is for easier
12
- # debugging.
13
- def self.disable_dynamic_methods
14
- @@disable_dynamic_methods = true
15
- end
8
+ attr_reader :last_payload
16
9
 
17
10
  def method_missing(method_sym, *arguments, &_block)
18
- if @@disable_dynamic_methods
19
- super
20
- else
21
- get "/#{method_sym}", *arguments
22
- end
11
+ get "/#{method_sym}", *arguments
23
12
  end
24
13
 
25
14
  def cache
@@ -30,6 +19,18 @@ module APICake
30
19
  def default_params; {}; end
31
20
  def default_query; {}; end
32
21
 
22
+ def get(path, extra=nil, params={})
23
+ get!(path, extra, params).parsed_response
24
+ end
25
+
26
+ def get!(path, extra=nil, params={})
27
+ key = "#{self.class.base_uri}+#{path}+#{extra}+#{params}"
28
+
29
+ @last_payload = cache.get key do
30
+ http_get(path, extra, params)
31
+ end
32
+ end
33
+
33
34
  def url(path, extra=nil, params={})
34
35
  payload = get! path, extra, params
35
36
  payload.request.last_uri.to_s
@@ -40,17 +41,7 @@ module APICake
40
41
  File.write filename, payload.response.body
41
42
  end
42
43
 
43
- def get(path, extra=nil, params={})
44
- get!(path, extra, params).parsed_response
45
- end
46
-
47
- def get!(path, extra=nil, params={})
48
- key = "#{path}+#{extra}+#{params}"
49
-
50
- cache.get key do
51
- http_get(path, extra, params)
52
- end
53
- end
44
+ private
54
45
 
55
46
  def http_get(path, extra=nil, params={})
56
47
  if extra.is_a?(Hash) and params.empty?
@@ -68,4 +59,4 @@ module APICake
68
59
  APICake::Payload.new payload
69
60
  end
70
61
  end
71
- end
62
+ end
@@ -9,11 +9,10 @@ module APICake
9
9
  @parsed_response = response.parsed_response
10
10
  end
11
11
 
12
- def inspect
12
+ def to_h
13
13
  { request: request, response: response, headers: headers,
14
14
  parsed_response: parsed_response }
15
15
  end
16
- alias_method :to_h, :inspect
17
16
 
18
17
  def to_s
19
18
  parsed_response.to_s
@@ -1,3 +1,3 @@
1
1
  module APICake
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
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.1
4
+ version: 0.0.2
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-13 00:00:00.000000000 Z
11
+ date: 2017-02-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: lightly