vidibus-service 0.2.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -1,4 +1,4 @@
1
- # Vidibus::Service [![](http://travis-ci.org/vidibus/vidibus-service.png)](http://travis-ci.org/vidibus/vidibus-service)
1
+ # Vidibus::Service [![Build Status](https://travis-ci.org/vidibus/vidibus-service.png)](https://travis-ci.org/vidibus/vidibus-service)
2
2
 
3
3
  DESCRIBE
4
4
 
data/Rakefile CHANGED
@@ -10,7 +10,7 @@ require "vidibus/service/version"
10
10
 
11
11
  Rake::RDocTask.new do |rdoc|
12
12
  rdoc.rdoc_dir = "rdoc"
13
- rdoc.title = "vidibus-sysinfo #{Vidibus::Service::VERSION}"
13
+ rdoc.title = "vidibus-service #{Vidibus::Service::VERSION}"
14
14
  rdoc.rdoc_files.include("README*")
15
15
  rdoc.rdoc_files.include("lib/**/*.rb")
16
16
  rdoc.options << "--charset=utf-8"
@@ -7,15 +7,21 @@ module Vidibus
7
7
  format :json
8
8
 
9
9
  class ServiceError < Error; end
10
+ class RequestError < Error; end
10
11
 
11
12
  attr_accessor :base_uri, :service, :this
12
13
 
13
14
  # Initializes a new client for given service.
14
15
  def initialize(service)
15
- raise ServiceError.new("Service required") unless service and service.is_a?(::Service)
16
+ unless service && service.is_a?(::Service)
17
+ raise(ServiceError, 'Service required')
18
+ end
19
+ unless service.url
20
+ raise(ServiceError, 'URL of service required')
21
+ end
16
22
  self.service = service
17
23
  self.this = ::Service.this
18
- self.base_uri = service.url or raise(ServiceError.new("URL of service required"))
24
+ self.base_uri = service.url
19
25
  end
20
26
 
21
27
  # Sends a GET request to given path.
@@ -46,13 +52,19 @@ module Vidibus
46
52
  options[options_type] = {:realm => service.realm_uuid, :service => this.uuid}.merge(options[options_type] || {})
47
53
  uri = build_uri(path)
48
54
  Vidibus::Secure.sign_request(verb, uri, options[options_type], secret)
49
- self.class.send(verb, uri, options)
55
+ begin
56
+ self.class.send(verb, uri, options)
57
+ rescue StandardError, Exception => e
58
+ raise(RequestError, e.message, e.backtrace)
59
+ end
50
60
  end
51
61
 
52
62
  # Builds URI from base URI of service and given path.
53
63
  def build_uri(path)
54
64
  path = path.to_s
55
- raise("Expected path, got #{path}") unless path.match(/^\//)
65
+ unless path.match(/^\//)
66
+ path = "/#{path}"
67
+ end
56
68
  base_uri + path
57
69
  end
58
70
 
@@ -1,5 +1,7 @@
1
1
  module Vidibus # :nodoc
2
2
  module Service # :nodoc
3
3
  class Error < StandardError; end
4
+ class ConfigurationError < Error; end
5
+ class ConnectorError < Error; end
4
6
  end
5
7
  end
@@ -4,15 +4,12 @@ module Vidibus
4
4
  extend ActiveSupport::Concern
5
5
  include Vidibus::Secure::Mongoid
6
6
 
7
- class ConfigurationError < Error; end
8
- class ConnectorError < Error; end
9
-
10
7
  included do
11
8
  field :url
12
9
  field :uuid
13
10
  field :function
14
11
  field :realm_uuid
15
- field :this, :type => ::Mongoid::Boolean
12
+ field :this, :type => Boolean, :default => false
16
13
 
17
14
  attr_accessor :nonce
18
15
  attr_encrypted :secret
@@ -22,7 +19,7 @@ module Vidibus
22
19
  validates :realm_uuid, :uuid => {:allow_blank => true}
23
20
  validates :function, :presence => true
24
21
  validates :secret, :presence => true, :unless => :connector?
25
- validates :realm_uuid, :presence => true, :unless => Proc.new {|s| s.connector? or s.this?}
22
+ validates :realm_uuid, :presence => true, :if => :realm_required?
26
23
 
27
24
  validate :dont_allow_secret_for_connector, :if => :connector?
28
25
 
@@ -70,6 +67,10 @@ module Vidibus
70
67
  errors.add(:secret, :secret_not_allowed_for_connector)
71
68
  end
72
69
  end
70
+
71
+ def realm_required?
72
+ !connector? && !this?
73
+ end
73
74
  end
74
75
 
75
76
  module ClassMethods
@@ -89,10 +90,7 @@ module Vidibus
89
90
  # Returns best service by function or UUID within given realm.
90
91
  # If a service can be found in stored, it will be fetched from Connector.
91
92
  def discover(wanted, realm = nil)
92
- unless service = local(wanted, realm)
93
- service = remote(wanted, realm)
94
- end
95
- service
93
+ local(wanted, realm) || remote(wanted, realm)
96
94
  end
97
95
 
98
96
  # Returns stored service by function or UUID within given realm.
@@ -106,15 +104,21 @@ module Vidibus
106
104
  # This method should not be called directly. Use #discover to avoid unneccessary lookups.
107
105
  def remote(wanted, realm)
108
106
  unless realm
109
- raise ArgumentError.new("Please provide a valid realm to discover an appropriate service.")
107
+ fail(ArgumentError, 'Please provide a valid realm to discover an appropriate service.')
110
108
  end
111
- if response = connector.client.get("/services/#{wanted}", :query => {:realm => realm})
112
- secret = response["secret"] || raise(ConnectorError.new("The Connector did not return a secret for #{wanted}. Response was: #{response.parsed_response.inspect}"))
109
+ response = connector.client.
110
+ get("/services/#{wanted}", :query => {:realm => realm})
111
+ if response
112
+ secret = response["secret"]
113
+ unless secret
114
+ fail(ConnectorError, "The Connector did not return a secret for #{wanted}. Response was: #{response.parsed_response.inspect}")
115
+ end
113
116
  secret = Vidibus::Secure.decrypt(secret, this.secret)
114
- attributes = response.only(%w[uuid function url]).merge(:realm_uuid => realm, :secret => secret)
117
+ attributes = response.only(%w[uuid function url]).
118
+ merge(:realm_uuid => realm, :secret => secret)
115
119
  create!(attributes)
116
120
  else
117
- raise "no service found"
121
+ fail('no service found')
118
122
  end
119
123
  end
120
124
  end
@@ -1,5 +1,5 @@
1
1
  module Vidibus
2
2
  module Service
3
- VERSION = '0.2.0'
3
+ VERSION = '0.3.0'
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: vidibus-service
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-08-30 00:00:00.000000000 Z
12
+ date: 2013-10-02 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: mongoid
@@ -286,7 +286,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
286
286
  version: '0'
287
287
  segments:
288
288
  - 0
289
- hash: -240109756340464642
289
+ hash: 2662895653579897609
290
290
  required_rubygems_version: !ruby/object:Gem::Requirement
291
291
  none: false
292
292
  requirements: