context-io 0.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 (51) hide show
  1. data/Gemfile +4 -0
  2. data/LICENSE +21 -0
  3. data/README.md +129 -0
  4. data/Rakefile +121 -0
  5. data/SPEC.md +49 -0
  6. data/context-io.gemspec +96 -0
  7. data/lib/context-io.rb +14 -0
  8. data/lib/context-io/account.rb +254 -0
  9. data/lib/context-io/authentication.rb +23 -0
  10. data/lib/context-io/config.rb +103 -0
  11. data/lib/context-io/connection.rb +45 -0
  12. data/lib/context-io/core_ext/hash.rb +31 -0
  13. data/lib/context-io/error.rb +24 -0
  14. data/lib/context-io/error/bad_request.rb +12 -0
  15. data/lib/context-io/error/client_error.rb +10 -0
  16. data/lib/context-io/error/forbidden.rb +12 -0
  17. data/lib/context-io/error/internal_server_error.rb +10 -0
  18. data/lib/context-io/error/not_found.rb +12 -0
  19. data/lib/context-io/error/payment_required.rb +13 -0
  20. data/lib/context-io/error/server_error.rb +10 -0
  21. data/lib/context-io/error/service_unavailable.rb +13 -0
  22. data/lib/context-io/error/unauthorized.rb +12 -0
  23. data/lib/context-io/file.rb +234 -0
  24. data/lib/context-io/folder.rb +90 -0
  25. data/lib/context-io/message.rb +160 -0
  26. data/lib/context-io/oauth_provider.rb +84 -0
  27. data/lib/context-io/request.rb +70 -0
  28. data/lib/context-io/request/oauth.rb +44 -0
  29. data/lib/context-io/resource.rb +16 -0
  30. data/lib/context-io/response.rb +5 -0
  31. data/lib/context-io/response/parse_json.rb +30 -0
  32. data/lib/context-io/response/raise_client_error.rb +59 -0
  33. data/lib/context-io/response/raise_server_error.rb +32 -0
  34. data/lib/context-io/source.rb +193 -0
  35. data/lib/context-io/version.rb +7 -0
  36. data/spec/account_spec.rb +247 -0
  37. data/spec/contextio_spec.rb +45 -0
  38. data/spec/file_spec.rb +101 -0
  39. data/spec/fixtures/accounts.json +21 -0
  40. data/spec/fixtures/files.json +41 -0
  41. data/spec/fixtures/files_group.json +47 -0
  42. data/spec/fixtures/folders.json +1 -0
  43. data/spec/fixtures/messages.json +1 -0
  44. data/spec/fixtures/oauth_providers.json +12 -0
  45. data/spec/fixtures/sources.json +1 -0
  46. data/spec/folder_spec.rb +48 -0
  47. data/spec/message_spec.rb +294 -0
  48. data/spec/oauth_provider_spec.rb +88 -0
  49. data/spec/source_spec.rb +248 -0
  50. data/spec/spec_helper.rb +4 -0
  51. metadata +214 -0
