client 0.0.9 → 0.1.0

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: 3da2c2f22dcab43cbb37c688cc58ec920ecd4e77
4
- data.tar.gz: 73ecaf8835854991dd90eed56c4a6c17dac8abd7
3
+ metadata.gz: f5aed7ac9da5ce353a6e0a2eec53302163a2bfce
4
+ data.tar.gz: c1f8bf69fbbf02d32e192f444756eb6af4f4ea58
5
5
  SHA512:
6
- metadata.gz: 8d476276275ae02148f1a04349f41c46169aa9033174a0ce422483a7d976d9086e28eb9c9789db4722e4d322e9b20ceb1dc0371d412ad5961447a9803eb73bae
7
- data.tar.gz: 603555b0daa53872f40f8b2009c5e33d40e9b0ce60a48d98e830d122d51d0589858312862d6165e5c913aea53f4621de73702004e8de0604e5b13c21f826ee02
6
+ metadata.gz: e9491f03d4985847bea50f0508b2161d0c501f9b75535cb734653acac30ae2f68f93580069254f36356ad9c406d6808c2af7ab6c9df3d83332547a8cd252b49f
7
+ data.tar.gz: cf95559aecf33cbe60f31f4dc93c0d934a29567d2fc9dad5062564e2d32d70f78c840e666f712e59e4e15dd0890e6488eabe5f6d3a6f1eed74fe902cd5b0af9b
@@ -39,18 +39,30 @@ end
39
39
  class Client
40
40
  class Base
41
41
  class << self
42
- # logger.info "Client::Base performed: #{method} to #{uri.to_s} \
43
- # params: #{params} got: #{r.inspect} code: #{r.code}"
44
-
45
42
  def method_missing(m, *args, &block)
46
- parse_method(m)
47
- parse_arguments(args)
43
+ MetaRequest.new(self , m, args).run
44
+ end
45
+ end
46
+
47
+
48
+
49
+ class MetaRequest
50
+ include HTTParty
51
+
52
+ def initialize(base_klass, method, args)
53
+ @base_klass = base_klass
54
+ @method = method
55
+ @args = args
56
+ end
57
+
58
+ def run
59
+ parse_method
60
+ parse_arguments
48
61
  set_content_type
49
62
  perform_action
50
63
  end
51
64
 
52
65
  private
53
-
54
66
  def set_content_type
55
67
  if @opts && @opts.delete(:content_type) == :json
56
68
  @opts[:headers] = { 'Content-Type' => 'application/json' }
@@ -61,22 +73,21 @@ class Client
61
73
  def perform_action
62
74
  case @action
63
75
  when *%w{find list}
64
- self.get(url, @opts)
76
+ @base_klass.get(url, @opts)
65
77
  when *%w{delete remove destroy}
66
- self.delete(url, @opts)
78
+ @base_klass.delete(url, @opts)
67
79
  when *%w{post create}
68
- self.post(url, @opts)
80
+ @base_klass.post(url, @opts)
69
81
  end
70
82
  end
71
83
 
72
-
73
- def parse_method(name)
74
- @action, @path = name.to_s.match(/(^[^_]+(?=_))_(.+)/).captures
84
+ def parse_method
85
+ @action, @path = @method.to_s.match(/(^[^_]+(?=_))_(.+)/).captures
75
86
  end
76
87
 
77
- def parse_arguments(args)
78
- @id = args.shift if args.first.is_a?(Integer)
79
- @opts = args.first || {}
88
+ def parse_arguments
89
+ @id = @args.shift if @args.first.is_a?(Integer)
90
+ @opts = @args.first || {}
80
91
  end
81
92
 
82
93
  def url
@@ -89,7 +100,7 @@ class Client
89
100
  end
90
101
 
91
102
  class << self
92
- attr_accessor :logger
103
+ attr_accessor :logger, :loaded_config_files
93
104
 
94
105
  def logger
95
106
  @logger ||= Logger.new(STDOUT).tap{ |l| l.level = Logger::WARN }
@@ -99,28 +110,40 @@ class Client
99
110
  @clients ||= {}
100
111
  end
101
112
 
