centro-client 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore CHANGED
@@ -1,5 +1,6 @@
1
1
  *.gem
2
2
  *.rbc
3
+ *.swp
3
4
  .bundle
4
5
  .config
5
6
  coverage
@@ -0,0 +1,42 @@
1
+ module Centro
2
+ module API
3
+ module Base
4
+
5
+ def mms(method, *args)
6
+ mms_connection.send(method, *args)
7
+ rescue Exception => e
8
+ raise adapt_error(e)
9
+ end
10
+
11
+ def finance(method, *args)
12
+ finance_connection.send(method, *args)
13
+ rescue Exception => e
14
+ raise adapt_error(e)
15
+ end
16
+
17
+ def auth(method, *args)
18
+ auth_connection.send(method, *args)
19
+ rescue Exception => e
20
+ raise adapt_error(e)
21
+ end
22
+
23
+ private
24
+
25
+ def adapt_error(e)
26
+ case e
27
+ when Faraday::Error::ConnectionFailed
28
+ Centro::Error::ConnectionFailed.new(e, e.response)
29
+ when Faraday::Error::ResourceNotFound
30
+ Centro::Error::ResourceNotFound.new(e, e.response)
31
+ when Faraday::Error::ParsingError
32
+ Centro::Error::ParsingError.new(e, e.response)
33
+ when Faraday::Error::TimeoutError
34
+ Centro::Error::TimeoutError.new(e, e.response)
35
+ else
36
+ Centro::Error::ClientError.new(e)
37
+ end
38
+ end
39
+
40
+ end
41
+ end
42
+ end
@@ -1,5 +1,5 @@
1
1
  module Centro
2
2
  class Client
3
- VERSION = "0.0.2"
3
+ VERSION = "0.0.3"
4
4
  end
5
5
  end
data/lib/centro/client.rb CHANGED
@@ -2,6 +2,9 @@ require 'omniauth'
2
2
  require 'omniauth-oauth2'
3
3
  require 'faraday_middleware'
4
4
 
5
+ require 'centro/errors'
6
+ require 'centro/api/base'
7
+
5
8
  module Centro
6
9
  class Client
7
10
  class << self
@@ -13,6 +16,10 @@ module Centro
13
16
  return @auth_host if defined?(@auth_host)
14
17
  @auth_host = 'localhost:3001'
15
18
  end
19
+ def finance_host
20
+ return @finance_host if defined?(@finance_host)
21
+ @finance_host = 'localhost:4000'
22
+ end
16
23
  def mms_host
17
24
  return @mms_host if defined?(@mms_host)
18
25
  @mms_host = 'localhost:3000'
@@ -24,6 +31,9 @@ module Centro
24
31
  def auth_url
25
32
  url_from_host(auth_host)
26
33
  end
34
+ def finance_url
35
+ url_from_host(finance_host)
36
+ end
27
37
  def mms_url
28
38
  url_from_host(mms_host)
29
39
  end
@@ -33,7 +43,7 @@ module Centro
33
43
  end
34
44
  end
35
45
 
36
- attr_accessor :client_id, :client_secret, :access_token
46
+ attr_accessor :client_id, :client_secret, :access_token, :access_token_expires_at
37
47
 
38
48
  def initialize(opts={})
39
49
  @client_id = opts.delete(:client_id) if opts[:client_id]
@@ -41,6 +51,10 @@ module Centro
41
51
  @access_token = opts.delete(:access_token) if opts[:access_token]
42
52
  end
43
53
 
54
+ def access_token_expired?
55
+ access_token_expires_at.nil? || access_token_expires_at < Time.now
56
+ end
57
+
44
58
  def retrieve_access_token(username, password)
45
59
  raise 'client_id and client_secret required' unless @client_id && @client_secret
46
60
  connection = Faraday.new self.class.auth_url do |conn|
@@ -52,6 +66,7 @@ module Centro
52
66
  :client_secret => @client_secret,
53
67
  :username => username,
