client 0.0.6 → 0.0.7
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 +13 -2
- data/lib/client.rb +12 -49
- data/lib/client/version.rb +1 -1
- data/spec/client/client_spec.rb +8 -2
- data/spec/fixtures/client.yml +1 -1
- data/spec/fixtures/twitter.yml +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: d902e6cc8f1fc09b8bc03c1686c741c23fe82fa8
|
4
|
+
data.tar.gz: a180e712414555650e2ba56afb0fa78073d9a5ef
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: eb1fbd425455a311343c5b472351c2b45cc26b0f28cd4e5b24a9269574a2fdafe5eb05b29b5fc8c6b53075685d93e6af793ebaac8da427bdd2c99ec382871b97
|
7
|
+
data.tar.gz: 092d2c47fb15a752480e7f768fa7e4fd507076d6a36e230f83bed5a40bd7d9b0103edfd40c56ccc80b03cdcaed5cf08f170c0fd13632f4d543132af86fd37d15
|
data/README.md
CHANGED
@@ -21,15 +21,26 @@ The yaml should look something like this:
|
|
21
21
|
|
22
22
|
```yml
|
23
23
|
google:
|
24
|
-
|
24
|
+
base_uri: 'http://www.google.com'
|
25
|
+
twitter:
|
26
|
+
base_uri: 'http://www.twitter.com'
|
25
27
|
```
|
26
28
|
|
27
29
|
This will generate a rest client for you to perform post and gets and return NET/http responses:
|
28
30
|
|
29
31
|
```ruby
|
30
32
|
require 'client'
|
31
|
-
Client::Google.get 'search', {q: 'bonzofenix gem client'}
|
33
|
+
Client::Google.get 'search', query: {q: 'bonzofenix gem client'}
|
32
34
|
#This should perform a GET to http://www.google.com/search?q=bonzofenix+gem+client
|
35
|
+
|
36
|
+
#Some rest magic too:
|
37
|
+
|
38
|
+
Client::Twitter.list_tweets(user_id: 2) #also try find_
|
39
|
+
#This should perform a GET to http://www.twitter.com/tweets?user_id=bonzofenix+gem+client
|
40
|
+
|
41
|
+
Client::Twitter.destroy_tweets(id: 3) #also try remove_ delete_
|
42
|
+
#This should perform a DELETE to http://www.twitter.com/tweets/3
|
43
|
+
|
33
44
|
```
|
34
45
|
|
35
46
|
You can also load specific yml files:
|
data/lib/client.rb
CHANGED
@@ -1,5 +1,6 @@
|
|
1
1
|
require 'client/version'
|
2
2
|
require 'net/http'
|
3
|
+
require 'httparty'
|
3
4
|
require 'logger'
|
4
5
|
require 'yaml'
|
5
6
|
|
@@ -22,59 +23,19 @@ end
|
|
22
23
|
class Client
|
23
24
|
class Base
|
24
25
|
class << self
|
25
|
-
|
26
|
-
|
27
|
-
def perform(method, resource, params = nil)
|
28
|
-
case method
|
29
|
-
when :get
|
30
|
-
api.get(resource)
|
31
|
-
when :post
|
32
|
-
api.post(resource, www_params)
|
33
|
-
when :delete
|
34
|
-
api.delete(resource)
|
35
|
-
end.tap do |r|
|
36
|
-
logger.info "Client::Base performed: #{method} to #{uri.to_s} \
|
37
|
-
params: #{params} got: #{r.inspect} code: #{r.code}"
|
38
|
-
end
|
39
|
-
end
|
40
|
-
|
41
|
-
def post(resource, params)
|
42
|
-
@params = params
|
43
|
-
perform(:post, resource)
|
44
|
-
end
|
45
|
-
|
46
|
-
def get(resource, params = nil)
|
47
|
-
perform(:get ,resource, params)
|
48
|
-
end
|
49
|
-
|
50
|
-
def logger
|
51
|
-
Client.logger
|
52
|
-
end
|
26
|
+
# logger.info "Client::Base performed: #{method} to #{uri.to_s} \
|
27
|
+
# params: #{params} got: #{r.inspect} code: #{r.code}"
|
53
28
|
|
54
29
|
def method_missing(m, *args, &block)
|
30
|
+
warn m
|
55
31
|
action, path = m.to_s.match(/(^[^_]+(?=_))_(.+)/).captures
|
56
|
-
|
32
|
+
params = args.first
|
57
33
|
case action
|
58
34
|
when *%w{find list}
|
59
|
-
|
35
|
+
self.get("/#{path}", query: params )
|
60
36
|
when *%w{delete remove destroy}
|
61
|
-
|
37
|
+
self.delete("/#{path}/#{params}")
|
62
38
|
end
|
63
|
-
|
64
|
-
|
65
|
-
end
|
66
|
-
|
67
|
-
private
|
68
|
-
|
69
|
-
def www_params
|
70
|
-
URI.encode_www_form(@params) if @params
|
71
|
-
end
|
72
|
-
def uri
|
73
|
-
URI(endpoint)
|
74
|
-
end
|
75
|
-
|
76
|
-
def api
|
77
|
-
Net::HTTP.new(uri)
|
78
39
|
end
|
79
40
|
end
|
80
41
|
end
|
@@ -97,19 +58,21 @@ class Client
|
|
97
58
|
warn '''Check that you have an client.env file in your project with
|
98
59
|
the following entry.
|
99
60
|
gitlab:
|
100
|
-
|
61
|
+
base_uri: http://gitlab.com/api/v3/
|
101
62
|
other_server:
|
102
|
-
|
63
|
+
base_uri: other_endpoint.com
|
103
64
|
'''
|
104
65
|
{}
|
105
66
|
end
|
106
67
|
generate_clients
|
107
68
|
end
|
108
69
|
|
70
|
+
private
|
109
71
|
def generate_clients
|
110
72
|
clients.each do |name, info|
|
111
73
|
Class.new(Base) do
|
112
|
-
|
74
|
+
include HTTParty
|
75
|
+
base_uri info.fetch('base_uri')
|
113
76
|
end.tap do |client_class|
|
114
77
|
const_set(name.camelize, client_class)
|
115
78
|
end
|
data/lib/client/version.rb
CHANGED
data/spec/client/client_spec.rb
CHANGED
@@ -17,11 +17,17 @@ describe Client do
|
|
17
17
|
end
|
18
18
|
|
19
19
|
it 'perform a post' do
|
20
|
-
Client::Twitter.post('/tweet', {id: '1', text: 'wtf'})
|
20
|
+
Client::Twitter.post('/tweet',body: {id: '1', text: 'wtf'})
|
21
21
|
WebMock.should have_requested(:post, 'http://twitter.com/tweet')
|
22
22
|
.with { |req| req.body == 'id=1&text=wtf' }
|
23
23
|
end
|
24
24
|
|
25
|
+
it 'perform a get with params' do
|
26
|
+
Client::Twitter.get('/tweet', query: {id: 10})
|
27
|
+
|
28
|
+
WebMock.should have_requested(:get, 'http://twitter.com/tweet?id=10')
|
29
|
+
end
|
30
|
+
|
25
31
|
it 'perform a get' do
|
26
32
|
Client::Twitter.get('/tweet')
|
27
33
|
WebMock.should have_requested(:get, 'http://twitter.com/tweet')
|
@@ -35,7 +41,7 @@ describe Client do
|
|
35
41
|
end
|
36
42
|
|
37
43
|
%w{delete remove destroy}.each do |action|
|
38
|
-
it "perform a delete with
|
44
|
+
it "perform a delete with params for #{action}" do
|
39
45
|
Client::Twitter.send("#{action}_tweet", 1)
|
40
46
|
WebMock.should have_requested(:delete, 'http://twitter.com/tweet/1')
|
41
47
|
end
|
data/spec/fixtures/client.yml
CHANGED
@@ -1,2 +1,2 @@
|
|
1
1
|
random_client:
|
2
|
-
|
2
|
+
base_uri: 'www.google.com'
|
data/spec/fixtures/twitter.yml
CHANGED
@@ -1,2 +1,2 @@
|
|
1
1
|
twitter:
|
2
|
-
|
2
|
+
base_uri: http://twitter.com
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: client
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.7
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- bonzofenix
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2013-12-
|
11
|
+
date: 2013-12-06 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: httparty
|