102
- def load_clients(path = "#{Dir.pwd}/client.yml")
113
+ def loaded_config_files
114
+ @loaded_config_files ||= []
115
+ end
116
+ def load_clients(path = default_config_path)
103
117
  begin
104
118
  clients.merge! YAML.load_file(path)
119
+ loaded_config_files << path
105
120
  rescue
106
- warn '''Check that you have an client.env file in your project with
107
- the following entry.
108
- gitlab:
109
- base_uri: http://gitlab.com/api/v3/
110
- other_server:
111
- base_uri: other_endpoint.com
112
- '''
121
+ warn """Check that you have an file in: \n\t
122
+ #{path}
123
+
124
+ that respects the following format: \n\t
125
+ example:
126
+ base_uri: http://example.com/api/v3/
127
+ """
113
128
  {}
114
129
  end
115
130
  generate_clients
116
131
  end
117
132
 
118
133
  private
134
+ def default_config_path
135
+ if ENV['RACK_ENV']
136
+ "#{Dir.pwd}/config/client_#{ENV['RACK_ENV']}.yml"
137
+ else
138
+ "#{Dir.pwd}/client.yml"
139
+ end
140
+ end
141
+
119
142
  def generate_clients
120
143
  clients.each do |name, info|
121
144
  Class.new(Base) do
122
145
  include HTTParty
123
- base_uri info.fetch('base_uri')
146
+ self.base_uri info.fetch('base_uri')
124
147
  end.tap do |client_class|
125
148
  const_set(name.camelize, client_class)
126
149
  end
@@ -1,3 +1,3 @@
1
1
  class Client
2
- VERSION = '0.0.9'
2
+ VERSION = '0.1.0'
3
3
  end
@@ -4,11 +4,20 @@ describe Client do
4
4
  subject(:client) do
5
5
  Client::RandomClient
6
6
  end
7
+ before{ stub_const('ENV', {'RACK_ENV' => nil}) }
7
8
 
8
9
  it 'defaults to client.yml if no file is loaded' do
9
10
  client.should be
10
11
  end
11
12
 
13
+ describe 'when RACK_ENV is presence' do
14
+ it 'tries to load config/client_production.yml' do
15
+ stub_const('ENV', {'RACK_ENV' => 'production'})
16
+ Client.load_clients
17
+ Client.loaded_config_files[1].should match(/config\/client_production.yml/)
18
+ end
19
+ end
20
+
12
21
  describe 'when there is a config' do
13
22
  before do
14
23
  stub_request(:any, /.*twitter.*/)
@@ -69,6 +78,11 @@ describe Client do
69
78
  WebMock.should have_requested(:delete, 'http://twitter.com/tweet/1')
70
79
  end
71
80
 
81
+ pending "accepts id as string as long as they can be converted to number" do
82
+ client.send("#{action}_tweet", '1')
83
+ WebMock.should have_requested(:delete, 'http://twitter.com/tweet/1')
84
+ end
85
+
72
86
  it "perform a delete with params and id for #{action}" do
73
87
  client.send("#{action}_tweet", 1, body: {token: 1234})
74
88
  WebMock.should have_requested(:delete, 'http://twitter.com/tweet/1')
@@ -79,12 +93,6 @@ describe Client do
79
93
 
80
94
  end
81
95
 
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
96
 
89
97
 
90
98
  describe 'when loading config files manually' do
@@ -0,0 +1,2 @@
1
+ another_client:
2
+ base_uri: 'www.google.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.9
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - bonzofenix
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-01-22 00:00:00.000000000 Z
11
+ date: 2014-01-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: httparty
@@ -114,6 +114,7 @@ files:
114
114
  - lib/client/version.rb
115
115
  - spec/client/client_spec.rb
116
116
  - spec/fixtures/client.yml
117
+ - spec/fixtures/config/client_production.yml
117
118
  - spec/fixtures/twitter.yml
118
119
  - spec/spec_helper.rb
119
120
  homepage: https://github.com/bonzofenix/client
@@ -143,5 +144,6 @@ summary: solves rest comunications
143
144
  test_files:
144
145
  - spec/client/client_spec.rb
145
146
  - spec/fixtures/client.yml
147
+ - spec/fixtures/config/client_production.yml
146
148
  - spec/fixtures/twitter.yml
147
149
  - spec/spec_helper.rb