paul-datapathy-ssbe-adapter 0.1.0

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.
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
@@ -0,0 +1,5 @@
1
+ *.sw?
2
+ .DS_Store
3
+ coverage
4
+ rdoc
5
+ pkg
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Paul Sadauskas
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,7 @@
1
+ = datapathy-ssbe-adapter
2
+
3
+ Description goes here.
4
+
5
+ == Copyright
6
+
7
+ Copyright (c) 2009 Paul Sadauskas. See LICENSE for details.
@@ -0,0 +1,49 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "datapathy-ssbe-adapter"
8
+ gem.summary = %Q{TODO}
9
+ gem.email = "psadauskas@gmail.com"
10
+ gem.homepage = "http://github.com/paul/datapathy-ssbe-adapter"
11
+ gem.authors = ["Paul Sadauskas"]
12
+ gem.version = "0.1.0"
13
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
14
+ end
15
+
16
+ rescue LoadError
17
+ puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
18
+ end
19
+
20
+ require 'spec/rake/spectask'
21
+ Spec::Rake::SpecTask.new(:spec) do |spec|
22
+ spec.libs << 'lib' << 'spec'
23
+ spec.spec_files = FileList['spec/**/*_spec.rb']
24
+ end
25
+
26
+ Spec::Rake::SpecTask.new(:rcov) do |spec|
27
+ spec.libs << 'lib' << 'spec'
28
+ spec.pattern = 'spec/**/*_spec.rb'
29
+ spec.rcov = true
30
+ end
31
+
32
+
33
+ task :default => :spec
34
+
35
+ require 'rake/rdoctask'
36
+ Rake::RDocTask.new do |rdoc|
37
+ if File.exist?('VERSION.yml')
38
+ config = YAML.load(File.read('VERSION.yml'))
39
+ version = "#{config[:major]}.#{config[:minor]}.#{config[:patch]}"
40
+ else
41
+ version = ""
42
+ end
43
+
44
+ rdoc.rdoc_dir = 'rdoc'
45
+ rdoc.title = "datapathy-ssbe-adapter #{version}"
46
+ rdoc.rdoc_files.include('README*')
47
+ rdoc.rdoc_files.include('lib/**/*.rb')
48
+ end
49
+
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
@@ -0,0 +1,76 @@
1
+ require 'resourceful'
2
+ require 'json'
3
+
4
+ require 'ssbe_authenticator'
5
+
6
+ require 'ssbe_model'
7
+
8
+ require 'service_descriptor'
9
+ require 'resource_descriptor'
10
+
11
+ module Datapathy::Adapters
12
+
13
+ class SsbeAdapter < Datapathy::Adapters::AbstractAdapter
14
+
15
+ attr_reader :http, :backend
16
+
17
+ def initialize(options = {})
18
+ super
19
+
20
+ @backend = options[:backend]
21
+ @user, @password = options[:user], options[:password]
22
+
23
+ @http = Resourceful::HttpAccessor.new
24
+ @http.logger = Resourceful::StdOutLogger.new if @options[:logging]
25
+ @http.cache_manager = Resourceful::InMemoryCacheManager.new
26
+ @http.add_authenticator Resourceful::SsbeAuthenticator.new(@user, @password)
27
+ end
28
+
29
+ def read(query)
30
+ if query.key_lookup?
31
+ response = http.resource(query.key, default_headers).get
32
+ query.filter_records([deserialize(response)])
33
+ else
34
+ response = resource_for(query).get
35
+ records = deserialize(response)['items']
36
+ query.filter_records(records)
37
+ end
38
+ end
39
+
40
+ protected
41
+
42
+ def deserialize(response)
43
+ JSON.parse(response.body.gsub('\/', '/'))
44
+ end
45
+
46
+ def resource_for(query)
47
+ model = query.model
48
+ url = if query.respond_to?(:location) && location = query.location
49
+ location
50
+ elsif model == ServiceDescriptor
51
+ services_uri
52
+ else
53
+ service_desc = ServiceDescriptor[model.service_type.to_s]
54
+ resource_desc = service_desc.resource_for(model.resource_name.to_s)
55
+ resource_desc.href
56
+ end
57
+
58
+ raise "Could not identify a location to look for #{model}" unless url
59
+
60
+ http.resource(url, default_headers)
61
+ end
62
+
63
+ def default_headers
64
+ @default_headers ||= {
65
+ :accept => 'application/vnd.absperf.sskj1+json; q=0.8, application/vnd.absperf.ssmj1+json; q=0.8, application/vnd.absperf.ssej1+json; q=0.8, application/vnd.absperf.sscj1+json; q=0.8'
66
+ }
67
+ end
68
+
69
+ def services_uri
70
+ @services_uri ||= "http://core.#{backend}/service_descriptors"
71
+ end
72
+
73
+ end
74
+
75
+ end
76
+
@@ -0,0 +1,11 @@
1
+
2
+ class ResourceDescriptor < SsbeModel
3
+
4
+ persists :name
5
+
6
+ def self.[](name)
7
+ self.select { |m|
8
+ m.name == name
9
+ }
10
+ end
11
+ end
@@ -0,0 +1,61 @@
1
+ require 'ostruct'
2
+
3
+ class ServiceDescriptor < SsbeModel
4
+
5
+ persists :service_type
6
+
7
+ def self.[](name)
8
+ service_type = IDENTIFIERS.detect { |i| i.name.to_s == name.to_s }.service_type
9
+ self.detect { |m|
10
+ m.service_type == service_type
11
+ }
12
+ end
13
+
14
+ def name
15
+ identifier.name
16
+ end
17
+
18
+ def mime_type
19
+ identifier.mime_type
20
+ end
21
+
22
+ def resources
23
+ ResourceDescriptor.from(href)
24
+ end
25
+
26
+ def resource_for(resource_name)
27
+ resources.detect { |r| r.name == resource_name }
28
+ end
29
+
30
+ protected
31
+
32
+ def identifier
33
+ @identifier ||= IDENTIFIERS.detect { |i| i.service_type == service_type }
34
+ end
35
+
36
+ class ServiceIdentifier < OpenStruct; end
37
+
38
+ IDENTIFIERS = [
39
+ ServiceIdentifier.new(
40
+ :name => :kernel,
41
+ :service_type => "http://systemshepherd.com/services/kernel",
42
+ :mime_type => "application/vnd.absperf.sskj1+json"
43
+ ),
44
+ ServiceIdentifier.new(
45
+ :name => :measurements,
46
+ :service_type => "http://systemshepherd.com/services/measurements",
47
+ :mime_type => "application/vnd.absperf.ssmj1+json"
48
+ ),
49
+ ServiceIdentifier.new(
50
+ :name => :escalations,
51
+ :service_type => "http://systemshepherd.com/services/escalations",
52
+ :mime_type => "application/vnd.absperf.ssej1+json"
53
+ ),
54
+ ServiceIdentifier.new(
55
+ :name => :configurator,
56
+ :service_type => "http://systemshepherd.com/services/configurator",
57
+ :mime_type => "application/vnd.absperf.sscj1+json"
58
+ )
59
+ ].freeze
60
+
61
+ end
@@ -0,0 +1,50 @@
1
+ module Resourceful
2
+
3
+ class SsbeAuthenticator
4
+ require 'httpauth'
5
+ require 'addressable/uri'
6
+
7
+
8
+ attr_reader :username, :password, :realm, :domain, :challenge
9
+
10
+ def initialize(username, password)
11
+ @username, @password = username, password
12
+ @realm = 'SystemShepherd'
13
+ @domain = nil
14
+ end
15
+
16
+ def update_credentials(challenge_response)
17
+ @domain = Addressable::URI.parse(challenge_response.uri).host
18
+ @challenge = HTTPAuth::Digest::Challenge.from_header(challenge_response.header['WWW-Authenticate'].first)
19
+ end
20
+
21
+ def valid_for?(challenge_response)
22
+ return false unless challenge_header = challenge_response.header['WWW-Authenticate']
23
+ begin
24
+ challenge = HTTPAuth::Digest::Challenge.from_header(challenge_header.first)
25
+ rescue HTTPAuth::UnwellformedHeader
26
+ return false
27
+ end
28
+ challenge.realm == @realm
29
+ end
30
+
31
+ def can_handle?(request)
32
+ @challenge && Addressable::URI.parse(request.uri).host == @domain
33
+ end
34
+
35
+ def add_credentials_to(request)
36
+ request.header['Authorization'] = credentials_for(request)
37
+ end
38
+
39
+ def credentials_for(request)
40
+ HTTPAuth::Digest::Credentials.from_challenge(@challenge,
41
+ :username => @username,
42
+ :password => @password,
43
+ :method => request.method.to_s.upcase,
44
+ :uri => Addressable::URI.parse(request.uri).path).to_header
45
+ end
46
+
47
+ end
48
+
49
+ end
50
+
@@ -0,0 +1,57 @@
1
+
2
+ class SsbeModel
3
+ include Datapathy::Model
4
+
5
+ persists :_type, :href, :id, :created_at, :updated_at
6
+
7
+ def initialize(attributes = {})
8
+ super
9
+ self._type = self.class.to_s
10
+ end
11
+
12
+ def self.service_type(service_type = nil)
13
+ if service_type
14
+ @service_type = service_type
15
+ else
16
+ @service_type
17
+ end
18
+ end
19
+
20
+ def self.resource_name(resource_name = nil)
21
+ if resource_name
22
+ @resource_name = resource_name
23
+ else
24
+ @resource_name
25
+ end
26
+ end
27
+
28
+ # Get a single resource from a known location
29
+ def self.at(href)
30
+ self.detect { |m|
31
+ m.href == href
32
+ }
33
+ end
34
+
35
+ # Get a collection from a location other than the default
36
+ def self.from(href)
37
+ query = SsbeConsole::SsbeQuery.new(model)
38
+ query.location = href
39
+ Datapathy::Collection.new(query)
40
+ end
41
+
42
+ def self.key
43
+ :href
44
+ end
45
+
46
+ end
47
+
48
+ module SsbeConsole
49
+ class SsbeQuery < Datapathy::Query
50
+ def location
51
+ @location
52
+ end
53
+ def location=(href)
54
+ @location = href
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,7 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe "DatapathySsbeAdapter" do
4
+ it "fails" do
5
+ fail "hey buddy, you should probably rename this file and start specing for real"
6
+ end
7
+ end
@@ -0,0 +1,9 @@
1
+ require 'spec'
2
+
3
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
4
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
5
+ require 'datapathy-ssbe-adapter'
6
+
7
+ Spec::Runner.configure do |config|
8
+
9
+ end
metadata ADDED
@@ -0,0 +1,68 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: paul-datapathy-ssbe-adapter
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Paul Sadauskas
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-09-24 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description:
17
+ email: psadauskas@gmail.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files:
23
+ - LICENSE
24
+ - README.rdoc
25
+ files:
26
+ - .document
27
+ - .gitignore
28
+ - LICENSE
29
+ - README.rdoc
30
+ - Rakefile
31
+ - VERSION
32
+ - lib/datapathy-ssbe-adapter.rb
33
+ - lib/resource_descriptor.rb
34
+ - lib/service_descriptor.rb
35
+ - lib/ssbe_authenticator.rb
36
+ - lib/ssbe_model.rb
37
+ - spec/datapathy-ssbe-adapter_spec.rb
38
+ - spec/spec_helper.rb
39
+ has_rdoc: false
40
+ homepage: http://github.com/paul/datapathy-ssbe-adapter
41
+ licenses:
42
+ post_install_message:
43
+ rdoc_options:
44
+ - --charset=UTF-8
45
+ require_paths:
46
+ - lib
47
+ required_ruby_version: !ruby/object:Gem::Requirement
48
+ requirements:
49
+ - - ">="
50
+ - !ruby/object:Gem::Version
51
+ version: "0"
52
+ version:
53
+ required_rubygems_version: !ruby/object:Gem::Requirement
54
+ requirements:
55
+ - - ">="
56
+ - !ruby/object:Gem::Version
57
+ version: "0"
58
+ version:
59
+ requirements: []
60
+
61
+ rubyforge_project:
62
+ rubygems_version: 1.3.5
63
+ signing_key:
64
+ specification_version: 3
65
+ summary: TODO
66
+ test_files:
67
+ - spec/spec_helper.rb
68
+ - spec/datapathy-ssbe-adapter_spec.rb