54
68
  :password => password)
69
+ self.access_token_expires_at = Time.now + response.body.expires_in
55
70
  self.access_token = response.body.access_token
56
71
  end
57
72
 
@@ -119,6 +134,15 @@ module Centro
119
134
  end
120
135
  end
121
136
  end
137
+ def finance_connection
138
+ raise 'access_token required' unless @access_token
139
+ @finance_connection ||= begin
140
+ Faraday.new self.class.finance_url do |conn|
141
+ conn.request :oauth2, @access_token
142
+ set_default_connection_options(conn)
143
+ end
144
+ end
145
+ end
122
146
  def mms_connection
123
147
  raise 'access_token required' unless @access_token
124
148
  @mms_connection ||= begin
@@ -0,0 +1,40 @@
1
+ module Centro
2
+ module Error
3
+
4
+ class Base < StandardError; end
5
+
6
+ class ClientError < Base
7
+ attr_reader :response
8
+
9
+ def initialize(ex, response = nil)
10
+ @wrapped_exception = nil
11
+ @response = response
12
+
13
+ if ex.respond_to?(:backtrace)
14
+ super(ex.message)
15
+ @wrapped_exception = ex
16
+ else
17
+ super(ex.to_s)
18
+ end
19
+ end
20
+
21
+ def backtrace
22
+ if @wrapped_exception
23
+ @wrapped_exception.backtrace
24
+ else
25
+ super
26
+ end
27
+ end
28
+
29
+ def inspect
30
+ %(#<#{self.class}>)
31
+ end
32
+ end
33
+
34
+ class ConnectionFailed < ClientError; end
35
+ class ResourceNotFound < ClientError; end
36
+ class ParsingError < ClientError; end
37
+ class TimeoutError < ClientError; end
38
+ end
39
+
40
+ end
metadata CHANGED
@@ -1,180 +1,184 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: centro-client
3
- version: !ruby/object:Gem::Version
4
- version: 0.0.2
3
+ version: !ruby/object:Gem::Version
4
+ hash: 25
5
5
  prerelease:
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 3
10
+ version: 0.0.3
6
11
  platform: ruby
7
- authors:
12
+ authors:
8
13
  - Tim Galeckas
9
14
  autorequire:
10
15
  bindir: bin
11
16
  cert_chain: []
12
- date: 2012-11-06 00:00:00.000000000 Z
13
- dependencies:
14
- - !ruby/object:Gem::Dependency
17
+
18
+ date: 2012-11-27 00:00:00 Z
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
15
21
  name: rspec
16
- requirement: !ruby/object:Gem::Requirement
17
- none: false
18
- requirements:
19
- - - ! '>='
20
- - !ruby/object:Gem::Version
21
- version: '0'
22
- type: :development
23
22
  prerelease: false
24
- version_requirements: !ruby/object:Gem::Requirement
25
- none: false
26
- requirements:
27
- - - ! '>='
28
- - !ruby/object:Gem::Version
29
- version: '0'
30
- - !ruby/object:Gem::Dependency
31
- name: webmock
32
- requirement: !ruby/object:Gem::Requirement
33
- none: false
34
- requirements:
35
- - - ! '>='
36
- - !ruby/object:Gem::Version
37
- version: '0'
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ none: false
25
+ requirements:
26
+ - - ">="
27
+ - !ruby/object:Gem::Version
28
+ hash: 3
29
+ segments:
30
+ - 0
31
+ version: "0"
38
32
  type: :development
33
+ version_requirements: *id001
34
+ - !ruby/object:Gem::Dependency
35
+ name: webmock
39
36
  prerelease: false
40
- version_requirements: !ruby/object:Gem::Requirement
41
- none: false
42
- requirements:
43
- - - ! '>='
44
- - !ruby/object:Gem::Version
45
- version: '0'
46
- - !ruby/object:Gem::Dependency
47
- name: guard-rspec
48
- requirement: !ruby/object:Gem::Requirement
49
- none: false
50
- requirements:
51
- - - ! '>='
52
- - !ruby/object:Gem::Version
53
- version: '0'
37
+ requirement: &id002 !ruby/object:Gem::Requirement
38
+ none: false
39
+ requirements:
40
+ - - ">="
41
+ - !ruby/object:Gem::Version
42
+ hash: 3
43
+ segments:
44
+ - 0
45
+ version: "0"
54
46
  type: :development
47
+ version_requirements: *id002
48
+ - !ruby/object:Gem::Dependency
49
+ name: guard-rspec
55
50
  prerelease: false
56
- version_requirements: !ruby/object:Gem::Requirement
57
- none: false
58
- requirements:
59
- - - ! '>='
60
- - !ruby/object:Gem::Version
61
- version: '0'
62
- - !ruby/object:Gem::Dependency
51
+ requirement: &id003 !ruby/object:Gem::Requirement
52
+ none: false
53
+ requirements:
54
+ - - ">="
55
+ - !ruby/object:Gem::Version
56
+ hash: 3
57
+ segments:
58
+ - 0
59
+ version: "0"
60
+ type: :development
61
+ version_requirements: *id003
62
+ - !ruby/object:Gem::Dependency
63
63
  name: omniauth
64
- requirement: !ruby/object:Gem::Requirement
65
- none: false
66
- requirements:
67
- - - ! '>='
68
- - !ruby/object:Gem::Version
69
- version: '0'
70
- type: :runtime
71
64
  prerelease: false
72
- version_requirements: !ruby/object:Gem::Requirement
73
- none: false
74
- requirements:
75
- - - ! '>='
76
- - !ruby/object:Gem::Version
77
- version: '0'
78
- - !ruby/object:Gem::Dependency
79
- name: omniauth-oauth2
80
- requirement: !ruby/object:Gem::Requirement
81
- none: false
82
- requirements:
83
- - - ! '>='
84
- - !ruby/object:Gem::Version
85
- version: '0'
65
+ requirement: &id004 !ruby/object:Gem::Requirement
66
+ none: false
67
+ requirements:
68
+ - - ">="
69
+ - !ruby/object:Gem::Version
70
+ hash: 3
71
+ segments:
72
+ - 0
73
+ version: "0"
86
74
  type: :runtime
75
+ version_requirements: *id004
76
+ - !ruby/object:Gem::Dependency
77
+ name: omniauth-oauth2
87
78
  prerelease: false
88
- version_requirements: !ruby/object:Gem::Requirement
89
- none: false
90
- requirements:
91
- - - ! '>='
92
- - !ruby/object:Gem::Version
93
- version: '0'
94
- - !ruby/object:Gem::Dependency
95
- name: faraday
96
- requirement: !ruby/object:Gem::Requirement
97
- none: false
98
- requirements:
99
- - - ! '>='
100
- - !ruby/object:Gem::Version
101
- version: '0'
79
+ requirement: &id005 !ruby/object:Gem::Requirement
80
+ none: false
81
+ requirements:
82
+ - - ">="
83
+ - !ruby/object:Gem::Version
84
+ hash: 3
85
+ segments:
86
+ - 0
87
+ version: "0"
102
88
  type: :runtime
89
+ version_requirements: *id005
90
+ - !ruby/object:Gem::Dependency
91
+ name: faraday
103
92
  prerelease: false
104
- version_requirements: !ruby/object:Gem::Requirement
105
- none: false
106
- requirements:
107
- - - ! '>='
108
- - !ruby/object:Gem::Version
109
- version: '0'
110
- - !ruby/object:Gem::Dependency
111
- name: faraday_middleware
112
- requirement: !ruby/object:Gem::Requirement
113
- none: false
114
- requirements:
115
- - - ! '>='
116
- - !ruby/object:Gem::Version
117
- version: '0'
93
+ requirement: &id006 !ruby/object:Gem::Requirement
94
+ none: false
95
+ requirements:
96
+ - - ">="
97
+ - !ruby/object:Gem::Version
98
+ hash: 3
99
+ segments:
100
+ - 0
101
+ version: "0"
118
102
  type: :runtime
103
+ version_requirements: *id006
104
+ - !ruby/object:Gem::Dependency
105
+ name: faraday_middleware
119
106
  prerelease: false
120
- version_requirements: !ruby/object:Gem::Requirement
121
- none: false
122
- requirements:
123
- - - ! '>='
124
- - !ruby/object:Gem::Version
125
- version: '0'
126
- - !ruby/object:Gem::Dependency
127
- name: multi_xml
128
- requirement: !ruby/object:Gem::Requirement
129
- none: false
130
- requirements:
131
- - - ! '>='
132
- - !ruby/object:Gem::Version
133
- version: '0'
107
+ requirement: &id007 !ruby/object:Gem::Requirement
108
+ none: false
109
+ requirements:
110
+ - - ">="
111
+ - !ruby/object:Gem::Version
112
+ hash: 3
113
+ segments:
114
+ - 0
115
+ version: "0"
134
116
  type: :runtime
117
+ version_requirements: *id007
118
+ - !ruby/object:Gem::Dependency
119
+ name: multi_xml
135
120
  prerelease: false
136
- version_requirements: !ruby/object:Gem::Requirement
137
- none: false
138
- requirements:
139
- - - ! '>='
140
- - !ruby/object:Gem::Version
141
- version: '0'
121
+ requirement: &id008 !ruby/object:Gem::Requirement
122
+ none: false
123
+ requirements:
124
+ - - ">="
125
+ - !ruby/object:Gem::Version
126
+ hash: 3
127
+ segments:
128
+ - 0
129
+ version: "0"
130
+ type: :runtime
131
+ version_requirements: *id008
142
132
  description: Ruby Client for the Centro api
143
- email:
133
+ email:
144
134
  - tim@galeckas.com
145
135
  executables: []
136
+
146
137
  extensions: []
138
+
147
139
  extra_rdoc_files: []
148
- files:
140
+
141
+ files:
149
142
  - .gitignore
150
143
  - README.md
151
144
  - centro-client.gemspec
152
145
  - lib/centro-client.rb
146
+ - lib/centro/api/base.rb
153
147
  - lib/centro/client.rb
154
148
  - lib/centro/client/version.rb
149
+ - lib/centro/errors.rb
155
150
  homepage: http://github.com/centro/centro.rb
156
151
  licenses: []
152
+
157
153
  post_install_message:
158
154
  rdoc_options: []
159
- require_paths:
155
+
156
+ require_paths:
160
157
  - lib
161
- required_ruby_version: !ruby/object:Gem::Requirement
158
+ required_ruby_version: !ruby/object:Gem::Requirement
162
159
  none: false
163
- requirements:
164
- - - ! '>='
165
- - !ruby/object:Gem::Version
166
- version: '0'
167
- required_rubygems_version: !ruby/object:Gem::Requirement
160
+ requirements:
161
+ - - ">="
162
+ - !ruby/object:Gem::Version
163
+ hash: 3
164
+ segments:
165
+ - 0
166
+ version: "0"
167
+ required_rubygems_version: !ruby/object:Gem::Requirement
168
168
  none: false
169
- requirements:
170
- - - ! '>='
171
- - !ruby/object:Gem::Version
172
- version: '0'
169
+ requirements:
170
+ - - ">="
171
+ - !ruby/object:Gem::Version
172
+ hash: 3
173
+ segments:
174
+ - 0
175
+ version: "0"
173
176
  requirements: []
177
+
174
178
  rubyforge_project:
175
- rubygems_version: 1.8.23
179
+ rubygems_version: 1.8.15
176
180
  signing_key:
177
181
  specification_version: 3
178
182
  summary: Ruby Client for the Centro api
179
183
  test_files: []
180
- has_rdoc:
184
+