restpack-group-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,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,8 @@
1
+ source 'https://rubygems.org'
2
+
3
+ group :test do
4
+ gem 'rspec', :require => 'spec'
5
+ end
6
+
7
+ gemspec
8
+
data/LICENSE ADDED
@@ -0,0 +1,19 @@
1
+ Copyright (c) 2013 Gavin Joyce and RestPack contributors
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy of
4
+ this software and associated documentation files (the "Software"), to deal in
5
+ the Software without restriction, including without limitation the rights to
6
+ use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
7
+ of the Software, and to permit persons to whom the Software is furnished to do
8
+ so, subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included in all
11
+ copies or substantial portions of the Software.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19
+ SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,9 @@
1
+ # restpack-group-client [![Build Status](https://api.travis-ci.org/RestPack/restpack-group-client.png?branch=master)](https://travis-ci.org/RestPack/restpack-group-client) [![Code Climate](https://codeclimate.com/github/RestPack/restpack-group-client.png)](https://codeclimate.com/github/RestPack/restpack-group-client) [![Dependency Status](https://gemnasium.com/RestPack/restpack-group-client.png)](https://gemnasium.com/RestPack/restpack-group-client)
2
+
3
+ A client gem to the restpack-group-service
4
+
5
+ ## Running Tests
6
+
7
+ ```
8
+ rake test
9
+ ```
data/Rakefile ADDED
@@ -0,0 +1,31 @@
1
+ require "bundler/gem_tasks"
2
+
3
+ task :default => :test
4
+ task :test => :spec
5
+
6
+ begin
7
+ require "rspec/core/rake_task"
8
+
9
+ desc "Run all specs"
10
+ RSpec::Core::RakeTask.new(:spec) do |t|
11
+ t.rspec_opts = ['-cfs']
12
+ end
13
+ rescue LoadError
14
+ end
15
+
16
+ task :gem do
17
+ ["gem:build", "gem:push"].each do |task|
18
+ Rake::Task[task].reenable
19
+ Rake::Task[task].invoke
20
+ end
21
+ end
22
+
23
+ namespace :gem do
24
+ task :build do
25
+ sh "gem build restpack-group-client.gemspec"
26
+ end
27
+
28
+ task :push do
29
+ sh "gem push restpack-group-client-#{RestPack::Group::Client::VERSION}.gem"
30
+ end
31
+ end
@@ -0,0 +1,4 @@
1
+ require 'restpack-group-client/version'
2
+ require 'restpack-group-client/client'
3
+ require 'rest_client'
4
+ require 'yajl/json_gem'
@@ -0,0 +1,38 @@
1
+ require 'uri'
2
+ require_relative 'restpack_client'
3
+ require_relative 'models/group'
4
+ require_relative 'models/membership'
5
+ require_relative 'models/invitation'
6
+
7
+ module RestPack
8
+ module Group
9
+ module Client
10
+ class Api < RestPackClientApi
11
+ def groups
12
+ @group_client ||= GroupClient.new(@options)
13
+ end
14
+ end
15
+
16
+ class GroupClient < RestPackClientApi
17
+ def index(params)
18
+ #TODO: GJ: require channel_id
19
+ response = get('/api/v1/groups.json', params)
20
+
21
+ memberships = []
22
+ invitations = []
23
+ groups = []
24
+
25
+ memberships = response[:memberships].map { |m| Membership.new(m) } if response[:memberships]
26
+ invitations = response[:invitations].map { |i| Invitation.new(i) } if response[:invitations]
27
+ groups = response[:groups].map {|g| Group.new(g, memberships, invitations) } if response[:groups]
28
+
29
+ {
30
+ groups: groups,
31
+ memberships: memberships,
32
+ invitations: invitations
33
+ }
34
+ end
35
+ end
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,33 @@
1
+ module RestPack::Group::Client
2
+ class Group
3
+ attr_accessor :id, :name, :description, :channel_id, :created_by_user_id, :created_at, :updated_at,
4
+ :memberships, :invitations
5
+
6
+ def initialize(data, memberships, invitations)
7
+ @id = data[:id]
8
+ @name = data[:name]
9
+ @name = data[:description]
10
+ @name = data[:channel_id]
11
+ @name = data[:created_by_user_id]
12
+ @name = data[:created_at]
13
+ @name = data[:updated_at]
14
+
15
+ @memberships = []
16
+ @invitations = []
17
+
18
+ for id in data[:membership_ids]
19
+ membership = memberships.first { |m| m.id == id }
20
+ membership.group = self
21
+ membership.invitation = invitations.first { |i| i.id == membership.invitation_id }
22
+ @memberships << membership
23
+ end
24
+
25
+ for id in data[:invitation_ids]
26
+ invitation = invitations.first { |i| m.id == id }
27
+ invitation.group = self
28
+ invitation.membership = memberships.first { |m| m.id == invitation.membership_id }
29
+ @invitations << invitation
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,24 @@
1
+ module RestPack::Group::Client
2
+ class Invitation
3
+ attr_accessor :id, :channel_id, :group_id, :inviter_id, :invitee_id, :status, :email, :access_key,
4
+ :remaining_uses, :expires_at, :created_at, :updated_at, :group, :membership
5
+
6
+ def initialize(data)
7
+ @group = nil
8
+ @membership = nil
9
+
10
+ @id = data[:id]
11
+ @channel_id = data[:channel_id]
12
+ @group_id = data[:group_id]
13
+ @inviter_id = data[:inviter_id]
14
+ @invitee_id = data[:invitee_id]
15
+ @status = data[:status]
16
+ @email = data[:email]
17
+ @access_key = data[:access_key]
18
+ @remaining_uses = data[:remaining_uses]
19
+ @expires_at = data[:expires_at]
20
+ @created_at = data[:created_at]
21
+ @updated_at = data[:updated_at]
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,19 @@
1
+ module RestPack::Group::Client
2
+ class Membership
3
+ attr_accessor :id, :channel_id, :group_id, :user_id, :created_at, :updated_at, :invitation_id,
4
+ :group, :invitation
5
+
6
+ def initialize(data)
7
+ @group = nil
8
+ @invitation = nil
9
+
10
+ @id = data[:id]
11
+ @channel_id = data[:channel_id]
12
+ @group_id = data[:group_id]
13
+ @user_id = data[:user_id]
14
+ @created_at = data[:created_at]
15
+ @updated_at = data[:updated_at]
16
+ @invitation_id = data[:invitation_id]
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,32 @@
1
+ require 'rest-client'
2
+
3
+ module RestPack
4
+ module Group
5
+ class RestPackClientApi #TODO: GJ: move to common client gem
6
+ attr_accessor :options
7
+ def initialize(options = {})
8
+ @options = {
9
+ use_https: true,
10
+ domain: 'localhost:1113',
11
+ username: nil,
12
+ password: nil
13
+ }.merge options
14
+ end
15
+
16
+ def get(path, params = {})
17
+ url = build_url(path)
18
+ json = RestClient.get(url, params: params)
19
+ JSON.parse(json, :symbolize_keys => true)
20
+ end
21
+
22
+ def build_url(path)
23
+ protocol = @options[:use_https] ? "https://" : "http://"
24
+ username_and_password = ''
25
+ if options[:username] || options[:password]
26
+ username_and_password = "#{options[:username]}:#{options[:password]}@"
27
+ end
28
+ protocol + username_and_password + options[:domain] + path
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,7 @@
1
+ module RestPack
2
+ module Group
3
+ module Client
4
+ VERSION = "0.0.1"
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,25 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'restpack-group-client'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "restpack-group-client"
8
+ gem.version = RestPack::Group::Client::VERSION
9
+ gem.authors = ["Gavin Joyce"]
10
+ gem.email = ["gavinjoyce@gmail.com"]
11
+ gem.description = %q{A simple client gem for restpack-group-service}
12
+ gem.summary = %q{A simple client gem for restpack-group-service}
13
+ gem.homepage = "https://github.com/RestPack"
14
+
15
+ gem.files = `git ls-files`.split($/)
16
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
+ gem.require_paths = ["lib"]
19
+
20
+ gem.add_dependency 'rest-client', '~> 1.6.7'
21
+ gem.add_dependency 'yajl-ruby', '~> 1.1.0'
22
+
23
+ gem.add_development_dependency 'rspec', '~> 2.12.0'
24
+ gem.add_development_dependency 'rake', '~> 10.0.3'
25
+ end
@@ -0,0 +1,63 @@
1
+ require 'spec_helper'
2
+
3
+ describe RestPack::Group::Client do
4
+
5
+ context "when initialising" do
6
+ describe "with default options" do
7
+ it "has defaults" do
8
+ client = RestPack::Group::Client::Api.new
9
+ client.options[:use_https].should == true
10
+ client.options[:domain].should == 'localhost:1113'
11
+ end
12
+ end
13
+
14
+ describe "with specified options" do
15
+ it "overrides defaults" do
16
+ client = RestPack::Group::Client::Api.new use_https: false, domain: 'group.services.internal.acme.org'
17
+ client.options[:use_https].should == false
18
+ client.options[:domain].should == 'group.services.internal.acme.org'
19
+ end
20
+ end
21
+ end
22
+
23
+ context "Group endpoint" do
24
+ let(:client) { client = RestPack::Group::Client::Api.new(use_https: false, password: '1234') }
25
+ let(:params) { { channel_id: 1 } }
26
+ before(:each) do
27
+ RestClient.should_receive(:get)
28
+ .with('http://:1234@localhost:1113/api/v1/groups.json', params: params)
29
+ .and_return(json)
30
+
31
+ @response = client.groups.index(params)
32
+ end
33
+
34
+ context "with data" do
35
+ let(:json) { File.read('./spec/fixtures/group/index.json') }
36
+ describe "#index" do
37
+ it "parses response correctly" do
38
+ @response[:groups].length.should == 2
39
+ @response[:memberships].length.should == 3
40
+ @response[:invitations].length.should == 2
41
+ end
42
+
43
+ it "returns hierarichal group, membership and invitation data" do
44
+ @response[:groups][0].memberships.length.should == 2
45
+ @response[:groups][1].memberships.length.should == 1
46
+
47
+ @response[:groups][0].invitations.length.should == 2
48
+ @response[:groups][1].invitations.length.should == 0
49
+ end
50
+ end
51
+ end
52
+
53
+ context "with empty data" do
54
+ let(:json) { File.read('./spec/fixtures/group/index-empty.json') }
55
+ it "returns valid data" do
56
+ @response[:groups].length.should == 0
57
+ @response[:memberships].length.should == 0
58
+ @response[:invitations].length.should == 0
59
+ end
60
+ end
61
+ end
62
+
63
+ end
@@ -0,0 +1,3 @@
1
+ {
2
+ "groups": []
3
+ }
@@ -0,0 +1,100 @@
1
+ {
2
+ "memberships": [
3
+ {
4
+ "id": 14,
5
+ "channel_id": 1,
6
+ "group_id": 14,
7
+ "user_id": 4,
8
+ "created_at": "2013-03-16T08:42:32Z",
9
+ "updated_at": "2013-03-16T08:42:32Z",
10
+ "url": "/api/v1/memberships/14.json",
11
+ "invitation_id": 23
12
+ },
13
+ {
14
+ "id": 12,
15
+ "channel_id": 1,
16
+ "group_id": 14,
17
+ "user_id": 3,
18
+ "created_at": "2013-03-16T08:36:04Z",
19
+ "updated_at": "2013-03-16T08:36:04Z",
20
+ "url": "/api/v1/memberships/12.json",
21
+ "invitation_id": null
22
+ },
23
+ {
24
+ "id": 13,
25
+ "channel_id": 1,
26
+ "group_id": 15,
27
+ "user_id": 3,
28
+ "created_at": "2013-03-16T08:36:27Z",
29
+ "updated_at": "2013-03-16T08:36:27Z",
30
+ "url": "/api/v1/memberships/13.json",
31
+ "invitation_id": null
32
+ }
33
+ ],
34
+ "groups": [
35
+ {
36
+ "id": 14,
37
+ "name": "The First Group",
38
+ "description": "This group is the first",
39
+ "channel_id": 1,
40
+ "created_by_user_id": 3,
41
+ "created_at": "2013-03-16T08:36:04Z",
42
+ "updated_at": "2013-03-16T08:36:04Z",
43
+ "url": "/api/v1/groups/14.json",
44
+ "membership_ids": [
45
+ 12,
46
+ 14
47
+ ],
48
+ "invitation_ids": [
49
+ 22,
50
+ 23
51
+ ]
52
+ },
53
+ {
54
+ "id": 15,
55
+ "name": "The Second Group",
56
+ "description": "This group is the second",
57
+ "channel_id": 1,
58
+ "created_by_user_id": 3,
59
+ "created_at": "2013-03-16T08:36:27Z",
60
+ "updated_at": "2013-03-16T08:36:27Z",
61
+ "url": "/api/v1/groups/15.json",
62
+ "membership_ids": [
63
+ 13
64
+ ],
65
+ "invitation_ids": []
66
+ }
67
+ ],
68
+ "invitations": [
69
+ {
70
+ "id": 23,
71
+ "channel_id": 1,
72
+ "group_id": 14,
73
+ "inviter_id": 3,
74
+ "invitee_id": 4,
75
+ "status": "accepted",
76
+ "email": null,
77
+ "access_key": "sHjSy",
78
+ "remaining_uses": null,
79
+ "expires_at": null,
80
+ "created_at": "2013-03-16T08:37:18Z",
81
+ "updated_at": "2013-03-16T08:42:32Z",
82
+ "url": "/api/v1/invitations/23.json"
83
+ },
84
+ {
85
+ "id": 22,
86
+ "channel_id": 1,
87
+ "group_id": 14,
88
+ "inviter_id": 3,
89
+ "invitee_id": 142857,
90
+ "status": "available",
91
+ "email": null,
92
+ "access_key": "Tt97X4Hx",
93
+ "remaining_uses": null,
94
+ "expires_at": null,
95
+ "created_at": "2013-03-16T08:36:57Z",
96
+ "updated_at": "2013-03-16T08:36:57Z",
97
+ "url": "/api/v1/invitations/22.json"
98
+ }
99
+ ]
100
+ }
@@ -0,0 +1,6 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+ Bundler.setup(:default, :test)
4
+ require 'rspec'
5
+
6
+ require 'restpack-group-client'
metadata ADDED
@@ -0,0 +1,130 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: restpack-group-client
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Gavin Joyce
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-03-16 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rest-client
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: 1.6.7
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: 1.6.7
30
+ - !ruby/object:Gem::Dependency
31
+ name: yajl-ruby
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ~>
36
+ - !ruby/object:Gem::Version
37
+ version: 1.1.0
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ~>
44
+ - !ruby/object:Gem::Version
45
+ version: 1.1.0
46
+ - !ruby/object:Gem::Dependency
47
+ name: rspec
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ~>
52
+ - !ruby/object:Gem::Version
53
+ version: 2.12.0
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: 2.12.0
62
+ - !ruby/object:Gem::Dependency
63
+ name: rake
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ~>
68
+ - !ruby/object:Gem::Version
69
+ version: 10.0.3
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ~>
76
+ - !ruby/object:Gem::Version
77
+ version: 10.0.3
78
+ description: A simple client gem for restpack-group-service
79
+ email:
80
+ - gavinjoyce@gmail.com
81
+ executables: []
82
+ extensions: []
83
+ extra_rdoc_files: []
84
+ files:
85
+ - .gitignore
86
+ - Gemfile
87
+ - LICENSE
88
+ - README.md
89
+ - Rakefile
90
+ - lib/restpack-group-client.rb
91
+ - lib/restpack-group-client/client.rb
92
+ - lib/restpack-group-client/models/group.rb
93
+ - lib/restpack-group-client/models/invitation.rb
94
+ - lib/restpack-group-client/models/membership.rb
95
+ - lib/restpack-group-client/restpack_client.rb
96
+ - lib/restpack-group-client/version.rb
97
+ - restpack-group-client.gemspec
98
+ - spec/client_spec.rb
99
+ - spec/fixtures/group/index-empty.json
100
+ - spec/fixtures/group/index.json
101
+ - spec/spec_helper.rb
102
+ homepage: https://github.com/RestPack
103
+ licenses: []
104
+ post_install_message:
105
+ rdoc_options: []
106
+ require_paths:
107
+ - lib
108
+ required_ruby_version: !ruby/object:Gem::Requirement
109
+ none: false
110
+ requirements:
111
+ - - ! '>='
112
+ - !ruby/object:Gem::Version
113
+ version: '0'
114
+ required_rubygems_version: !ruby/object:Gem::Requirement
115
+ none: false
116
+ requirements:
117
+ - - ! '>='
118
+ - !ruby/object:Gem::Version
119
+ version: '0'
120
+ requirements: []
121
+ rubyforge_project:
122
+ rubygems_version: 1.8.24
123
+ signing_key:
124
+ specification_version: 3
125
+ summary: A simple client gem for restpack-group-service
126
+ test_files:
127
+ - spec/client_spec.rb
128
+ - spec/fixtures/group/index-empty.json
129
+ - spec/fixtures/group/index.json
130
+ - spec/spec_helper.rb