crowd-client 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,4 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in crowd-client.gemspec
4
+ gemspec
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,27 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "crowd-client/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "crowd-client"
7
+ s.version = Crowd::Client::VERSION
8
+ s.authors = ["Paul Nicholson"]
9
+ s.email = ["paul@webpowerdesign.net"]
10
+ s.homepage = ""
11
+ s.summary = %q{Crowd Client Library}
12
+ s.description = %q{A simple rest client for Crowd}
13
+
14
+ s.rubyforge_project = "crowd-client"
15
+
16
+ s.files = `git ls-files`.split("\n")
17
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
18
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
+ s.require_paths = ["lib"]
20
+
21
+ s.add_development_dependency "rspec"
22
+
23
+ s.add_runtime_dependency "faraday", "~> 0.7.5"
24
+ s.add_runtime_dependency "faraday_middleware", "~> 0.7.0"
25
+ s.add_runtime_dependency "patron", "~> 0.4.16"
26
+ s.add_runtime_dependency "json", "~> 1.6.1"
27
+ end
@@ -0,0 +1,55 @@
1
+ require "crowd-client/version"
2
+ require "crowd-client/exceptions"
3
+ require "ostruct"
4
+ require "faraday"
5
+ require "faraday_middleware"
6
+ require "patron"
7
+ require "json"
8
+
9
+ module Crowd
10
+ module Client
11
+ extend self
12
+ @config = ::OpenStruct.new({
13
+ :url => 'http://127.0.0.1:8095/crowd/rest/usermanagement/1/',
14
+ :application => 'application',
15
+ :password => 'password',
16
+ :debug => false
17
+ })
18
+
19
+ def config
20
+ @config
21
+ end
22
+
23
+ def login(username, password)
24
+ response = connection.post('session', :username => username, :password => password)
25
+
26
+ raise Exception::AuthenticationFailed.new if response.status == 400 && response.body['reason'] == 'INVALID_USER_AUTHENTICATION'
27
+ raise Exception::InactiveAccount.new if response.status == 400 && response.body['reason'] == 'INACTIVE_ACCOUNT'
28
+ raise Exception::UnkownError if response.status != 201
29
+ return response.body['token']
30
+ end
31
+
32
+ def valid_session?(token)
33
+ response = connection.post("session/#{token}", {})
34
+ response.status == 200 ? true : false
35
+ end
36
+
37
+ def logout(token)
38
+ response = connection.delete("session/#{token}", {})
39
+ raise Exception::UnkownError if response.status != 204
40
+ end
41
+
42
+ def connection
43
+ ::Faraday::Connection.new(
44
+ :url => config.url,
45
+ :headers => { :accept => 'application/json' },
46
+ :user_agent => "Crowd Client for Ruby/#{VERSION}"
47
+ ) do |builder|
48
+ builder.request :json
49
+ builder.response :logger if config.debug
50
+ builder.use Faraday::Response::ParseJson
51
+ builder.adapter :patron
52
+ end.tap {|connection| connection.basic_auth config.application, config.password }
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,9 @@
1
+ module Crowd::Client
2
+ class Exception < RuntimeError; end
3
+ class Exception::UnkownError < Crowd::Client::Exception; end
4
+ class Exception::InactiveAccount < Crowd::Client::Exception; end
5
+ class Exception::AuthenticationFailed < Crowd::Client::Exception; end
6
+ end
7
+
8
+
9
+
@@ -0,0 +1,5 @@
1
+ module Crowd
2
+ module Client
3
+ VERSION = "0.0.1"
4
+ end
5
+ end
@@ -0,0 +1,47 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../lib/crowd-client')
2
+
3
+ describe Crowd::Client do
4
+
5
+ describe "#config" do
6
+ subject { Crowd::Client.config }
7
+
8
+ its(:url) { should == "http://127.0.0.1:8095/crowd/rest/usermanagement/1/" }
9
+ its(:application) { should == 'application' }
10
+ its(:password) { should == 'password' }
11
+ end
12
+
13
+ describe "#login" do
14
+
15
+ it "should authenticate and return a session token" do
16
+ subject.login('user@example.com', 'password').should =~ /[A-Za-z0-9]{24}/
17
+ end
18
+
19
+ it "should raise Crowd::Client::Exception::AuthenticationFailed if authentication fails" do
20
+ lambda { subject.login('user@example.com', 'wrong_password') }.should raise_error(Crowd::Client::Exception::AuthenticationFailed)
21
+ end
22
+
23
+
24
+ it "should raise Crowd::Client::Exception::InactiveAccount if an account is inactive" do
25
+ lambda { subject.login('inactive_user@example.com', 'password') } .should raise_error(Crowd::Client::Exception::InactiveAccount)
26
+ end
27
+ end
28
+
29
+ describe "#valid_session?" do
30
+
31
+ it "should validate the current session" do
32
+ token = subject.login('user@example.com', 'password')
33
+ subject.valid_session?(token).should be_true
34
+ end
35
+ end
36
+
37
+ describe "#logout" do
38
+
39
+ it "should logout the current session" do
40
+ token = subject.login('user@example.com', 'password')
41
+ subject.valid_session?(token).should be_true
42
+ subject.logout(token)
43
+ subject.valid_session?(token).should be_false
44
+ end
45
+
46
+ end
47
+ end
metadata ADDED
@@ -0,0 +1,109 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: crowd-client
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Paul Nicholson
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2011-10-18 00:00:00.000000000Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rspec
16
+ requirement: &70300634298280 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: *70300634298280
25
+ - !ruby/object:Gem::Dependency
26
+ name: faraday
27
+ requirement: &70300634296840 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ~>
31
+ - !ruby/object:Gem::Version
32
+ version: 0.7.5
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: *70300634296840
36
+ - !ruby/object:Gem::Dependency
37
+ name: faraday_middleware
38
+ requirement: &70300634295460 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ~>
42
+ - !ruby/object:Gem::Version
43
+ version: 0.7.0
44
+ type: :runtime
45
+ prerelease: false
46
+ version_requirements: *70300634295460
47
+ - !ruby/object:Gem::Dependency
48
+ name: patron
49
+ requirement: &70300634278140 !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: 0.4.16
55
+ type: :runtime
56
+ prerelease: false
57
+ version_requirements: *70300634278140
58
+ - !ruby/object:Gem::Dependency
59
+ name: json
60
+ requirement: &70300634277380 !ruby/object:Gem::Requirement
61
+ none: false
62
+ requirements:
63
+ - - ~>
64
+ - !ruby/object:Gem::Version
65
+ version: 1.6.1
66
+ type: :runtime
67
+ prerelease: false
68
+ version_requirements: *70300634277380
69
+ description: A simple rest client for Crowd
70
+ email:
71
+ - paul@webpowerdesign.net
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - .gitignore
77
+ - Gemfile
78
+ - Rakefile
79
+ - crowd-client.gemspec
80
+ - lib/crowd-client.rb
81
+ - lib/crowd-client/exceptions.rb
82
+ - lib/crowd-client/version.rb
83
+ - spec/crowd-client_spec.rb
84
+ homepage: ''
85
+ licenses: []
86
+ post_install_message:
87
+ rdoc_options: []
88
+ require_paths:
89
+ - lib
90
+ required_ruby_version: !ruby/object:Gem::Requirement
91
+ none: false
92
+ requirements:
93
+ - - ! '>='
94
+ - !ruby/object:Gem::Version
95
+ version: '0'
96
+ required_rubygems_version: !ruby/object:Gem::Requirement
97
+ none: false
98
+ requirements:
99
+ - - ! '>='
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ requirements: []
103
+ rubyforge_project: crowd-client
104
+ rubygems_version: 1.8.10
105
+ signing_key:
106
+ specification_version: 3
107
+ summary: Crowd Client Library
108
+ test_files:
109
+ - spec/crowd-client_spec.rb