supplejack_client 1.0.1

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.
Files changed (47) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +27 -0
  3. data/.rspec +2 -0
  4. data/Gemfile +16 -0
  5. data/Guardfile +24 -0
  6. data/LICENSE.txt +674 -0
  7. data/README.md +153 -0
  8. data/Rakefile +9 -0
  9. data/lib/generators/locales/en.yml +11 -0
  10. data/lib/generators/supplejack/install_generator.rb +28 -0
  11. data/lib/generators/templates/README +19 -0
  12. data/lib/generators/templates/supplejack_client.rb +120 -0
  13. data/lib/supplejack/config.rb +116 -0
  14. data/lib/supplejack/controllers/helpers.rb +172 -0
  15. data/lib/supplejack/engine.rb +20 -0
  16. data/lib/supplejack/exceptions.rb +17 -0
  17. data/lib/supplejack/facet.rb +33 -0
  18. data/lib/supplejack/item.rb +94 -0
  19. data/lib/supplejack/item_relation.rb +73 -0
  20. data/lib/supplejack/log_subscriber.rb +58 -0
  21. data/lib/supplejack/paginated_collection.rb +61 -0
  22. data/lib/supplejack/record.rb +147 -0
  23. data/lib/supplejack/request.rb +95 -0
  24. data/lib/supplejack/search.rb +346 -0
  25. data/lib/supplejack/url_formats/item_hash.rb +208 -0
  26. data/lib/supplejack/user.rb +132 -0
  27. data/lib/supplejack/user_set.rb +349 -0
  28. data/lib/supplejack/user_set_relation.rb +143 -0
  29. data/lib/supplejack/util.rb +120 -0
  30. data/lib/supplejack/version.rb +10 -0
  31. data/lib/supplejack_client.rb +29 -0
  32. data/spec/spec_helper.rb +23 -0
  33. data/spec/supplejack/controllers/helpers_spec.rb +277 -0
  34. data/spec/supplejack/facet_spec.rb +44 -0
  35. data/spec/supplejack/item_relation_spec.rb +111 -0
  36. data/spec/supplejack/item_spec.rb +115 -0
  37. data/spec/supplejack/log_subscriber_spec.rb +40 -0
  38. data/spec/supplejack/paginated_collection_spec.rb +43 -0
  39. data/spec/supplejack/record_spec.rb +255 -0
  40. data/spec/supplejack/request_spec.rb +195 -0
  41. data/spec/supplejack/search_spec.rb +727 -0
  42. data/spec/supplejack/url_formats/item_hash_spec.rb +341 -0
  43. data/spec/supplejack/user_set_relation_spec.rb +149 -0
  44. data/spec/supplejack/user_set_spec.rb +465 -0
  45. data/spec/supplejack/user_spec.rb +159 -0
  46. data/supplejack_client.gemspec +30 -0
  47. metadata +159 -0
