assistly 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 (52) hide show
  1. data/.gemtest +0 -0
  2. data/.gitignore +11 -0
  3. data/.rspec +3 -0
  4. data/.travis.yml +7 -0
  5. data/.yardopts +9 -0
  6. data/Gemfile +12 -0
  7. data/HISTORY.mkd +4 -0
  8. data/LICENSE.mkd +20 -0
  9. data/README.mkd +121 -0
  10. data/Rakefile +23 -0
  11. data/assistly.gemspec +43 -0
  12. data/lib/assistly.rb +26 -0
  13. data/lib/assistly/api.rb +23 -0
  14. data/lib/assistly/authentication.rb +25 -0
  15. data/lib/assistly/client.rb +25 -0
  16. data/lib/assistly/client/case.rb +51 -0
  17. data/lib/assistly/client/customer.rb +108 -0
  18. data/lib/assistly/client/interaction.rb +41 -0
  19. data/lib/assistly/client/user.rb +38 -0
  20. data/lib/assistly/client/utils.rb +83 -0
  21. data/lib/assistly/configuration.rb +104 -0
  22. data/lib/assistly/connection.rb +39 -0
  23. data/lib/assistly/error.rb +62 -0
  24. data/lib/assistly/request.rb +44 -0
  25. data/lib/assistly/search.rb +473 -0
  26. data/lib/assistly/version.rb +4 -0
  27. data/lib/faraday/request/multipart_with_file.rb +30 -0
  28. data/lib/faraday/response/raise_http_4xx.rb +45 -0
  29. data/lib/faraday/response/raise_http_5xx.rb +24 -0
  30. data/spec/assistly/api_spec.rb +70 -0
  31. data/spec/assistly/client/case_spec.rb +88 -0
  32. data/spec/assistly/client/customer_spec.rb +158 -0
  33. data/spec/assistly/client/interaction_spec.rb +58 -0
  34. data/spec/assistly/client/user_spec.rb +58 -0
  35. data/spec/assistly/client_spec.rb +10 -0
  36. data/spec/assistly_spec.rb +99 -0
  37. data/spec/faraday/response_spec.rb +34 -0
  38. data/spec/fixtures/case.json +59 -0
  39. data/spec/fixtures/case_update.json +59 -0
  40. data/spec/fixtures/cases.json +182 -0
  41. data/spec/fixtures/customer.json +58 -0
  42. data/spec/fixtures/customer_create.json +56 -0
  43. data/spec/fixtures/customer_create_email.json +15 -0
  44. data/spec/fixtures/customer_update.json +47 -0
  45. data/spec/fixtures/customer_update_email.json +15 -0
  46. data/spec/fixtures/customers.json +98 -0
  47. data/spec/fixtures/interaction_create.json +126 -0
  48. data/spec/fixtures/interactions.json +139 -0
  49. data/spec/fixtures/user.json +15 -0
  50. data/spec/fixtures/users.json +24 -0
  51. data/spec/helper.rb +53 -0
  52. metadata +391 -0
