client 0.0.8 → 0.0.9

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: a59fea54a5a4afd0fa732219a662e8f550766740
4
- data.tar.gz: 798ff068cf67053d0f0238ae13777e08d453c261
3
+ metadata.gz: 3da2c2f22dcab43cbb37c688cc58ec920ecd4e77
4
+ data.tar.gz: 73ecaf8835854991dd90eed56c4a6c17dac8abd7
5
5
  SHA512:
6
- metadata.gz: 9e058240202a995825a808662bf6b9f00b511f59912fc7091b5c15e45bb3b7c7b08ad0ea60a3828519e0f2c8b8612bc5805fffcc51104be72c7b98221d540b60
7
- data.tar.gz: aeec1d1a72f9b52914cbe836f56eab9cc6b156f634294d090dc6fff4a0a080e446a79e0cf10048755da51a5c9958f50ba931997c7538af6238d3780600416d39
6
+ metadata.gz: 8d476276275ae02148f1a04349f41c46169aa9033174a0ce422483a7d976d9086e28eb9c9789db4722e4d322e9b20ceb1dc0371d412ad5961447a9803eb73bae
7
+ data.tar.gz: 603555b0daa53872f40f8b2009c5e33d40e9b0ce60a48d98e830d122d51d0589858312862d6165e5c913aea53f4621de73702004e8de0604e5b13c21f826ee02
data/README.md CHANGED
@@ -12,7 +12,7 @@ Add this line to your application's Gemfile:
12
12
  and run `bundle`
13
13
 
14
14
  ## Usage
15
- Client tries to generate res clients at soon as you require the gem.
15
+ Client tries to generate rest clients as soon as you require the gem.
16
16
  It will try to find `client.yml` in the main folder of your project:
17
17
 
18
18
  /my_project/client.yml
@@ -26,21 +26,29 @@ The yaml should look something like this:
26
26
  base_uri: 'http://www.twitter.com'
27
27
  ```
28
28
 
29
- This will generate a rest client for you to perform post and gets and return NET/http responses:
29
+ This will generate a rest client for you to perform post and gets and return NET/HTTP responses:
30
30
 
31
31
  ```ruby
32
32
  require 'client'
33
33
  Client::Google.get 'search', query: {q: 'bonzofenix gem client'}
34
34
  #This should perform a GET to http://www.google.com/search?q=bonzofenix+gem+client
35
-
35
+
36
36
  #Some rest magic too:
37
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
38
+ Client::Twitter.list_tweets(query: {user_id: 2}) #also try find_
39
+ #This should perform a GET to http://www.twitter.com/tweets?user_id=2
40
40
 
41
- Client::Twitter.destroy_tweets(id: 3) #also try remove_ delete_
41
+ Client::Twitter.list_tweets(1, query: {token: 'asd123'}) #also try find_1
42
+ #This should perform a GET to http://www.twitter.com/tweets/1?token=asd123
43
+
44
+ Client::Twitter.destroy_tweets(3) #also try remove_ delete_
42
45
  #This should perform a DELETE to http://www.twitter.com/tweets/3
43
46
 
47
+ Client::Twitter.create_tweet(body:{text: 'a tweet'}) #also try post_
48
+ #This should perform a POST to http://www.twitter.com/tweets with body: text='a tweet'
49
+
50
+ Client::Twitter.create_tweet(body: {text: 'a tweet'}, content_type: :json) #also try post_
51
+ #This should perform a POST to http://www.twitter.com/tweets with body: '{"text": "a tweet"}' and header CONTENT-TYPE application/json
44
52
  ```
45
53
 
46
54
  You can also load specific yml files:
data/client.gemspec CHANGED
@@ -10,7 +10,7 @@ Gem::Specification.new do |spec|
10
10
  spec.email = ["bonzofenix@gmail.com"]
11
11
  spec.description = %q{Client gives you the possibility to hit rest endpoints in an elegant way}
12
12
  spec.summary = %q{solves rest comunications}
13
- spec.homepage = ""
13
+ spec.homepage = "https://github.com/bonzofenix/client"
14
14
  spec.license = "MIT"
15
15
 
16
16
  spec.files = `git ls-files`.split($/)
@@ -1,3 +1,3 @@
1
1
  class Client
2
- VERSION = '0.0.8'
2
+ VERSION = '0.0.9'
3
3
  end
data/lib/client.rb CHANGED
@@ -43,19 +43,49 @@ class Client
43
43
  # params: #{params} got: #{r.inspect} code: #{r.code}"
44
44
 
45
45
  def method_missing(m, *args, &block)
46
- action, path = m.to_s.match(/(^[^_]+(?=_))_(.+)/).captures
47
- params , body = *args[0..1]
48
- warn params
49
- case action
46
+ parse_method(m)
47
+ parse_arguments(args)
48
+ set_content_type
49
+ perform_action
50
+ end
51
+
52
+ private
53
+
54
+ def set_content_type
55
+ if @opts && @opts.delete(:content_type) == :json
56
+ @opts[:headers] = { 'Content-Type' => 'application/json' }
57
+ @opts[:body] = @opts[:body].to_json
58
+ end
59
+ end
60
+
61
+ def perform_action
62
+ case @action
50
63
  when *%w{find list}
51
- self.get("/#{path}", query: params )
64
+ self.get(url, @opts)
52
65
  when *%w{delete remove destroy}
53
- self.delete("/#{path}/#{params}", body: body)
66
+ self.delete(url, @opts)
54
67
  when *%w{post create}
55
- self.post("/#{path}",body: params)
68
+ self.post(url, @opts)
69
+ end
70
+ end
71
+
72
+
73
+ def parse_method(name)
74
+ @action, @path = name.to_s.match(/(^[^_]+(?=_))_(.+)/).captures
75
+ end
76
+
77
+ def parse_arguments(args)
78
+ @id = args.shift if args.first.is_a?(Integer)
79
+ @opts = args.first || {}
80
+ end
81
+
82
+ def url
83
+ "/#{@path}".tap do |u|
84
+ u << "/#{@id}" if @id
56
85
  end
57
86
  end
58
87
  end
88
+
59
89
  end
60
90
 
61
91
  class << self
@@ -1,8 +1,12 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe Client do
4
+ subject(:client) do
5
+ Client::RandomClient
6
+ end
7
+
4
8
  it 'defaults to client.yml if no file is loaded' do
5
- Client::RandomClient.should be
9
+ client.should be
6
10
  end
7
11
 
8
12
  describe 'when there is a config' do
@@ -11,43 +15,62 @@ describe Client do
11
15
  Client.load_clients("#{Dir.pwd}/twitter.yml")
12
16
  end
13
17
 
18
+ subject(:client) do
19
+ Client::Twitter
20
+ end
21
+
14
22
  it 'creates a subclass' do
15
- Client::Twitter.should be
23
+ client.should be
16
24
  end
17
25
 
26
+ it 'returns the endpoint' do
27
+ client.base_uri.should =='http://twitter.com'
28
+ end
18
29
  it 'perform a post' do
19
- Client::Twitter.post_tweet(id: '1', text: 'wtf')
30
+ client.post_tweet(body: {id: '1', text: 'wtf'})
20
31
  WebMock.should have_requested(:post, 'http://twitter.com/tweet')
21
32
  .with { |req| req.body == 'id=1&text=wtf' }
22
33
  end
23
34
 
24
- it 'perform a get with params' do
25
- Client::Twitter.get('/tweet', query: {id: 10})
35
+ describe 'when json content type is given ' do
36
+ it 'parse the post body to json' do
37
+ client.post_tweet(body:{id: '1', text: 'wtf'}, content_type: :json)
38
+ WebMock.should have_requested(:post, 'http://twitter.com/tweet')
39
+ .with { |req| req.body == '{"id":"1","text":"wtf"}' }
40
+ end
41
+ end
26
42
 
27
- WebMock.should have_requested(:get, 'http://twitter.com/tweet?id=10')
43
+ it 'perform a get with params' do
44
+ client.get('/tweet', query: {id: 10})
45
+ WebMock.should have_requested(:get, 'http://twitter.com/tweet?id=10')
28
46
  end
29
47
 
30
48
  it 'perform a get' do
31
- Client::Twitter.get('/tweet')
49
+ client.get('/tweet')
32
50
  WebMock.should have_requested(:get, 'http://twitter.com/tweet')
33
51
  end
34
52
 
35
53
 
36
54
  %w{find list}.each do |action|
37
55
  it "perform a get with params for #{action}" do
38
- Client::Twitter.send("#{action}_tweet", {id: 10})
56
+ client.send("#{action}_tweet", query: {id: 10})
39
57
  WebMock.should have_requested(:get, 'http://twitter.com/tweet?id=10')
40
58
  end
59
+
60
+ it "perform a get with params and id for #{action}" do
61
+ client.send("#{action}_tweet", 1, query: {token: 1234})
62
+ WebMock.should have_requested(:get, 'http://twitter.com/tweet/1?token=1234')
63
+ end
41
64
  end
42
65
 
43
66
  %w{delete remove destroy}.each do |action|
44
67
  it "perform a delete with id for #{action}" do
45
- Client::Twitter.send("#{action}_tweet", 1)
68
+ client.send("#{action}_tweet", 1)
46
69
  WebMock.should have_requested(:delete, 'http://twitter.com/tweet/1')
47
70
  end
48
71
 
49
72
  it "perform a delete with params and id for #{action}" do
50
- Client::Twitter.send("#{action}_tweet", 1, {token: 1234})
73
+ client.send("#{action}_tweet", 1, body: {token: 1234})
51
74
  WebMock.should have_requested(:delete, 'http://twitter.com/tweet/1')
52
75
  .with { |req| req.body == 'token=1234' }
53
76
 
@@ -56,6 +79,13 @@ describe Client do
56
79
 
57
80
  end
58
81
 
82
+ describe 'when working with nested urls' do
83
+ pending 'resolves first level of nested resource' do
84
+ client.groups(1).should be_kind_of(client)
85
+ end
86
+ end
87
+
88
+
59
89
 
60
90
  describe 'when loading config files manually' do
61
91
  it 'warns when it can fine the file'
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.8
4
+ version: 0.0.9
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-06 00:00:00.000000000 Z
11
+ date: 2014-01-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: httparty
@@ -116,7 +116,7 @@ files:
116
116
  - spec/fixtures/client.yml
117
117
  - spec/fixtures/twitter.yml
118
118
  - spec/spec_helper.rb
119
- homepage: ''
119
+ homepage: https://github.com/bonzofenix/client
120
120
  licenses:
121
121
  - MIT
122
122
  metadata: {}
@@ -136,7 +136,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
136
136
  version: '0'
137
137
  requirements: []
138
138
  rubyforge_project:
139
- rubygems_version: 2.1.5
139
+ rubygems_version: 2.0.3
140
140
  signing_key:
141
141
  specification_version: 4
142
142
  summary: solves rest comunications