paper-cup 0.1.2 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +29 -5
- data/lib/paper_cup/client.rb +52 -0
- data/lib/paper_cup.rb +3 -1
- data/paper-cup.gemspec +1 -1
- data/test/paper_cup_test/client_test.rb +36 -0
- metadata +3 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8c341398114a2a9df8db16fdabaafaeddc12112c
|
4
|
+
data.tar.gz: faf80ab69ab9ab93de024834cd141791c6332b06
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d438c4b0506ccd7bb9da19cb1f8a02f533ac3b82fd278d0a37afde50cfbad6fbfb72e2a510bb07cf13665bc890cc4331585830aeeba48418d40af6d58c90d544
|
7
|
+
data.tar.gz: 817983209a7a5605beba459718e75b225c6d2b65a911f381dd46cade61bc9f357632f3cdd78f6c71a028a0bf9916dbec7e1c2e41d3ae29de3f5d7f3bd7771702
|
data/README.md
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
# PaperCup
|
2
2
|
|
3
|
-
![TravisCI Badge](https://
|
3
|
+
![TravisCI Badge](https://travis-ci.org/casapick/paper-cup.svg)
|
4
4
|
|
5
5
|
Instalation
|
6
6
|
----
|
@@ -23,20 +23,44 @@ For all the methods it accepts the next parameters:
|
|
23
23
|
|
24
24
|
* url: is mandatory
|
25
25
|
* headers: optional, it has to be a hash. Example: `{ "Content-Type" => "application/json"}`
|
26
|
-
* params: optional, same as the header. Example: { name: "Goku", race: "Saiyan" }
|
26
|
+
* params: optional, same as the header. Example: `{ name: "Goku", race: "Saiyan" }`
|
27
27
|
* body: optional, the body will be inserted with the [`-d`](http://curl.haxx.se/docs/manpage.html#-d) option of curl as it come.
|
28
28
|
|
29
|
-
All the method returns a [`Response`](https://github.com/casapick/paper-cup/blob/master/lib/paper_cup/response.rb) object, that has two attributes status and body
|
29
|
+
All the method returns a [`Response`](https://github.com/casapick/paper-cup/blob/master/lib/paper_cup/response.rb) object, that has two attributes `status` and `body`.
|
30
|
+
|
31
|
+
### Using a client
|
32
|
+
You can also instantiate a client and store to make continous calls to the same domain. It can have default params and headers that will be used on all calls.
|
33
|
+
|
34
|
+
~~~ruby
|
35
|
+
client = PaperCup::Client.new url: 'https://api.github.com'
|
36
|
+
client.get('users/casapick')
|
37
|
+
~~~
|
38
|
+
|
39
|
+
You can also "persist" an endpoint. You also can append headers and params:
|
40
|
+
|
41
|
+
~~~ruby
|
42
|
+
users = client.endpoint(:users)
|
43
|
+
=> #<PaperCup::Client:0x007fba325a5188 @options={:url=>"https://api.github.com/users", :headers=>{}, :params=>{}}>
|
44
|
+
|
45
|
+
response = users.get(:casapick)
|
46
|
+
response.body
|
47
|
+
=> {"login"=>"casapick",
|
48
|
+
"id"=>8136680,
|
49
|
+
"avatar_url"=>"https://avatars.githubusercontent.com/u/8136680?v=3",
|
50
|
+
# ...
|
51
|
+
"created_at"=>"2014-07-11T15:41:22Z",
|
52
|
+
"updated_at"=>"2015-07-29T18:39:00Z"}
|
53
|
+
~~~
|
30
54
|
|
31
55
|
GET
|
32
56
|
----
|
33
57
|
|
34
|
-
`PaperCup.get(url
|
58
|
+
`PaperCup.get(url)`
|
35
59
|
|
36
60
|
POST - PUT
|
37
61
|
----------
|
38
62
|
|
39
|
-
`PaperCup.post(url
|
63
|
+
`PaperCup.post(url, params: {name: 'pepe'}, headers: { "Content-Type" => "application/json"})`
|
40
64
|
|
41
65
|
|
42
66
|
Response
|
@@ -0,0 +1,52 @@
|
|
1
|
+
module PaperCup
|
2
|
+
class Client
|
3
|
+
attr_accessor :options
|
4
|
+
|
5
|
+
def initialize(opts)
|
6
|
+
@options = opts
|
7
|
+
end
|
8
|
+
|
9
|
+
def url
|
10
|
+
options.fetch(:url)
|
11
|
+
end
|
12
|
+
|
13
|
+
def headers
|
14
|
+
options[:headers] || {}
|
15
|
+
end
|
16
|
+
|
17
|
+
def params
|
18
|
+
options[:params] || {}
|
19
|
+
end
|
20
|
+
|
21
|
+
PaperCup::METHODS.each do |method|
|
22
|
+
define_method(method) do |path, opts = {}|
|
23
|
+
opts = merged_opts_for_request(
|
24
|
+
method: method,
|
25
|
+
path: path,
|
26
|
+
**opts
|
27
|
+
)
|
28
|
+
Request.new(opts).exec
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
def endpoint(path, opts = {})
|
33
|
+
opts = merged_opts_for_request opts.merge(path: path)
|
34
|
+
opts.reject! do |k,v|
|
35
|
+
k == :method
|
36
|
+
end
|
37
|
+
|
38
|
+
return self.class.new(opts)
|
39
|
+
end
|
40
|
+
|
41
|
+
private
|
42
|
+
|
43
|
+
def merged_opts_for_request(opts)
|
44
|
+
_method = opts[:method] || :get
|
45
|
+
_url = File.join(url, opts.fetch(:path).to_s)
|
46
|
+
_headers = headers.merge(opts[:headers] || {})
|
47
|
+
_params = params.merge(opts[:params] || {})
|
48
|
+
|
49
|
+
{ method: _method, url: _url, headers: _headers, params: _params }
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
data/lib/paper_cup.rb
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
require "paper_cup/request"
|
2
|
-
module PaperCup
|
3
2
|
|
3
|
+
module PaperCup
|
4
4
|
INSERT_METHODS = %w(post put)
|
5
5
|
QUERY_METHODS = %w(get head)
|
6
6
|
METHODS = INSERT_METHODS + QUERY_METHODS
|
@@ -11,3 +11,5 @@ module PaperCup
|
|
11
11
|
end
|
12
12
|
end
|
13
13
|
end
|
14
|
+
|
15
|
+
require "paper_cup/client"
|
data/paper-cup.gemspec
CHANGED
@@ -0,0 +1,36 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
def client
|
4
|
+
@client ||= PaperCup::Client.new(
|
5
|
+
url: 'http://api.github.com',
|
6
|
+
params: { test_param_1: :pepito, test_param_2: :pepita },
|
7
|
+
headers: { 'X-Test-Header-1': :pepote, 'X-Test-Header-2': :papita }
|
8
|
+
)
|
9
|
+
end
|
10
|
+
|
11
|
+
describe PaperCup::Client do
|
12
|
+
it '#merged_opts_for_request merges options properly' do
|
13
|
+
result = client.send :merged_opts_for_request, {
|
14
|
+
method: :get,
|
15
|
+
path: '/users/casapick',
|
16
|
+
params: { test_param_2: :popote, test_param_3: :papota },
|
17
|
+
headers: { 'X-Test-Header-2': :popote, 'X-Test-Header-3': :papota }
|
18
|
+
}
|
19
|
+
|
20
|
+
assert_equal result.fetch(:method), :get
|
21
|
+
assert_equal result.fetch(:url), 'http://api.github.com/users/casapick'
|
22
|
+
assert_equal result.fetch(:params), test_param_1: :pepito, test_param_2: :popote, test_param_3: :papota
|
23
|
+
assert_equal result.fetch(:headers), 'X-Test-Header-1': :pepote, 'X-Test-Header-2': :popote, 'X-Test-Header-3': :papota
|
24
|
+
end
|
25
|
+
|
26
|
+
it '#endpoint returns a new client with merged url, params and headers' do
|
27
|
+
users = client.endpoint(:users,
|
28
|
+
params: { test_param_2: :popote, test_param_3: :papota },
|
29
|
+
headers: { 'X-Test-Header-2': :popote, 'X-Test-Header-3': :papota }
|
30
|
+
)
|
31
|
+
|
32
|
+
assert_equal users.url, 'http://api.github.com/users'
|
33
|
+
assert_equal users.params, test_param_1: :pepito, test_param_2: :popote, test_param_3: :papota
|
34
|
+
assert_equal users.headers, 'X-Test-Header-1': :pepote, 'X-Test-Header-2': :popote, 'X-Test-Header-3': :papota
|
35
|
+
end
|
36
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: paper-cup
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Mariano Matayoshi
|
@@ -80,11 +80,13 @@ files:
|
|
80
80
|
- README.md
|
81
81
|
- Rakefile
|
82
82
|
- lib/paper_cup.rb
|
83
|
+
- lib/paper_cup/client.rb
|
83
84
|
- lib/paper_cup/request.rb
|
84
85
|
- lib/paper_cup/response.rb
|
85
86
|
- lib/paper_cup/utils.rb
|
86
87
|
- paper-cup.gemspec
|
87
88
|
- test/paper_cup_test.rb
|
89
|
+
- test/paper_cup_test/client_test.rb
|
88
90
|
- test/paper_cup_test/request_test.rb
|
89
91
|
- test/paper_cup_test/response_test.rb
|
90
92
|
- test/test_helper.rb
|