@@ -0,0 +1,4 @@
1
+ module Assistly
2
+ # The version of the gem
3
+ VERSION = '0.1'.freeze unless defined?(::Assistly::VERSION)
4
+ end
@@ -0,0 +1,30 @@
1
+ require 'faraday'
2
+
3
+ # @private
4
+ module Faraday
5
+ # @private
6
+ class Request::MultipartWithFile < Faraday::Middleware
7
+ def call(env)
8
+ if env[:body].is_a?(Hash)
9
+ env[:body].each do |key, value|
10
+ if value.is_a?(File)
11
+ env[:body][key] = Faraday::UploadIO.new(value, mime_type(value), value.path)
12
+ end
13
+ end
14
+ end
15
+
16
+ @app.call(env)
17
+ end
18
+
19
+ private
20
+
21
+ def mime_type(file)
22
+ case file.path
23
+ when /\.jpe?g/i then 'image/jpeg'
24
+ when /\.gif$/i then 'image/gif'
25
+ when /\.png$/i then 'image/png'
26
+ else 'application/octet-stream'
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,45 @@
1
+ require 'faraday'
2
+
3
+ # @private
4
+ module Faraday
5
+ # @private
6
+ class Response::RaiseHttp4xx < Response::Middleware
7
+ def on_complete(env)
8
+ case env[:status].to_i
9
+ when 400
10
+ raise Assistly::BadRequest.new(error_message(env), env[:response_headers])
11
+ when 401
12
+ raise Assistly::Unauthorized.new(error_message(env), env[:response_headers])
13
+ when 403
14
+ raise Assistly::Forbidden.new(error_message(env), env[:response_headers])
15
+ when 404
16
+ raise Assistly::NotFound.new(error_message(env), env[:response_headers])
17
+ when 406
18
+ raise Assistly::NotAcceptable.new(error_message(env), env[:response_headers])
19
+ when 420
20
+ raise Assistly::EnhanceYourCalm.new(error_message(env), env[:response_headers])
21
+ end
22
+ end
23
+
24
+ private
25
+
26
+ def error_message(env)
27
+ "#{env[:method].to_s.upcase} #{env[:url].to_s}: #{env[:status]}#{error_body(env[:body])}"
28
+ end
29
+
30
+ def error_body(body)
31
+ if body.nil?
32
+ nil
33
+ elsif body['error']
34
+ ": #{body['error']}"
35
+ elsif body['errors']
36
+ first = body['errors'].to_a.first
37
+ if first.kind_of? Hash
38
+ ": #{first['message'].chomp}"
39
+ else
40
+ ": #{first.chomp}"
41
+ end
42
+ end
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,24 @@
1
+ require 'faraday'
2
+
3
+ # @private
4
+ module Faraday
5
+ # @private
6
+ class Response::RaiseHttp5xx < Response::Middleware
7
+ def on_complete(env)
8
+ case env[:status].to_i
9
+ when 500
10
+ raise Assistly::InternalServerError.new(error_message(env, "Something is technically wrong."), env[:response_headers])
11
+ when 502
12
+ raise Assistly::BadGateway.new(error_message(env, "Assistly is down or being upgraded."), env[:response_headers])
13
+ when 503
14
+ raise Assistly::ServiceUnavailable.new(error_message(env, "(__-){ Assistly is over capacity."), env[:response_headers])
15
+ end
16
+ end
17
+
18
+ private
19
+
20
+ def error_message(env, body=nil)
21
+ "#{env[:method].to_s.upcase} #{env[:url].to_s}: #{[env[:status].to_s + ':', body].compact.join(' ')} Check http://status.twitter.com/ for updates on the status of the Assistly service."
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,70 @@
1
+ require 'helper'
2
+
3
+ describe Assistly::API do
4
+ before do
5
+ @keys = Assistly::Configuration::VALID_OPTIONS_KEYS
6
+ end
7
+
8
+ context "with module configuration" do
9
+
10
+ before do
11
+ Assistly.configure do |config|
12
+ @keys.each do |key|
13
+ config.send("#{key}=", key)
14
+ end
15
+ end
16
+ end
17
+
18
+ after do
19
+ Assistly.reset
20
+ end
21
+
22
+ it "should inherit module configuration" do
23
+ api = Assistly::API.new
24
+ @keys.each do |key|
25
+ api.send(key).should == key
26
+ end
27
+ end
28
+
29
+ context "with class configuration" do
30
+
31
+ before do
32
+ @configuration = {
33
+ :consumer_key => 'CK',
34
+ :consumer_secret => 'CS',
35
+ :oauth_token => 'OT',
36
+ :oauth_token_secret => 'OS',
37
+ :adapter => :typhoeus,
38
+ :endpoint => 'http://tumblr.com/',
39
+ :format => :xml,
40
+ :proxy => 'http://erik:sekret@proxy.example.com:8080',
41
+ :subdomain => 'zencoder',
42
+ :user_agent => 'Custom User Agent',
43
+ :version => "amazing"
44
+ }
45
+ end
46
+
47
+ context "during initialization"
48
+
49
+ it "should override module configuration" do
50
+ api = Assistly::API.new(@configuration)
51
+ @keys.each do |key|
52
+ api.send(key).should == @configuration[key]
53
+ end
54
+ end
55
+
56
+ context "after initilization" do
57
+
58
+ it "should override module configuration after initialization" do
59
+ api = Assistly::API.new
60
+ @configuration.each do |key, value|
61
+ api.send("#{key}=", value)
62
+ end
63
+ @keys.each do |key|
64
+ api.send(key).should == @configuration[key]
65
+ end
66
+ end
67
+ end
68
+ end
69
+ end
70
+ end
@@ -0,0 +1,88 @@
1
+ require 'helper'
2
+
3
+ describe Assistly::Client do
4
+ Assistly::Configuration::VALID_FORMATS.each do |format|
5
+ context ".new(:format => '#{format}')" do
6
+ before do
7
+ @client = Assistly::Client.new(:subdomain => "example", :format => format, :consumer_key => 'CK', :consumer_secret => 'CS', :oauth_token => 'OT', :oauth_token_secret => 'OS')
8
+ end
9
+
10
+ describe ".cases" do
11
+
12
+ context "lookup" do
13
+
14
+ before do
15
+ stub_get("cases.#{format}").
16
+ to_return(:body => fixture("cases.#{format}"), :headers => {:content_type => "application/#{format}; charset=utf-8"})
17
+ end
18
+
19
+ it "should get the correct resource" do
20
+ @client.cases
21
+ a_get("cases.#{format}").
22
+ should have_been_made
23
+ end
24
+
25
+ it "should return up to 100 cases worth of extended information" do
26
+ cases = @client.cases
27
+
28
+ cases.should be_a Array
29
+ cases.first.case.id.should == 1
30
+ cases.first.case.user.name.should == "Jeremy Suriel"
31
+ end
32
+
33
+ end
34
+ end
35
+
36
+ describe ".case" do
37
+
38
+ context "lookup" do
39
+
40
+ before do
41
+ stub_get("cases/1.#{format}").
42
+ to_return(:body => fixture("case.#{format}"), :headers => {:content_type => "application/#{format}; charset=utf-8"})
43
+ end
44
+
45
+ it "should get the correct resource" do
46
+ @client.case(1)
47
+ a_get("cases/1.#{format}").
48
+ should have_been_made
49
+ end
50
+
51
+ it "should return up to 100 cases worth of extended information" do
52
+ a_case = @client.case(1)
53
+
54
+ a_case.id.should == 1
55
+ a_case.external_id.should == "123"
56
+ a_case.subject.should == "Welcome to Assistly"
57
+ end
58
+
59
+ end
60
+ end
61
+
62
+ describe ".update_case" do
63
+
64
+ context "update" do
65
+
66
+ before do
67
+ stub_put("cases/1.#{format}").
68
+ to_return(:body => fixture("case_update.#{format}"), :headers => {:content_type => "application/#{format}; charset=utf-8"})
69
+ end
70
+
71
+ it "should get the correct resource" do
72
+ @client.update_case(1, :subject => "Welcome to Assistly")
73
+ a_put("cases/1.#{format}").
74
+ should have_been_made
75
+ end
76
+
77
+ it "should return up to 100 cases worth of extended information" do
78
+ a_case = @client.update_case(1, :subject => "Welcome to Assistly")
79
+
80
+ a_case.id.should == 1
81
+ a_case.subject.should == "Welcome to Assistly"
82
+ end
83
+
84
+ end
85
+ end
86
+ end
87
+ end
88
+ end
@@ -0,0 +1,158 @@
1
+ require 'helper'
2
+
3
+ describe Assistly::Client do
4
+ Assistly::Configuration::VALID_FORMATS.each do |format|
5
+ context ".new(:format => '#{format}')" do
6
+ before do
7
+ @client = Assistly::Client.new(:subdomain => "example", :format => format, :consumer_key => 'CK', :consumer_secret => 'CS', :oauth_token => 'OT', :oauth_token_secret => 'OS')
8
+ end
9
+
10
+ describe ".customers" do
11
+
12
+ context "lookup" do
13
+
14
+ before do
15
+ stub_get("customers.#{format}").
16
+ to_return(:body => fixture("customers.#{format}"), :headers => {:content_type => "application/#{format}; charset=utf-8"})
17
+ end
18
+
19
+ it "should get the correct resource" do
20
+ @client.customers
21
+ a_get("customers.#{format}").
22
+ should have_been_made
23
+ end
24
+
25
+ it "should return up to 100 customers worth of extended information" do
26
+ customers = @client.customers
27
+
28
+ customers.should be_a Array
29
+ customers.first.customer.first_name.should == "Jeremy"
30
+ end
31
+
32
+ end
33
+ end
34
+
35
+ describe ".customer" do
36
+
37
+ context "lookup" do
38
+
39
+ before do
40
+ stub_get("customers/1.#{format}").
41
+ to_return(:body => fixture("customer.#{format}"), :headers => {:content_type => "application/#{format}; charset=utf-8"})
42
+ end
43
+
44
+ it "should get the correct resource" do
45
+ @client.customer(1)
46
+ a_get("customers/1.#{format}").
47
+ should have_been_made
48
+ end
49
+
50
+ it "should return up to 100 customers worth of extended information" do
51
+ customer = @client.customer(1)
52
+
53
+ customer.first_name.should == "Jeremy"
54
+ customer.addresses.first.address.city.should == "Commack"
55
+ end
56
+
57
+ end
58
+ end
59
+
60
+ describe ".create_customer" do
61
+
62
+ context "create" do
63
+
64
+ before do
65
+ stub_post("customers.#{format}").
66
+ to_return(:body => fixture("customer_create.#{format}"), :headers => {:content_type => "application/#{format}; charset=utf-8"})
67
+ end
68
+
69
+ it "should get the correct resource" do
70
+ @client.create_customer(:name => "Chris Warren", :twitter => "cdwarren")
71
+ a_post("customers.#{format}").
72
+ should have_been_made
73
+ end
74
+
75
+ it "should return the information about this user" do
76
+ customer = @client.create_customer(:name => "John Smith", :twitter => "cdwarren")
77
+
78
+ customer.first_name.should == "John"
79
+ customer.phones.first.phone.phone.should == "123-456-7890"
80
+ end
81
+
82
+ end
83
+ end
84
+
85
+ describe ".update_customer" do
86
+
87
+ context "update" do
88
+
89
+ before do
90
+ stub_put("customers/1.#{format}").
91
+ to_return(:body => fixture("customer_update.#{format}"), :headers => {:content_type => "application/#{format}; charset=utf-8"})
92
+ end
93
+
94
+ it "should get the correct resource" do
95
+ @client.update_customer(1, :name => "Chris Warren", :twitter => "cdwarren")
96
+ a_put("customers/1.#{format}").
97
+ should have_been_made
98
+ end
99
+
100
+ it "should return the information about this user" do
101
+ customer = @client.update_customer(1, :name => "Joslyn Esser")
102
+
103
+ customer.first_name.should == "Joslyn"
104
+ end
105
+
106
+ end
107
+ end
108
+
109
+ describe ".create_customer_email" do
110
+
111
+ context "create" do
112
+
113
+ before do
114
+ stub_post("customers/1/emails.#{format}").
115
+ to_return(:body => fixture("customer_create_email.#{format}"), :headers => {:content_type => "application/#{format}; charset=utf-8"})
116
+ end
117
+
118
+ it "should get the correct resource" do
119
+ @client.create_customer_email(1, :email => "foo@example.com")
120
+ a_post("customers/1/emails.#{format}").
121
+ should have_been_made
122
+ end
123
+
124
+ it "should return the information about this user" do
125
+ email = @client.create_customer_email(1, :email => "api@example.com")
126
+
127
+ email.email.should == "api@example.com"
128
+ end
129
+
130
+ end
131
+ end
132
+
133
+ describe ".update_customer_email" do
134
+
135
+ context "update" do
136
+
137
+ before do
138
+ stub_put("customers/1/emails/2.#{format}").
139
+ to_return(:body => fixture("customer_update_email.#{format}"), :headers => {:content_type => "application/#{format}; charset=utf-8"})
140
+ end
141
+
142
+ it "should get the correct resource" do
143
+ @client.update_customer_email(1, 2, :email => "foo@example.com")
144
+ a_put("customers/1/emails/2.#{format}").
145
+ should have_been_made
146
+ end
147
+
148
+ it "should return the information about this user" do
149
+ email = @client.update_customer_email(1, 2, :email => "api@example.com")
150
+
151
+ email.email.should == "api@example.com"
152
+ end
153
+
154
+ end
155
+ end
156
+ end
157
+ end
158
+ end
@@ -0,0 +1,58 @@
1
+ require 'helper'
2
+
3
+ describe Assistly::Client do
4
+ Assistly::Configuration::VALID_FORMATS.each do |format|
5
+ context ".new(:format => '#{format}')" do
6
+ before do
7
+ @client = Assistly::Client.new(:subdomain => "example", :format => format, :consumer_key => 'CK', :consumer_secret => 'CS', :oauth_token => 'OT', :oauth_token_secret => 'OS')
8
+ end
9
+
10
+ describe ".create_interaction" do
11
+ context "create a new interaction" do
12
+ before do
13
+ stub_post("interactions.#{format}").
14
+ to_return(:body => fixture("interaction_create.#{format}"), :headers => {:content_type => "application/#{format}; charset=utf-8"})
15
+ end
16
+
17
+ it "should get the correct resource" do
18
+ @client.create_interaction(:interaction_subject => "this is an api test", :customer_email => "foo@example.com")
19
+ a_post("interactions.#{format}").
20
+ should have_been_made
21
+ end
22
+
23
+ it "should create an interaction" do
24
+ interaction = @client.create_interaction(:interaction_subject => "this is an api test", :customer_email => "foo@example.com")
25
+
26
+ interaction.customer.emails.first.email.email.should == "customer@zencoder.com"
27
+ interaction.interaction.interactionable.email.subject.should == "this is an api test"
28
+ end
29
+ end
30
+ end
31
+
32
+ describe ".interactions" do
33
+
34
+ context "lookup" do
35
+
36
+ before do
37
+ stub_get("interactions.#{format}").
38
+ to_return(:body => fixture("interactions.#{format}"), :headers => {:content_type => "application/#{format}; charset=utf-8"})
39
+ end
40
+
41
+ it "should get the correct resource" do
42
+ @client.interactions
43
+ a_get("interactions.#{format}").
44
+ should have_been_made
45
+ end
46
+
47
+ it "should return up to 100 users worth of extended information" do
48
+ interactions = @client.interactions
49
+
50
+ interactions.should be_a Array
51
+ interactions.last.interaction.user.name.should == "Agent Jeremy"
52
+ end
53
+
54
+ end
55
+ end
56
+ end
57
+ end
58
+ end