@@ -0,0 +1,159 @@
1
+ # The Supplejack Client code is Crown copyright (C) 2014, New Zealand Government,
2
+ # and is licensed under the GNU General Public License, version 3.
3
+ # See https://github.com/DigitalNZ/supplejack_client for details.
4
+ #
5
+ # Supplejack was created by DigitalNZ at the National Library of NZ
6
+ # and the Department of Internal Affairs. http://digitalnz.org/supplejack
7
+
8
+ require 'spec_helper'
9
+
10
+ module Supplejack
11
+ describe User do
12
+ let(:user) { Supplejack::User.new({'id' => 'abc', 'authentication_token' => '12345'}) }
13
+
14
+ before(:each) do
15
+ Supplejack::User.stub(:get) { {'user' => {'id' => 'abc', 'authentication_token' => '12345'}} }
16
+ end
17
+
18
+ describe '#initialize' do
19
+ it 'initializes the user attributes' do
20
+ Supplejack::User.new({'authentication_token' => '12345'}).api_key.should eq '12345'
21
+ end
22
+
23
+ it 'initializes the user name' do
24
+ Supplejack::User.new({'name' => 'Juanito'}).name.should eq 'Juanito'
25
+ end
26
+
27
+ it 'initializes the sets attributes' do
28
+ Supplejack::User.new({'sets' => [{name: 'Dogs'}]}).sets_attributes.should eq [{name: 'Dogs'}]
29
+ end
30
+
31
+ it 'initializes the use_own_api attribute' do
32
+ Supplejack::User.new({'use_own_api_key' => true}).use_own_api_key.should be_true
33
+ end
34
+
35
+ it 'initializes the regenerate_api_key attribute' do
36
+ Supplejack::User.new({'regenerate_api_key' => true}).regenerate_api_key.should be_true
37
+ end
38
+ end
39
+
40
+ describe '#save' do
41
+ it 'should execute a put request with the user attribtues' do
42
+ user.stub(:api_attributes) { {username: 'John', email: 'john@boost.co.nz'} }
43
+ Supplejack::User.should_receive(:put).with('/users/12345', {}, {username: 'John', email: 'john@boost.co.nz'})
44
+ user.save.should be_true
45
+ end
46
+
47
+ context 'regenerate api key' do
48
+ before(:each) do
49
+ user.regenerate_api_key = true
50
+ end
51
+
52
+ it 'should post a nil authentication_token attribute so it is regenerated' do
53
+ Supplejack::User.should_receive(:put).with('/users/12345', {}, hash_including(authentication_token: nil))
54
+ user.save
55
+ end
56
+
57
+ it 'should set the regenerated api_key on the user' do
58
+ Supplejack::User.stub(:put).and_return({'user'=>{'id'=>'525f7a2df6941993e2000004', 'name'=>nil, 'username'=>nil, 'email'=>nil, 'api_key'=>'71H5yPsxVhDmsjmj1NJW'}})
59
+ user.save
60
+ user.instance_variable_get('@api_key').should eq '71H5yPsxVhDmsjmj1NJW'
61
+ end
62
+
63
+ end
64
+
65
+ it 'returns false when a error ocurred' do
66
+ Supplejack::User.stub(:put).and_raise(RestClient::Forbidden)
67
+ user.save.should be_false
68
+ end
69
+ end
70
+
71
+ describe '#destroy' do
72
+ it 'should execute a delete request with the admin key' do
73
+ Supplejack.stub(:api_key) { 'admin_key' }
74
+ Supplejack::User.should_receive(:delete).with('/users/abc')
75
+ user.destroy.should be_true
76
+ end
77
+
78
+ it 'returns false if there is a exception' do
79
+ Supplejack::User.stub(:delete).and_raise(RestClient::Forbidden)
80
+ user.destroy.should be_false
81
+ end
82
+ end
83
+
84
+ describe '#api_attributes' do
85
+ it 'returns the name, username, email and encrypted_password' do
86
+ user = Supplejack::User.new(name: 'John', username: 'Johnny', email: 'john@boost.co.nz', encrypted_password: 'xyz', api_key: '12345')
87
+ user.api_attributes.should eq({name: 'John', username: 'Johnny', email: 'john@boost.co.nz', encrypted_password: 'xyz'})
88
+ end
89
+
90
+ it 'doesn\'t return the attribute if not present' do
91
+ user.api_attributes.should_not include(:name)
92
+ end
93
+
94
+ context 'regenerate api key' do
95
+ let(:user) { Supplejack::User.new(api_key: '12345', regenerate_api_key: true) }
96
+
97
+ it 'returns a nil authentication_token' do
98
+ user.api_attributes.should have_key(:authentication_token)
99
+ user.api_attributes[:authentication_token].should be_nil
100
+ end
101
+ end
102
+
103
+ it 'returns the sets_attributes' do
104
+ user = Supplejack::User.new(sets: [{name: 'Dogs', privacy: 'hidden'}])
105
+ user.api_attributes[:sets].should eq [{name: 'Dogs', privacy: 'hidden'}]
106
+ end
107
+ end
108
+
109
+ describe '#use_own_api_key?' do
110
+ it 'returns false by default' do
111
+ Supplejack::User.new.use_own_api_key?.should be_false
112
+ end
113
+
114
+ it 'returns true' do
115
+ Supplejack::User.new('use_own_api_key' => true).use_own_api_key?.should be_true
116
+ end
117
+ end
118
+
119
+ describe '#regenerate_api_key?' do
120
+ it 'returns false by default' do
121
+ Supplejack::User.new.regenerate_api_key?.should be_false
122
+ end
123
+
124
+ it 'returns true' do
125
+ Supplejack::User.new('regenerate_api_key' => true).regenerate_api_key?.should be_true
126
+ end
127
+ end
128
+
129
+ describe '.find' do
130
+ it 'fetches the user from the api' do
131
+ Supplejack::User.should_receive(:get).with('/users/12345')
132
+ Supplejack::User.find('12345')
133
+ end
134
+
135
+ it 'initializes a user with the response' do
136
+ Supplejack::User.should_receive(:new).with({'id' => 'abc', 'authentication_token' => '12345'})
137
+ Supplejack::User.find('12345')
138
+ end
139
+ end
140
+
141
+ describe '.create' do
142
+ before :each do
143
+ @attributes = {email: 'dev@boost.com', name: 'dev', username: 'developer', encrypted_password: 'weird_string'}
144
+ Supplejack::User.stub(:post) { {'user' => {'email' => 'dev@boost.com', 'name' => 'dev', 'username' => 'developer', 'api_key' => '123456'}} }
145
+ end
146
+
147
+ it 'executes a post request' do
148
+ Supplejack::User.should_receive(:post).with("/users", {}, {user: @attributes})
149
+ Supplejack::User.create(@attributes)
150
+ end
151
+
152
+ it 'returns a Supplejack::User object with the api_key' do
153
+ user = Supplejack::User.create(@attributes)
154
+ user.should be_a Supplejack::User
155
+ user.api_key.should eq '123456'
156
+ end
157
+ end
158
+ end
159
+ end
@@ -0,0 +1,30 @@
1
+ # The Supplejack Client code is Crown copyright (C) 2014, New Zealand Government,
2
+ # and is licensed under the GNU General Public License, version 3.
3
+ # See https://github.com/DigitalNZ/supplejack_client for details.
4
+ #
5
+ # Supplejack was created by DigitalNZ at the National Library of NZ
6
+ # and the Department of Internal Affairs. http://digitalnz.org/supplejack
7
+
8
+ # -*- encoding: utf-8 -*-
9
+ require File.expand_path('../lib/supplejack/version', __FILE__)
10
+
11
+ Gem::Specification.new do |gem|
12
+ gem.authors = ['Supplejack']
13
+ gem.email = ['info@digitalnz.org']
14
+ gem.description = %q{ Library to abstract the interaction with the Supplejack API }
15
+ gem.summary = %q{ Connects to the API, and allows you to treat models as if they were in a local database }
16
+ gem.homepage = 'http://digitalnz.org'
17
+
18
+ gem.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
+ gem.files = `git ls-files`.split("\n")
20
+ gem.test_files = `git ls-files -- spec/*`.split("\n")
21
+ gem.name = 'supplejack_client'
22
+ gem.require_paths = ['lib']
23
+ gem.version = Supplejack::VERSION
24
+
25
+ gem.add_dependency 'rails', '~> 3.2.12'
26
+ gem.add_dependency 'rest-client', '~> 1.6'
27
+ gem.add_dependency 'rails_autolink', '~> 1.0'
28
+
29
+ gem.add_development_dependency 'rspec', '~> 2.8'
30
+ end
metadata ADDED
@@ -0,0 +1,159 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: supplejack_client
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Supplejack
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-04-15 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rails
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 3.2.12
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 3.2.12
27
+ - !ruby/object:Gem::Dependency
28
+ name: rest-client
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.6'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.6'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rails_autolink
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '2.8'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '2.8'
69
+ description: " Library to abstract the interaction with the Supplejack API "
70
+ email:
71
+ - info@digitalnz.org
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - ".rspec"
78
+ - Gemfile
79
+ - Guardfile
80
+ - LICENSE.txt
81
+ - README.md
82
+ - Rakefile
83
+ - lib/generators/locales/en.yml
84
+ - lib/generators/supplejack/install_generator.rb
85
+ - lib/generators/templates/README
86
+ - lib/generators/templates/supplejack_client.rb
87
+ - lib/supplejack/config.rb
88
+ - lib/supplejack/controllers/helpers.rb
89
+ - lib/supplejack/engine.rb
90
+ - lib/supplejack/exceptions.rb
91
+ - lib/supplejack/facet.rb
92
+ - lib/supplejack/item.rb
93
+ - lib/supplejack/item_relation.rb
94
+ - lib/supplejack/log_subscriber.rb
95
+ - lib/supplejack/paginated_collection.rb
96
+ - lib/supplejack/record.rb
97
+ - lib/supplejack/request.rb
98
+ - lib/supplejack/search.rb
99
+ - lib/supplejack/url_formats/item_hash.rb
100
+ - lib/supplejack/user.rb
101
+ - lib/supplejack/user_set.rb
102
+ - lib/supplejack/user_set_relation.rb
103
+ - lib/supplejack/util.rb
104
+ - lib/supplejack/version.rb
105
+ - lib/supplejack_client.rb
106
+ - spec/spec_helper.rb
107
+ - spec/supplejack/controllers/helpers_spec.rb
108
+ - spec/supplejack/facet_spec.rb
109
+ - spec/supplejack/item_relation_spec.rb
110
+ - spec/supplejack/item_spec.rb
111
+ - spec/supplejack/log_subscriber_spec.rb
112
+ - spec/supplejack/paginated_collection_spec.rb
113
+ - spec/supplejack/record_spec.rb
114
+ - spec/supplejack/request_spec.rb
115
+ - spec/supplejack/search_spec.rb
116
+ - spec/supplejack/url_formats/item_hash_spec.rb
117
+ - spec/supplejack/user_set_relation_spec.rb
118
+ - spec/supplejack/user_set_spec.rb
119
+ - spec/supplejack/user_spec.rb
120
+ - supplejack_client.gemspec
121
+ homepage: http://digitalnz.org
122
+ licenses: []
123
+ metadata: {}
124
+ post_install_message:
125
+ rdoc_options: []
126
+ require_paths:
127
+ - lib
128
+ required_ruby_version: !ruby/object:Gem::Requirement
129
+ requirements:
130
+ - - ">="
131
+ - !ruby/object:Gem::Version
132
+ version: '0'
133
+ required_rubygems_version: !ruby/object:Gem::Requirement
134
+ requirements:
135
+ - - ">="
136
+ - !ruby/object:Gem::Version
137
+ version: '0'
138
+ requirements: []
139
+ rubyforge_project:
140
+ rubygems_version: 2.2.0
141
+ signing_key:
142
+ specification_version: 4
143
+ summary: Connects to the API, and allows you to treat models as if they were in a
144
+ local database
145
+ test_files:
146
+ - spec/spec_helper.rb
147
+ - spec/supplejack/controllers/helpers_spec.rb
148
+ - spec/supplejack/facet_spec.rb
149
+ - spec/supplejack/item_relation_spec.rb
150
+ - spec/supplejack/item_spec.rb
151
+ - spec/supplejack/log_subscriber_spec.rb
152
+ - spec/supplejack/paginated_collection_spec.rb
153
+ - spec/supplejack/record_spec.rb
154
+ - spec/supplejack/request_spec.rb
155
+ - spec/supplejack/search_spec.rb
156
+ - spec/supplejack/url_formats/item_hash_spec.rb
157
+ - spec/supplejack/user_set_relation_spec.rb
158
+ - spec/supplejack/user_set_spec.rb
159
+ - spec/supplejack/user_spec.rb