@@ -0,0 +1,88 @@
1
+ require 'spec_helper'
2
+
3
+ describe ContextIO::OAuthProvider do
4
+ describe '.all' do
5
+ before(:each) do
6
+ json_providers = File.read(File.join(File.dirname(__FILE__), 'fixtures', 'oauth_providers.json'))
7
+ @request = stub_request(:get, 'https://api.context.io/2.0/oauth_providers').
8
+ to_return(:body => json_providers)
9
+ end
10
+
11
+ it 'returns an array of OAuthProvider objects' do
12
+ providers = ContextIO::OAuthProvider.all
13
+ providers.first.should be_a(ContextIO::OAuthProvider)
14
+ end
15
+
16
+ it 'calls the API request' do
17
+ ContextIO::OAuthProvider.all
18
+
19
+ @request.should have_been_requested
20
+ end
21
+
22
+ it 'sets the attributes on the provider objects' do
23
+ ContextIO::OAuthProvider.all.first.consumer_key.should == '1qa2ws3ed'
24
+ end
25
+ end
26
+
27
+ describe '.create' do
28
+ before(:each) do
29
+ @request = stub_request(:post, 'https://api.context.io/2.0/oauth_providers').
30
+ to_return(:body => '{
31
+ "success": true,
32
+ "provider_consumer_key": "a",
33
+ "resource_url": "https://api.context.io/2.0/oauth_providers/a"
34
+ }')
35
+ end
36
+
37
+ it 'returns true for a successful creation' do
38
+ ContextIO::OAuthProvider.create(:gmail, 'a', 'b').should be_true
39
+ end
40
+
41
+ it 'calls the API request' do
42
+ ContextIO::OAuthProvider.create(:gmail, 'a', 'b')
43
+
44
+ @request.should have_been_requested
45
+ end
46
+ end
47
+
48
+ describe '.find' do
49
+ before(:each) do
50
+ json_providers = File.read(File.join(File.dirname(__FILE__), 'fixtures', 'oauth_providers.json'))
51
+ @provider = MultiJson.decode(json_providers).first
52
+ @key = @provider['provider_consumer_key']
53
+ @request = stub_request(:get, "https://api.context.io/2.0/oauth_providers/#@key").
54
+ to_return(:body => MultiJson.encode(@provider))
55
+ end
56
+
57
+ it 'calls the API request' do
58
+ ContextIO::OAuthProvider.find(@key)
59
+
60
+ @request.should have_been_requested
61
+ end
62
+
63
+ it 'returns an instance of OAuthProvider' do
64
+ ContextIO::OAuthProvider.find(@key).should be_a(ContextIO::OAuthProvider)
65
+ end
66
+ end
67
+
68
+ describe '#destroy' do
69
+ before(:each) do
70
+ json_providers = File.read(File.join(File.dirname(__FILE__), 'fixtures', 'oauth_providers.json'))
71
+ provider = MultiJson.decode(json_providers).first
72
+ @key = provider['provider_consumer_key']
73
+ @provider = ContextIO::OAuthProvider.from_json(provider)
74
+ @request = stub_request(:delete, "https://api.context.io/2.0/oauth_providers/#@key").
75
+ to_return(:body => '{"success":true}')
76
+ end
77
+
78
+ it 'calls the API request' do
79
+ @provider.destroy
80
+
81
+ @request.should have_been_requested
82
+ end
83
+
84
+ it 'returns true for a successful destroy' do
85
+ @provider.destroy.should be_true
86
+ end
87
+ end
88
+ end
@@ -0,0 +1,248 @@
1
+ require "spec_helper"
2
+
3
+ describe ContextIO::Source do
4
+ before(:each) do
5
+ @fixtures_path = File.expand_path(File.join(File.dirname(__FILE__), 'fixtures'))
6
+ account_id = 'abcdef1234567890'
7
+ @account = ContextIO::Account.new
8
+ @account.instance_eval do
9
+ @id = account_id
10
+ end
11
+ @sources_url = "https://api.context.io/2.0/accounts/#{account_id}/sources"
12
+ @existing_src = ContextIO::Source.new(@account.id, {'label' => 'me.example.com::imap.example.com',
13
+ 'email' => 'me@example.com', 'server' => 'imap.example.com',
14
+ 'username' => "me", 'use_ssl' => true, 'port' => '', 'type' => 'IMAP'})
15
+ end
16
+
17
+ describe ".new" do
18
+ it "raises ArgumentError for empty account ID" do
19
+ lambda { ContextIO::Source.new(nil) }.should raise_error ArgumentError
20
+ end
21
+
22
+ it "sets attributes of Source object" do
23
+ src = ContextIO::Source.new(@account.id,
24
+ {"label" => "bi@example.com::imap.example.com",
25
+ "email" => "bi@example.com",
26
+ "authentication_type" => "oauth"})
27
+ src.account_id.should == @account.id
28
+ src.label.should == "bi@example.com::imap.example.com"
29
+ src.email.should == "bi@example.com"
30
+ src.authentication_type.should == "oauth"
31
+ end
32
+ end
33
+
34
+ describe ".all" do
35
+ before(:each) do
36
+ json_sources = File.read(File.join(@fixtures_path, 'sources.json'))
37
+ @response = stub_request(:get, @sources_url).
38
+ to_return(:body => json_sources)
39
+ end
40
+
41
+ it 'returns an array of Source objects for given account ID' do
42
+ sources = ContextIO::Source.all(@account.id)
43
+ sources.should be_a(Array)
44
+ sources.first.should be_a(ContextIO::Source)
45
+ end
46
+
47
+ it 'returns an array of Soruce objects for given Account object' do
48
+ sources = ContextIO::Source.all(@account)
49
+ sources.should be_a(Array)
50
+ sources.first.should be_a(ContextIO::Source)
51
+ end
52
+
53
+ it 'returns empty array when no account is given' do
54
+ sources = ContextIO::Source.all(nil)
55
+ sources.should be_a(Array)
56
+ sources.length.should == 0
57
+ end
58
+
59
+ it 'calls the API method' do
60
+ ContextIO::Source.all(@account)
61
+ @response.should have_been_requested
62
+ end
63
+
64
+ it 'sets attributes of Source object' do
65
+ src = ContextIO::Source.all(@account).first
66
+ src.label.should == 'bi@example.com::imap.example.com'
67
+ src.authentication_type.should == 'oauth'
68
+ src.port == 993
69
+ src.service_level.should == 'pro'
70
+ src.username.should == 'bi@example.com'
71
+ src.server.should == 'imap.example.com'
72
+ src.source_type.should == 'imap'
73
+ src.sync_period.should == '1d'
74
+ src.use_ssl.should be_true
75
+ src.status.should == 'OK'
76
+ end
77
+
78
+ it 'sends query' do
79
+ q = {
80
+ :status => 'INVALID_CREDENTIALS',
81
+ :status_ok => '1'
82
+ }
83
+
84
+ @response = @response.with(:query => q)
85
+
86
+ ContextIO::Source.all(@account, q)
87
+ @response.should have_been_requested
88
+ end
89
+ end
90
+
91
+ describe '.find' do
92
+ before(:each) do
93
+ @sources = MultiJson.decode(File.read(File.join(@fixtures_path, 'sources.json')))
94
+ @find_url = "#{@sources_url}/#{@sources.first['label']}"
95
+ @response = stub_request(:get, @find_url).
96
+ to_return(:body => MultiJson.encode(@sources.first))
97
+ end
98
+
99
+ it 'calls API method' do
100
+ ContextIO::Source.find(@account, @sources.first['label'])
101
+ @response.should have_been_requested
102
+ end
103
+
104
+ it 'returns single source for given label' do
105
+ src = ContextIO::Source.find(@account, @sources.first['label'])
106
+ src.should be_a(ContextIO::Source)
107
+ src.label.should == @sources.first['label']
108
+ end
109
+ end
110
+
111
+ describe '#save' do
112
+ it 'returns true if the save was successful' do
113
+ @stub = stub_request(:post, "https://api.context.io/2.0/accounts/#{@account.id}/sources").
114
+ with(:body => { :email => 'me@example.com', :server => 'imap@example.com',
115
+ :username => "me", :use_ssl => 'true', :port => '143', :type => 'IMAP'}).
116
+ to_return(
117
+ :body => '{
118
+ "success": true
119
+ }'
120
+ )
121
+
122
+ src = ContextIO::Source.new(@account.id, {'email' => 'me@example.com', 'server' => 'imap@example.com',
123
+ 'username' => "me", 'use_ssl' => true, 'port' => 143, 'type' => 'IMAP'})
124
+
125
+ src.save.should be_true
126
+ end
127
+
128
+ it 'returns false if the save was not successful' do
129
+ @stub = stub_request(:post, "https://api.context.io/2.0/accounts/#{@account.id}/sources").
130
+ with(:body => { :email => 'me@example.com', :server => 'imap@example.com',
131
+ :username => "me", :use_ssl => 'true', :port => '143', :type => 'IMAP'}).
132
+ to_return(
133
+ :body => '{
134
+ "success": false
135
+ }'
136
+ )
137
+
138
+ src = ContextIO::Source.new(@account.id, {'email' => 'me@example.com', 'server' => 'imap@example.com',
139
+ 'username' => "me", 'use_ssl' => true, 'port' => 143, 'type' => 'IMAP'})
140
+
141
+ src.save.should be_false
142
+ end
143
+
144
+ it 'raises ArgumentError if mandatory arguments are missing' do
145
+ src = ContextIO::Source.new(@account.id, {'email' => '', 'server' => 'imap@example.com',
146
+ 'username' => "me", 'use_ssl' => true, 'port' => '143', 'type' => 'IMAP'})
147
+ lambda { src.save }.should raise_error ArgumentError
148
+
149
+ src = ContextIO::Source.new(@account.id, {'email' => 'me@example.com', 'server' => '',
150
+ 'username' => "me", 'use_ssl' => true, 'port' => '143', 'type' => 'IMAP'})
151
+ lambda { src.save }.should raise_error ArgumentError
152
+
153
+ src = ContextIO::Source.new(@account.id, {'email' => 'me@example.com', 'server' => '',
154
+ 'username' => '', 'use_ssl' => true, 'port' => '143', 'type' => 'IMAP'})
155
+ lambda { src.save }.should raise_error ArgumentError
156
+
157
+ src = ContextIO::Source.new(@account.id, {'email' => 'me@example.com', 'server' => '',
158
+ 'username' => "me", 'use_ssl' => true, 'port' => '', 'type' => 'IMAP'})
159
+ lambda { src.save }.should raise_error ArgumentError
160
+ end
161
+ end
162
+
163
+ describe '#update_attributes' do
164
+ it 'calls the API request' do
165
+ @stub = stub_request(:post, "#{@sources_url}/#{@existing_src.label}").
166
+ with(:body => { 'sync_period' => '1d' }).
167
+ to_return(
168
+ :body => '{
169
+ "success": true
170
+ }'
171
+ )
172
+
173
+ @existing_src.update_attributes("sync_period" => '1d')
174
+
175
+ @stub.should have_been_requested
176
+ end
177
+
178
+ it 'returns true if the update was successful' do
179
+ stub_request(:post, "#{@sources_url}/#{@existing_src.label}").
180
+ to_return(
181
+ :body => '{"success": true}'
182
+ )
183
+
184
+ @existing_src.update_attributes('status' => 'OK').should be_true
185
+ end
186
+
187
+ it 'returns false if the update was unsuccessful' do
188
+ stub_request(:post, "#{@sources_url}/#{@existing_src.label}").
189
+ to_return(
190
+ :body => '{"success": false}'
191
+ )
192
+
193
+ @existing_src.update_attributes('status' => 'OK').should be_false
194
+ end
195
+
196
+ it 'sets the attributes on the Source object' do
197
+ stub_request(:post, "#{@sources_url}/#{@existing_src.label}").
198
+ to_return(
199
+ :body => '{"success": true}'
200
+ )
201
+
202
+ @existing_src.update_attributes('service_level' => 'test_level')
203
+
204
+ @existing_src.service_level.should == 'test_level'
205
+ end
206
+ end
207
+
208
+ describe "#destroy" do
209
+ it 'calls the API request' do
210
+ @stub = stub_request(:delete, "#{@sources_url}/#{@existing_src.label}").
211
+ to_return(
212
+ :body => '{"success": true}'
213
+ )
214
+
215
+ @existing_src.destroy
216
+
217
+ @stub.should have_been_requested
218
+ end
219
+
220
+ it 'returns true if destroy is successful' do
221
+ stub_request(:delete, "#{@sources_url}/#{@existing_src.label}").
222
+ to_return(
223
+ :body => '{"success": true}'
224
+ )
225
+
226
+ @existing_src.destroy.should be_true
227
+ end
228
+
229
+ it 'resets source label if destroy is successful' do
230
+ stub_request(:delete, "#{@sources_url}/#{@existing_src.label}").
231
+ to_return(
232
+ :body => '{"success": true}'
233
+ )
234
+
235
+ @existing_src.destroy
236
+ @existing_src.label.should == ''
237
+ end
238
+
239
+ it 'returns false if destroy is not successful' do
240
+ stub_request(:delete, "#{@sources_url}/#{@existing_src.label}").
241
+ to_return(
242
+ :body => '{"success": false}'
243
+ )
244
+
245
+ @existing_src.destroy.should be_false
246
+ end
247
+ end
248
+ end
@@ -0,0 +1,4 @@
1
+ require 'context-io'
2
+ require 'rspec'
3
+ require 'webmock/rspec'
4
+ require 'multi_json'
metadata ADDED
@@ -0,0 +1,214 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: context-io
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Henrik Hodne
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-01-26 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: faraday
16
+ requirement: &70137325924260 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: 0.7.5
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *70137325924260
25
+ - !ruby/object:Gem::Dependency
26
+ name: simple_oauth
27
+ requirement: &70137325923800 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ~>
31
+ - !ruby/object:Gem::Version
32
+ version: 0.1.5
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: *70137325923800
36
+ - !ruby/object:Gem::Dependency
37
+ name: multi_json
38
+ requirement: &70137325923260 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ~>
42
+ - !ruby/object:Gem::Version
43
+ version: 1.0.0
44
+ type: :runtime
45
+ prerelease: false
46
+ version_requirements: *70137325923260
47
+ - !ruby/object:Gem::Dependency
48
+ name: webmock
49
+ requirement: &70137325922740 !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: 1.7.10
55
+ type: :runtime
56
+ prerelease: false
57
+ version_requirements: *70137325922740
58
+ - !ruby/object:Gem::Dependency
59
+ name: rspec
60
+ requirement: &70137325922240 !ruby/object:Gem::Requirement
61
+ none: false
62
+ requirements:
63
+ - - ~>
64
+ - !ruby/object:Gem::Version
65
+ version: 2.8.0
66
+ type: :development
67
+ prerelease: false
68
+ version_requirements: *70137325922240
69
+ - !ruby/object:Gem::Dependency
70
+ name: rake
71
+ requirement: &70137325921780 !ruby/object:Gem::Requirement
72
+ none: false
73
+ requirements:
74
+ - - ~>
75
+ - !ruby/object:Gem::Version
76
+ version: 0.9.0
77
+ type: :development
78
+ prerelease: false
79
+ version_requirements: *70137325921780
80
+ - !ruby/object:Gem::Dependency
81
+ name: yard
82
+ requirement: &70137325921300 !ruby/object:Gem::Requirement
83
+ none: false
84
+ requirements:
85
+ - - ~>
86
+ - !ruby/object:Gem::Version
87
+ version: 0.7.4
88
+ type: :development
89
+ prerelease: false
90
+ version_requirements: *70137325921300
91
+ - !ruby/object:Gem::Dependency
92
+ name: yard-rspec
93
+ requirement: &70137325920820 !ruby/object:Gem::Requirement
94
+ none: false
95
+ requirements:
96
+ - - ~>
97
+ - !ruby/object:Gem::Version
98
+ version: '0.1'
99
+ type: :development
100
+ prerelease: false
101
+ version_requirements: *70137325920820
102
+ - !ruby/object:Gem::Dependency
103
+ name: redcarpet
104
+ requirement: &70137325936660 !ruby/object:Gem::Requirement
105
+ none: false
106
+ requirements:
107
+ - - ~>
108
+ - !ruby/object:Gem::Version
109
+ version: 2.1.0
110
+ type: :development
111
+ prerelease: false
112
+ version_requirements: *70137325936660
113
+ - !ruby/object:Gem::Dependency
114
+ name: github-markup
115
+ requirement: &70137325936180 !ruby/object:Gem::Requirement
116
+ none: false
117
+ requirements:
118
+ - - ~>
119
+ - !ruby/object:Gem::Version
120
+ version: 0.7.0
121
+ type: :development
122
+ prerelease: false
123
+ version_requirements: *70137325936180
124
+ description: Ruby wrapper for the context.io API.
125
+ email: dvyjones@dvyjones.com
126
+ executables: []
127
+ extensions: []
128
+ extra_rdoc_files:
129
+ - README.md
130
+ - LICENSE
131
+ files:
132
+ - Gemfile
133
+ - LICENSE
134
+ - README.md
135
+ - Rakefile
136
+ - SPEC.md
137
+ - context-io.gemspec
138
+ - lib/context-io.rb
139
+ - lib/context-io/account.rb
140
+ - lib/context-io/authentication.rb
141
+ - lib/context-io/config.rb
142
+ - lib/context-io/connection.rb
143
+ - lib/context-io/core_ext/hash.rb
144
+ - lib/context-io/error.rb
145
+ - lib/context-io/error/bad_request.rb
146
+ - lib/context-io/error/client_error.rb
147
+ - lib/context-io/error/forbidden.rb
148
+ - lib/context-io/error/internal_server_error.rb
149
+ - lib/context-io/error/not_found.rb
150
+ - lib/context-io/error/payment_required.rb
151
+ - lib/context-io/error/server_error.rb
152
+ - lib/context-io/error/service_unavailable.rb
153
+ - lib/context-io/error/unauthorized.rb
154
+ - lib/context-io/file.rb
155
+ - lib/context-io/folder.rb
156
+ - lib/context-io/message.rb
157
+ - lib/context-io/oauth_provider.rb
158
+ - lib/context-io/request.rb
159
+ - lib/context-io/request/oauth.rb
160
+ - lib/context-io/resource.rb
161
+ - lib/context-io/response.rb
162
+ - lib/context-io/response/parse_json.rb
163
+ - lib/context-io/response/raise_client_error.rb
164
+ - lib/context-io/response/raise_server_error.rb
165
+ - lib/context-io/source.rb
166
+ - lib/context-io/version.rb
167
+ - spec/account_spec.rb
168
+ - spec/contextio_spec.rb
169
+ - spec/file_spec.rb
170
+ - spec/fixtures/accounts.json
171
+ - spec/fixtures/files.json
172
+ - spec/fixtures/files_group.json
173
+ - spec/fixtures/folders.json
174
+ - spec/fixtures/messages.json
175
+ - spec/fixtures/oauth_providers.json
176
+ - spec/fixtures/sources.json
177
+ - spec/folder_spec.rb
178
+ - spec/message_spec.rb
179
+ - spec/oauth_provider_spec.rb
180
+ - spec/source_spec.rb
181
+ - spec/spec_helper.rb
182
+ homepage: https://github.com/dvyjones/context-io
183
+ licenses: []
184
+ post_install_message:
185
+ rdoc_options:
186
+ - --charset=UTF-8
187
+ - --markup tomdoc
188
+ - --main README.md
189
+ require_paths:
190
+ - lib
191
+ required_ruby_version: !ruby/object:Gem::Requirement
192
+ none: false
193
+ requirements:
194
+ - - ! '>='
195
+ - !ruby/object:Gem::Version
196
+ version: '0'
197
+ segments:
198
+ - 0
199
+ hash: 418150477359024522
200
+ required_rubygems_version: !ruby/object:Gem::Requirement
201
+ none: false
202
+ requirements:
203
+ - - ! '>='
204
+ - !ruby/object:Gem::Version
205
+ version: '0'
206
+ requirements: []
207
+ rubyforge_project: context-io
208
+ rubygems_version: 1.8.10
209
+ signing_key:
210
+ specification_version: 2
211
+ summary: Ruby wrapper for the context.io API.
212
+ test_files:
213
+ - spec/spec_helper.rb
214
+ has_rdoc: