client 0.0.4 → 0.0.5
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/.travis.yml +3 -0
- data/README.md +36 -11
- data/Rakefile +5 -0
- data/lib/client/version.rb +1 -1
- data/lib/client.rb +26 -11
- data/spec/client/client_spec.rb +5 -3
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: dd0537dc74943e0bf99d8055ca8952945bd911a8
|
4
|
+
data.tar.gz: ef5e10a90e36fc38bbdc1ce93c8c306a8e3e655d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5350cf7f71f42706edbeea7b21c088ba159586d54221d9996d8af67b692ccdd62891bdc9647d61a46fd1a16912c1259a0d8c0d5c669a0b14acb48bcfede30895
|
7
|
+
data.tar.gz: 159ffde0117c95141339244e4aa36baa7a1b4c29cd165a5706eeed5fcdab41637591071fa655b84a7e5b9fdf0f868b2c747af4f7d14029778716f006a590a2f9
|
data/.travis.yml
ADDED
data/README.md
CHANGED
@@ -1,24 +1,49 @@
|
|
1
1
|
# Client
|
2
|
+
[](https://travis-ci.org/bonzofenix/client)
|
2
3
|
|
3
|
-
|
4
|
+
Client provides you with metaprogram ruby rest clients generated from yml configurations.
|
4
5
|
|
5
6
|
## Installation
|
6
7
|
|
7
8
|
Add this line to your application's Gemfile:
|
8
9
|
|
9
10
|
gem 'client'
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
$ bundle
|
14
|
-
|
15
|
-
Or install it yourself as:
|
16
|
-
|
17
|
-
$ gem install client
|
11
|
+
|
12
|
+
and run `bundle`
|
18
13
|
|
19
14
|
## Usage
|
20
|
-
|
21
|
-
|
15
|
+
Client tries to generate res clients at soon as you require the gem.
|
16
|
+
It will try to find `client.yml` in the main folder of your project:
|
17
|
+
|
18
|
+
/my_project/client.yml
|
19
|
+
|
20
|
+
The yaml should look something like this:
|
21
|
+
|
22
|
+
```yml
|
23
|
+
google:
|
24
|
+
endpoint: 'http://www.google.com'
|
25
|
+
```
|
26
|
+
|
27
|
+
This will generate a rest client for you to perform post and gets and return NET/http responses:
|
28
|
+
|
29
|
+
```ruby
|
30
|
+
require 'client'
|
31
|
+
Client::Google.get 'search', {q: 'bonzofenix gem client'}
|
32
|
+
#This should perform a GET to http://www.google.com/search?q=bonzofenix+gem+client
|
33
|
+
```
|
34
|
+
|
35
|
+
You can also load specific yml files:
|
36
|
+
|
37
|
+
```ruby
|
38
|
+
require 'client'
|
39
|
+
Client.load_clients 'config/twitter.yml', 'config/google.yml'
|
40
|
+
Client::Twitter
|
41
|
+
# => Client::Twitter
|
42
|
+
Client::Google
|
43
|
+
# => Client::Google
|
44
|
+
```
|
45
|
+
|
46
|
+
See the spec folder for more examples.
|
22
47
|
|
23
48
|
## Contributing
|
24
49
|
|
data/Rakefile
CHANGED
data/lib/client/version.rb
CHANGED
data/lib/client.rb
CHANGED
@@ -1,5 +1,6 @@
|
|
1
1
|
require 'client/version'
|
2
2
|
require 'net/http'
|
3
|
+
require 'logger'
|
3
4
|
require 'yaml'
|
4
5
|
|
5
6
|
class String
|
@@ -21,28 +22,42 @@ end
|
|
21
22
|
class Client
|
22
23
|
class Base
|
23
24
|
class << self
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
25
|
+
attr_accessor :endpoint
|
26
|
+
def perform(method, resource, params)
|
27
|
+
uri = URI("#{endpoint}/#{resource}")
|
28
|
+
case method
|
29
|
+
when :get
|
30
|
+
uri.query = URI.encode_www_form(params) if params
|
31
|
+
Net::HTTP.get_response(uri)
|
32
|
+
when :post
|
33
|
+
Net::HTTP.post_form(uri, params)
|
34
|
+
end.tap do |r|
|
35
|
+
logger.info "Client::Base performed: #{method} to #{uri.to_s} \
|
36
|
+
params: #{params} got: #{r.inspect} code: #{r.code}"
|
37
|
+
end
|
30
38
|
end
|
31
39
|
|
32
40
|
def post(resource, params)
|
33
|
-
|
34
|
-
Net::HTTP.post_form(uri, params)
|
41
|
+
perform(:post,resource, params)
|
35
42
|
end
|
36
43
|
|
37
44
|
def get(resource, params = nil)
|
38
|
-
|
39
|
-
|
40
|
-
|
45
|
+
perform(:get ,resource, params)
|
46
|
+
end
|
47
|
+
|
48
|
+
def logger
|
49
|
+
Client.logger
|
41
50
|
end
|
42
51
|
end
|
43
52
|
end
|
44
53
|
|
45
54
|
class << self
|
55
|
+
attr_accessor :logger
|
56
|
+
|
57
|
+
def logger
|
58
|
+
@logger ||= Logger.new(STDOUT).tap{ |l| l.level = Logger::WARN }
|
59
|
+
end
|
60
|
+
|
46
61
|
def clients
|
47
62
|
@clients ||= {}
|
48
63
|
end
|
data/spec/client/client_spec.rb
CHANGED
@@ -11,9 +11,6 @@ describe Client do
|
|
11
11
|
Client.load_clients("#{Dir.pwd}/twitter.yml")
|
12
12
|
end
|
13
13
|
|
14
|
-
describe '#load_clients' do
|
15
|
-
it 'warns when it can fine the file'
|
16
|
-
end
|
17
14
|
|
18
15
|
it 'creates a subclass' do
|
19
16
|
Client::Twitter.should be
|
@@ -35,4 +32,9 @@ describe Client do
|
|
35
32
|
WebMock.should have_requested(:get, 'http://twitter.com/tweet?id=10')
|
36
33
|
end
|
37
34
|
end
|
35
|
+
|
36
|
+
|
37
|
+
describe 'when loading config files manually' do
|
38
|
+
it 'warns when it can fine the file'
|
39
|
+
end
|
38
40
|
end
|
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.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- bonzofenix
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2013-11-
|
11
|
+
date: 2013-11-20 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -75,6 +75,7 @@ extensions: []
|
|
75
75
|
extra_rdoc_files: []
|
76
76
|
files:
|
77
77
|
- .gitignore
|
78
|
+
- .travis.yml
|
78
79
|
- Gemfile
|
79
80
|
- LICENSE.txt
|
80
81
|
- README.md
|