sqwiggle-ruby 0.0.1 → 0.0.2
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.
- checksums.yaml +4 -4
- data/README.md +11 -2
- data/lib/sqwiggle-ruby.rb +21 -16
- data/lib/sqwiggle/api/client.rb +72 -0
- data/lib/sqwiggle/api/error_handler.rb +14 -0
- data/lib/sqwiggle/api/errors/authentication_error.rb +8 -0
- data/lib/sqwiggle/api/errors/authorization_error.rb +7 -0
- data/lib/sqwiggle/api/errors/bad_request_error.rb +8 -0
- data/lib/sqwiggle/api/errors/not_found_error.rb +7 -0
- data/lib/sqwiggle/api/errors/payment_required_error.rb +7 -0
- data/lib/sqwiggle/api/errors/server_error.rb +7 -0
- data/lib/sqwiggle/api/resource.rb +71 -0
- data/lib/sqwiggle/api/service.rb +21 -0
- data/lib/sqwiggle/api_client.rb +5 -0
- data/lib/sqwiggle/attachment.rb +10 -0
- data/lib/sqwiggle/conversation.rb +9 -0
- data/lib/sqwiggle/error.rb +17 -0
- data/lib/sqwiggle/invite.rb +5 -2
- data/lib/sqwiggle/message.rb +11 -2
- data/lib/sqwiggle/notification.rb +23 -0
- data/lib/sqwiggle/organization.rb +5 -0
- data/lib/sqwiggle/room.rb +8 -0
- data/lib/sqwiggle/user.rb +15 -2
- data/lib/sqwiggle/version.rb +1 -1
- data/spec/{client_spec.rb → api/client_spec.rb} +17 -5
- data/spec/{resource_spec.rb → api/resource_spec.rb} +3 -3
- data/spec/conversation_spec.rb +14 -0
- data/spec/errors_spec.rb +6 -6
- data/spec/invite_spec.rb +14 -0
- data/spec/message_spec.rb +129 -2
- data/spec/resource_behaviour.rb +3 -1
- data/spec/room_spec.rb +12 -0
- data/spec/sqwiggle_spec.rb +1 -1
- data/spec/user_spec.rb +15 -0
- data/sqwiggle-ruby.gemspec +2 -1
- metadata +44 -18
- data/LICENSE.txt +0 -22
- data/lib/sqwiggle/client.rb +0 -47
- data/lib/sqwiggle/error_handler.rb +0 -12
- data/lib/sqwiggle/errors/authentication_error.rb +0 -6
- data/lib/sqwiggle/errors/authorization_error.rb +0 -5
- data/lib/sqwiggle/errors/bad_request_error.rb +0 -6
- data/lib/sqwiggle/errors/not_found_error.rb +0 -5
- data/lib/sqwiggle/errors/payment_required_error.rb +0 -5
- data/lib/sqwiggle/errors/server_error.rb +0 -5
- data/lib/sqwiggle/resource.rb +0 -39
- data/lib/sqwiggle/service.rb +0 -19
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c96444e1599a78bc667b4fa719e89ba6657c5669
|
4
|
+
data.tar.gz: 3db518ea9a461c24af426565f8c84791b104c588
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6f73fb590c240d8ed98eb995539dc1caafcc54c663636dbd8f7781d94606bd6fef456b647cfea2460256dae52d0a92b5deefd7f107ae38ecb0aec12f3a97228b
|
7
|
+
data.tar.gz: 2e0464663d867a7c6c2ef9cce8d91cd940316f215485046b1946e48a39ce309e67743e641b256ebd0c7b39775a13c91d2dfc9cf19cec2af6825a646edf0de63d
|
data/README.md
CHANGED
@@ -1,4 +1,8 @@
|
|
1
|
-
# Sqwiggle
|
1
|
+
# Ruby Client for the Sqwiggle API
|
2
|
+
|
3
|
+
The API and this gem are currently in ~~BETA~~ ALPHA
|
4
|
+
|
5
|
+
Comments, pull requests, discussion and input are all welcome at this stage.
|
2
6
|
|
3
7
|

|
4
8
|
|
@@ -20,7 +24,12 @@ Or install it yourself as:
|
|
20
24
|
|
21
25
|
## Usage
|
22
26
|
|
23
|
-
|
27
|
+
Create an API Client at https://www.sqwiggle.com/company/clients
|
28
|
+
|
29
|
+
## TODO
|
30
|
+
|
31
|
+
* Compile todo list
|
32
|
+
|
24
33
|
|
25
34
|
## Contributing
|
26
35
|
|
data/lib/sqwiggle-ruby.rb
CHANGED
@@ -2,25 +2,30 @@ require 'faraday'
|
|
2
2
|
require 'json'
|
3
3
|
require 'virtus'
|
4
4
|
|
5
|
-
|
6
|
-
|
7
|
-
require "sqwiggle/error_handler"
|
8
|
-
require "sqwiggle/service"
|
9
|
-
require "sqwiggle/client"
|
10
|
-
require "sqwiggle/resource"
|
5
|
+
module Sqwiggle
|
6
|
+
module Api
|
7
|
+
require "sqwiggle/api/error_handler"
|
8
|
+
require "sqwiggle/api/service"
|
9
|
+
require "sqwiggle/api/client"
|
10
|
+
require "sqwiggle/api/resource"
|
11
11
|
|
12
|
-
require "sqwiggle/errors/authentication_error"
|
13
|
-
require "sqwiggle/errors/authorization_error"
|
14
|
-
require "sqwiggle/errors/bad_request_error"
|
15
|
-
require "sqwiggle/errors/not_found_error"
|
16
|
-
require "sqwiggle/errors/payment_required_error"
|
17
|
-
require "sqwiggle/errors/server_error"
|
12
|
+
require "sqwiggle/api/errors/authentication_error"
|
13
|
+
require "sqwiggle/api/errors/authorization_error"
|
14
|
+
require "sqwiggle/api/errors/bad_request_error"
|
15
|
+
require "sqwiggle/api/errors/not_found_error"
|
16
|
+
require "sqwiggle/api/errors/payment_required_error"
|
17
|
+
require "sqwiggle/api/errors/server_error"
|
18
18
|
|
19
|
-
require "sqwiggle/
|
20
|
-
require "sqwiggle/
|
21
|
-
require "sqwiggle/
|
19
|
+
require "sqwiggle/api_client"
|
20
|
+
require "sqwiggle/invite"
|
21
|
+
require "sqwiggle/message"
|
22
|
+
require "sqwiggle/conversation"
|
23
|
+
require "sqwiggle/user"
|
24
|
+
require "sqwiggle/room"
|
25
|
+
require "sqwiggle/notification"
|
26
|
+
require "sqwiggle/version"
|
27
|
+
end
|
22
28
|
|
23
|
-
module Sqwiggle
|
24
29
|
class << self
|
25
30
|
attr_accessor :token
|
26
31
|
end
|
@@ -0,0 +1,72 @@
|
|
1
|
+
module Sqwiggle
|
2
|
+
module Api
|
3
|
+
class Client
|
4
|
+
class NoTokenError < StandardError;end;
|
5
|
+
|
6
|
+
attr_accessor :token
|
7
|
+
attr_writer :url #for testing purposes
|
8
|
+
|
9
|
+
def initialize(token=nil)
|
10
|
+
unless @token = token || Sqwiggle.token
|
11
|
+
raise NoTokenError
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
def self.service(key, klass)
|
16
|
+
define_method key, -> {
|
17
|
+
instance_eval('Service').new instance_eval(klass), self
|
18
|
+
}
|
19
|
+
end
|
20
|
+
|
21
|
+
service :api_clients, 'ApiClient'
|
22
|
+
service :conversations, 'Conversation'
|
23
|
+
service :invites, 'Invite'
|
24
|
+
service :messages, 'Message'
|
25
|
+
service :organization, 'Organization'
|
26
|
+
service :users, 'User'
|
27
|
+
service :rooms, 'Room'
|
28
|
+
|
29
|
+
def ==(val)
|
30
|
+
self.class == val.class && val.token == token
|
31
|
+
end
|
32
|
+
|
33
|
+
def get(endpoint, params={})
|
34
|
+
connection.get endpoint, params
|
35
|
+
end
|
36
|
+
|
37
|
+
def put(endpoint, params)
|
38
|
+
connection.put endpoint, params
|
39
|
+
end
|
40
|
+
|
41
|
+
def post(endpoint, params)
|
42
|
+
connection.post endpoint, params
|
43
|
+
end
|
44
|
+
|
45
|
+
def delete(endpoint)
|
46
|
+
connection.delete endpoint
|
47
|
+
end
|
48
|
+
|
49
|
+
def inspect
|
50
|
+
#This is purely to stop huge console output when inspecting resource
|
51
|
+
#objects as they contain a reference to the loading client
|
52
|
+
"#<Sqwiggle::Api::Client # (#{object_id})>"
|
53
|
+
end
|
54
|
+
|
55
|
+
private
|
56
|
+
|
57
|
+
def url
|
58
|
+
@url || 'https://api.sqwiggle.com/'
|
59
|
+
end
|
60
|
+
|
61
|
+
def connection
|
62
|
+
@connection ||= Faraday.new(url:url) do |f|
|
63
|
+
f.request :url_encoded # form-encode POST params
|
64
|
+
f.adapter Faraday.default_adapter # make requests with Net::HTTP
|
65
|
+
f.basic_auth token, 'X'
|
66
|
+
f.use ErrorHandler
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
module Sqwiggle
|
2
|
+
module Api
|
3
|
+
class ErrorHandler < Faraday::Response::Middleware
|
4
|
+
def on_complete(env)
|
5
|
+
raise Errors::BadRequestError if env[:status] == 400
|
6
|
+
raise Errors::AuthenticationError if env[:status] == 401
|
7
|
+
raise Errors::PaymentRequiredError if env[:status] == 402
|
8
|
+
raise Errors::AuthorizationError if env[:status] == 403
|
9
|
+
raise Errors::NotFoundError if env[:status] == 404
|
10
|
+
raise Errors::ServerError if env[:status] == 500
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
@@ -0,0 +1,71 @@
|
|
1
|
+
module Sqwiggle
|
2
|
+
module Api
|
3
|
+
class Resource
|
4
|
+
|
5
|
+
include Virtus.model
|
6
|
+
|
7
|
+
attribute :client, Client, default: :default_client
|
8
|
+
attribute :id, Integer
|
9
|
+
|
10
|
+
def default_client
|
11
|
+
Client.new
|
12
|
+
end
|
13
|
+
|
14
|
+
class << self
|
15
|
+
|
16
|
+
attr_accessor :endpoint
|
17
|
+
|
18
|
+
def all(client=Client.new)
|
19
|
+
[].tap do |result|
|
20
|
+
JSON.parse(client.get(self.endpoint).body).each do |item|
|
21
|
+
result << self.new(item.merge client:client)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
def find(id, client=Client.new)
|
27
|
+
attrs = JSON.parse client.get("#{endpoint}/#{id}").body
|
28
|
+
attrs.merge! client:client
|
29
|
+
self.new attrs
|
30
|
+
end
|
31
|
+
|
32
|
+
def create(params, client=Client.new)
|
33
|
+
attrs = JSON.parse(client.post("#{endpoint}", params).body)
|
34
|
+
attrs.merge! client:client
|
35
|
+
self.new attrs
|
36
|
+
end
|
37
|
+
|
38
|
+
def endpoint
|
39
|
+
@endpoint || default_endpoint
|
40
|
+
end
|
41
|
+
|
42
|
+
def default_endpoint
|
43
|
+
"/#{self.to_s.downcase.split('::').last}s"
|
44
|
+
end
|
45
|
+
|
46
|
+
end
|
47
|
+
|
48
|
+
def update(params)
|
49
|
+
res = client.put("#{self.class.endpoint}/#{id}", params)
|
50
|
+
attrs = JSON.parse(res.body, :symbolize_names => true)
|
51
|
+
self.attributes = attrs
|
52
|
+
self
|
53
|
+
end
|
54
|
+
|
55
|
+
def delete
|
56
|
+
res = client.delete("#{self.class.endpoint}/#{id}")
|
57
|
+
res.status == 204
|
58
|
+
end
|
59
|
+
|
60
|
+
def save
|
61
|
+
if(id != nil)
|
62
|
+
update(self.attributes)
|
63
|
+
else
|
64
|
+
self.attributes = self.class.create(attributes, client).attributes
|
65
|
+
end
|
66
|
+
true
|
67
|
+
end
|
68
|
+
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
module Sqwiggle
|
2
|
+
module Api
|
3
|
+
class Service
|
4
|
+
|
5
|
+
attr_reader :klass, :client
|
6
|
+
|
7
|
+
def initialize(klass, client)
|
8
|
+
@klass, @client = klass, client
|
9
|
+
end
|
10
|
+
|
11
|
+
def all
|
12
|
+
klass.all client
|
13
|
+
end
|
14
|
+
|
15
|
+
def find(id)
|
16
|
+
klass.find id, client
|
17
|
+
end
|
18
|
+
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
module Sqwiggle
|
2
|
+
class Error < StandardError
|
3
|
+
|
4
|
+
attr_reader :status
|
5
|
+
attr_reader :error_type
|
6
|
+
attr_reader :param
|
7
|
+
|
8
|
+
def initialize(response)
|
9
|
+
h = JSON.parse(response.body)
|
10
|
+
@status = response.status
|
11
|
+
@type = h['type']
|
12
|
+
@param = h['param']
|
13
|
+
super(message)
|
14
|
+
end
|
15
|
+
|
16
|
+
end
|
17
|
+
end
|
data/lib/sqwiggle/invite.rb
CHANGED
data/lib/sqwiggle/message.rb
CHANGED
@@ -1,12 +1,21 @@
|
|
1
1
|
module Sqwiggle
|
2
|
-
class Message < Resource
|
2
|
+
class Message < Api::Resource
|
3
3
|
|
4
4
|
attribute :text, String
|
5
5
|
attribute :type, String
|
6
6
|
attribute :author, Hash
|
7
|
+
attribute :room_id, Integer
|
8
|
+
attribute :attachments, Array
|
9
|
+
attribute :mentions, Array
|
10
|
+
attribute :updated_at, DateTime
|
11
|
+
attribute :created_at, DateTime
|
7
12
|
|
8
13
|
def author
|
9
|
-
|
14
|
+
if @author[:type] == 'user'
|
15
|
+
client.users.find(@author[:id])
|
16
|
+
elsif @author[:type] == 'client'
|
17
|
+
client.api_clients.find(@author[:id])
|
18
|
+
end
|
10
19
|
end
|
11
20
|
|
12
21
|
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
module Sqwiggle
|
2
|
+
class Notification < Api::Resource
|
3
|
+
attribute :login, String
|
4
|
+
attribute :logout, String
|
5
|
+
attribute :workroom_enter, String
|
6
|
+
attribute :workroom_leave, String
|
7
|
+
attribute :conversation_enter, String
|
8
|
+
attribute :conversation_leave, String
|
9
|
+
attribute :stream_item, String
|
10
|
+
attribute :stream_item_in_conversation, String
|
11
|
+
attribute :mention, String
|
12
|
+
attribute :sqwiggle_news_updates, String
|
13
|
+
attribute :sqwiggle_tips, String
|
14
|
+
attribute :onboard_invite, String
|
15
|
+
attribute :onboard_busy, String
|
16
|
+
attribute :onboard_conversation_start, String
|
17
|
+
attribute :onboard_conversation_leave, String
|
18
|
+
attribute :onboard_mentions, String
|
19
|
+
attribute :onboard_notifications, String
|
20
|
+
attribute :onboard_chrome_access, String
|
21
|
+
attribute :onboard_invited, String
|
22
|
+
end
|
23
|
+
end
|
data/lib/sqwiggle/user.rb
CHANGED
@@ -1,6 +1,19 @@
|
|
1
1
|
module Sqwiggle
|
2
|
-
class User < Resource
|
2
|
+
class User < Api::Resource
|
3
3
|
self.endpoint = '/users'
|
4
|
-
attribute :name
|
4
|
+
attribute :name, String
|
5
|
+
attribute :role, String
|
6
|
+
attribute :media, Hash
|
7
|
+
attribute :status, String
|
8
|
+
attribute :message, String
|
9
|
+
attribute :email, String
|
10
|
+
attribute :confirmed, Boolean
|
11
|
+
attribute :time_zone, String
|
12
|
+
attribute :time_zone_offset, Float
|
13
|
+
attribute :created_at, DateTime
|
14
|
+
attribute :last_connected_at, DateTime
|
15
|
+
attribute :last_active_at, DateTime
|
16
|
+
attribute :last_still, String
|
17
|
+
attribute :avatar, String
|
5
18
|
end
|
6
19
|
end
|
data/lib/sqwiggle/version.rb
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
|
-
describe Sqwiggle::Client do
|
3
|
+
describe Sqwiggle::Api::Client do
|
4
4
|
|
5
5
|
|
6
6
|
describe ".new" do
|
@@ -12,23 +12,35 @@ describe Sqwiggle::Client do
|
|
12
12
|
Sqwiggle.token = 'global_token'
|
13
13
|
end
|
14
14
|
context "without specifying a token in the initializer" do
|
15
|
-
subject { Sqwiggle::Client.new }
|
15
|
+
subject { Sqwiggle::Api::Client.new }
|
16
16
|
its(:token) { should be == 'global_token' }
|
17
17
|
end
|
18
18
|
context "with specifying a token in the initializer" do
|
19
|
-
subject { Sqwiggle::Client.new 'local_token' }
|
19
|
+
subject { Sqwiggle::Api::Client.new 'local_token' }
|
20
20
|
its(:token) { should be == 'local_token' }
|
21
21
|
end
|
22
22
|
end
|
23
23
|
context "without a globally specified token" do
|
24
24
|
it "Raises an error" do
|
25
|
-
expect { Sqwiggle::Client.new }.to raise_error(Sqwiggle::Client::NoTokenError)
|
25
|
+
expect { Sqwiggle::Api::Client.new }.to raise_error(Sqwiggle::Api::Client::NoTokenError)
|
26
26
|
end
|
27
27
|
end
|
28
28
|
end
|
29
29
|
|
30
|
+
describe 'services' do
|
31
|
+
before do
|
32
|
+
Sqwiggle.token = '12345'
|
33
|
+
end
|
34
|
+
it { should respond_to :messages }
|
35
|
+
it { should respond_to :users }
|
36
|
+
it { should respond_to :conversations }
|
37
|
+
it { should respond_to :rooms }
|
38
|
+
it { should respond_to :invites }
|
39
|
+
it { should respond_to :api_clients }
|
40
|
+
end
|
41
|
+
|
30
42
|
describe "#get" do
|
31
|
-
subject { Sqwiggle::Client.new 'local_token' }
|
43
|
+
subject { Sqwiggle::Api::Client.new 'local_token' }
|
32
44
|
let(:endpoint) { '/users/1' }
|
33
45
|
|
34
46
|
it "delegates to Faraday" do
|
@@ -1,6 +1,6 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
|
-
describe Sqwiggle::Resource do
|
3
|
+
describe Sqwiggle::Api::Resource do
|
4
4
|
|
5
5
|
let(:client) { double('mock_client') }
|
6
6
|
let(:response) { double('faraday_response') }
|
@@ -14,7 +14,7 @@ describe Sqwiggle::Resource do
|
|
14
14
|
it "calls the index endpoint" do
|
15
15
|
response.stub(:body).and_return("[{\"a\":1,\"b\":2}]")
|
16
16
|
client.should_receive(:get).with('/resources')
|
17
|
-
Sqwiggle::Resource.all(client)
|
17
|
+
Sqwiggle::Api::Resource.all(client)
|
18
18
|
end
|
19
19
|
end
|
20
20
|
|
@@ -22,7 +22,7 @@ describe Sqwiggle::Resource do
|
|
22
22
|
it "calls the index endpoint" do
|
23
23
|
response.stub(:body).and_return("{\"a\":1,\"b\":2}")
|
24
24
|
client.should_receive(:get).with('/resources/1')
|
25
|
-
Sqwiggle::Resource.find(1, client)
|
25
|
+
Sqwiggle::Api::Resource.find(1, client)
|
26
26
|
end
|
27
27
|
end
|
28
28
|
|
@@ -0,0 +1,14 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
describe Sqwiggle::Conversation do
|
4
|
+
it_behaves_like "a resource"
|
5
|
+
subject { described_class.new }
|
6
|
+
it { should respond_to :participating }
|
7
|
+
it { should respond_to :participated }
|
8
|
+
it { should respond_to :status }
|
9
|
+
it { should respond_to :room_id }
|
10
|
+
it { should respond_to :created_at }
|
11
|
+
end
|
12
|
+
|
13
|
+
|
14
|
+
|
data/spec/errors_spec.rb
CHANGED
@@ -14,7 +14,7 @@ describe "api error handling" do
|
|
14
14
|
end
|
15
15
|
|
16
16
|
it "raises an exception" do
|
17
|
-
expect{Sqwiggle::Message.all}.to raise_error(Sqwiggle::Errors::BadRequestError)
|
17
|
+
expect{Sqwiggle::Message.all}.to raise_error(Sqwiggle::Api::Errors::BadRequestError)
|
18
18
|
end
|
19
19
|
|
20
20
|
end
|
@@ -31,7 +31,7 @@ describe "api error handling" do
|
|
31
31
|
end
|
32
32
|
|
33
33
|
it "raises an exception" do
|
34
|
-
expect{Sqwiggle::Message.all}.to raise_error(Sqwiggle::Errors::AuthenticationError)
|
34
|
+
expect{Sqwiggle::Message.all}.to raise_error(Sqwiggle::Api::Errors::AuthenticationError)
|
35
35
|
end
|
36
36
|
end
|
37
37
|
|
@@ -47,7 +47,7 @@ describe "api error handling" do
|
|
47
47
|
end
|
48
48
|
|
49
49
|
it "raises an exception" do
|
50
|
-
expect{Sqwiggle::Message.all}.to raise_error(Sqwiggle::Errors::PaymentRequiredError)
|
50
|
+
expect{Sqwiggle::Message.all}.to raise_error(Sqwiggle::Api::Errors::PaymentRequiredError)
|
51
51
|
end
|
52
52
|
end
|
53
53
|
|
@@ -63,7 +63,7 @@ describe "api error handling" do
|
|
63
63
|
end
|
64
64
|
|
65
65
|
it "raises an exception" do
|
66
|
-
expect{Sqwiggle::Message.all}.to raise_error(Sqwiggle::Errors::AuthorizationError)
|
66
|
+
expect{Sqwiggle::Message.all}.to raise_error(Sqwiggle::Api::Errors::AuthorizationError)
|
67
67
|
end
|
68
68
|
end
|
69
69
|
|
@@ -79,7 +79,7 @@ describe "api error handling" do
|
|
79
79
|
end
|
80
80
|
|
81
81
|
it "raises an exception" do
|
82
|
-
expect{Sqwiggle::Message.all}.to raise_error(Sqwiggle::Errors::NotFoundError)
|
82
|
+
expect{Sqwiggle::Message.all}.to raise_error(Sqwiggle::Api::Errors::NotFoundError)
|
83
83
|
end
|
84
84
|
end
|
85
85
|
|
@@ -95,7 +95,7 @@ describe "api error handling" do
|
|
95
95
|
end
|
96
96
|
|
97
97
|
it "raises an exception" do
|
98
|
-
expect{Sqwiggle::Message.all}.to raise_error(Sqwiggle::Errors::ServerError)
|
98
|
+
expect{Sqwiggle::Message.all}.to raise_error(Sqwiggle::Api::Errors::ServerError)
|
99
99
|
end
|
100
100
|
end
|
101
101
|
|
data/spec/invite_spec.rb
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
describe Sqwiggle::Invite do
|
4
|
+
it_behaves_like "a resource"
|
5
|
+
subject { described_class.new }
|
6
|
+
it { should respond_to :from_id }
|
7
|
+
it { should respond_to :email }
|
8
|
+
it { should respond_to :avatar }
|
9
|
+
it { should respond_to :url }
|
10
|
+
it { should respond_to :created_at }
|
11
|
+
end
|
12
|
+
|
13
|
+
|
14
|
+
|
data/spec/message_spec.rb
CHANGED
@@ -2,10 +2,33 @@ require 'spec_helper'
|
|
2
2
|
|
3
3
|
describe Sqwiggle::Message do
|
4
4
|
|
5
|
+
before do
|
6
|
+
Sqwiggle.token = 'token'
|
7
|
+
end
|
8
|
+
|
5
9
|
it_behaves_like "a resource"
|
6
10
|
|
7
|
-
|
8
|
-
|
11
|
+
describe "attributes" do
|
12
|
+
subject { described_class.new }
|
13
|
+
it { should respond_to :room_id }
|
14
|
+
it { should respond_to :text }
|
15
|
+
it { should respond_to :author }
|
16
|
+
it { should respond_to :attachments }
|
17
|
+
it { should respond_to :mentions }
|
18
|
+
it { should respond_to :created_at }
|
19
|
+
it { should respond_to :updated_at }
|
20
|
+
end
|
21
|
+
|
22
|
+
subject do
|
23
|
+
Sqwiggle::Message.new({
|
24
|
+
id:1,
|
25
|
+
text:'dutch coffee',
|
26
|
+
author: {
|
27
|
+
id:1,
|
28
|
+
type:'user',
|
29
|
+
name:'Some Name'
|
30
|
+
}
|
31
|
+
})
|
9
32
|
end
|
10
33
|
|
11
34
|
describe "initialization" do
|
@@ -19,4 +42,108 @@ describe Sqwiggle::Message do
|
|
19
42
|
m.text.should be == 'some text'
|
20
43
|
end
|
21
44
|
end
|
45
|
+
|
46
|
+
describe "#author" do
|
47
|
+
context "with a user" do
|
48
|
+
before do
|
49
|
+
stub_request(:get, "https://token:X@api.sqwiggle.com/users/1").to_return({
|
50
|
+
:body => "{\"id\":1, \"name\":\"Some Name\"}"
|
51
|
+
})
|
52
|
+
end
|
53
|
+
|
54
|
+
specify { subject.author.should be_kind_of(Sqwiggle::User) }
|
55
|
+
specify { subject.author.id.should == 1 }
|
56
|
+
end
|
57
|
+
|
58
|
+
context "with an api client" do
|
59
|
+
subject do
|
60
|
+
Sqwiggle::Message.new({
|
61
|
+
text:'dutch coffee',
|
62
|
+
author: {
|
63
|
+
id:1,
|
64
|
+
type:'client',
|
65
|
+
name:'Github'
|
66
|
+
}
|
67
|
+
})
|
68
|
+
end
|
69
|
+
it "should return an api client"
|
70
|
+
# specify { subject.author.should be_kind_of(Sqwiggle::ApiClient) }
|
71
|
+
# specify { subject.author.id.should == 1 }
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
describe "#update" do
|
76
|
+
before do
|
77
|
+
stub_request(:put, "https://token:X@api.sqwiggle.com/messages/1").with({
|
78
|
+
:body => { :text => 'New Name' }
|
79
|
+
}).to_return({
|
80
|
+
:body => "{\"id\":1, \"text\":\"New Name\"}"
|
81
|
+
})
|
82
|
+
end
|
83
|
+
|
84
|
+
it "updates the record" do
|
85
|
+
subject.update :text => 'New Name'
|
86
|
+
subject.text.should be == 'New Name'
|
87
|
+
end
|
88
|
+
|
89
|
+
end
|
90
|
+
|
91
|
+
describe ".create" do
|
92
|
+
before do
|
93
|
+
stub_request(:post, "https://token:X@api.sqwiggle.com/messages").with({
|
94
|
+
:body => { :text => 'The Name' }
|
95
|
+
}).to_return({
|
96
|
+
:body => "{\"id\":1, \"text\":\"The Name\"}"
|
97
|
+
})
|
98
|
+
end
|
99
|
+
|
100
|
+
it "creates the message" do
|
101
|
+
m = Sqwiggle::Message.create :text =>'The Name'
|
102
|
+
m.class.should be == Sqwiggle::Message
|
103
|
+
m.text.should be == "The Name"
|
104
|
+
end
|
105
|
+
end
|
106
|
+
|
107
|
+
describe "#save" do
|
108
|
+
context "with a new record" do
|
109
|
+
before do
|
110
|
+
stub_request(:post, "https://token:X@api.sqwiggle.com/messages").to_return({
|
111
|
+
:body => "{\"id\":1, \"text\":\"The Name\"}"
|
112
|
+
})
|
113
|
+
end
|
114
|
+
subject { Sqwiggle::Message.new(text:'The Name') }
|
115
|
+
it "saves the new record" do
|
116
|
+
subject.save.should be == true
|
117
|
+
subject.id.should be == 1
|
118
|
+
assert_requested :post, "https://token:X@api.sqwiggle.com/messages"
|
119
|
+
end
|
120
|
+
end
|
121
|
+
|
122
|
+
context "with an existing record" do
|
123
|
+
before do
|
124
|
+
stub_request(:put, "https://token:X@api.sqwiggle.com/messages/1").to_return({
|
125
|
+
:body => "{\"id\":1, \"text\":\"The Name\"}"
|
126
|
+
})
|
127
|
+
end
|
128
|
+
subject { Sqwiggle::Message.new(id:1, text:'The Name') }
|
129
|
+
it "saves the new record" do
|
130
|
+
subject.save.should be == true
|
131
|
+
assert_requested :put, "https://token:X@api.sqwiggle.com/messages/1"
|
132
|
+
end
|
133
|
+
end
|
134
|
+
end
|
135
|
+
|
136
|
+
describe "#delete" do
|
137
|
+
before do
|
138
|
+
stub_request(:post, "https://token:X@api.sqwiggle.com/messages").to_return({
|
139
|
+
:body => "{\"id\":1, \"text\":\"The Name\"}"
|
140
|
+
})
|
141
|
+
subject { Sqwiggle::Message.new(id:1, text:'The Name') }
|
142
|
+
it "saves the new record" do
|
143
|
+
subject.delete.should be == true
|
144
|
+
assert_requested :delete, "https://token:X@api.sqwiggle.com/messages/1"
|
145
|
+
end
|
146
|
+
end
|
147
|
+
end
|
148
|
+
|
22
149
|
end
|
data/spec/resource_behaviour.rb
CHANGED
@@ -1,6 +1,8 @@
|
|
1
1
|
shared_examples_for "a resource" do
|
2
2
|
subject { described_class.new }
|
3
3
|
specify { subject.should respond_to :id }
|
4
|
-
specify { subject.client.should be == Sqwiggle::Client.new }
|
4
|
+
specify { subject.client.should be == Sqwiggle::Api::Client.new }
|
5
|
+
specify { described_class.should respond_to :find }
|
6
|
+
specify { described_class.should respond_to :create }
|
5
7
|
end
|
6
8
|
|
data/spec/room_spec.rb
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
describe Sqwiggle::Room do
|
4
|
+
it_behaves_like "a resource"
|
5
|
+
subject { described_class.new }
|
6
|
+
it { should respond_to :path }
|
7
|
+
it { should respond_to :name }
|
8
|
+
it { should respond_to :user_count }
|
9
|
+
it { should respond_to :created_at }
|
10
|
+
end
|
11
|
+
|
12
|
+
|
data/spec/sqwiggle_spec.rb
CHANGED
data/spec/user_spec.rb
CHANGED
@@ -2,5 +2,20 @@ require "spec_helper"
|
|
2
2
|
|
3
3
|
describe Sqwiggle::User do
|
4
4
|
it_behaves_like "a resource"
|
5
|
+
subject { described_class.new }
|
6
|
+
it { should respond_to :name }
|
7
|
+
it { should respond_to :role }
|
8
|
+
it { should respond_to :media }
|
9
|
+
it { should respond_to :status }
|
10
|
+
it { should respond_to :message }
|
11
|
+
it { should respond_to :email }
|
12
|
+
it { should respond_to :confirmed }
|
13
|
+
it { should respond_to :time_zone }
|
14
|
+
it { should respond_to :time_zone_offset }
|
15
|
+
it { should respond_to :created_at }
|
16
|
+
it { should respond_to :last_connected_at }
|
17
|
+
it { should respond_to :last_active_at }
|
18
|
+
it { should respond_to :last_still }
|
19
|
+
it { should respond_to :avatar }
|
5
20
|
end
|
6
21
|
|
data/sqwiggle-ruby.gemspec
CHANGED
@@ -10,7 +10,7 @@ Gem::Specification.new do |spec|
|
|
10
10
|
spec.email = ["luke@sqwiggle.com"]
|
11
11
|
spec.description = %q{Ruby wrapper for the sqwiggle API}
|
12
12
|
spec.summary = %q{Ruby wrapper for the sqwiggle API}
|
13
|
-
spec.homepage = ""
|
13
|
+
spec.homepage = "http://github.com/sqwiggle/sqwiggle-ruby"
|
14
14
|
spec.license = "MIT"
|
15
15
|
|
16
16
|
spec.files = `git ls-files`.split($/)
|
@@ -23,4 +23,5 @@ Gem::Specification.new do |spec|
|
|
23
23
|
|
24
24
|
spec.add_development_dependency "bundler", "~> 1.3"
|
25
25
|
spec.add_development_dependency "rake"
|
26
|
+
spec.add_development_dependency "pry"
|
26
27
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: sqwiggle-ruby
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Luke Roberts
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2014-01-24 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: faraday
|
@@ -66,6 +66,20 @@ dependencies:
|
|
66
66
|
- - '>='
|
67
67
|
- !ruby/object:Gem::Version
|
68
68
|
version: '0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: pry
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - '>='
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - '>='
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
69
83
|
description: Ruby wrapper for the sqwiggle API
|
70
84
|
email:
|
71
85
|
- luke@sqwiggle.com
|
@@ -78,34 +92,43 @@ files:
|
|
78
92
|
- .travis.yml
|
79
93
|
- Gemfile
|
80
94
|
- LICENSE
|
81
|
-
- LICENSE.txt
|
82
95
|
- README.md
|
83
96
|
- Rakefile
|
84
97
|
- lib/sqwiggle-ruby.rb
|
85
|
-
- lib/sqwiggle/client.rb
|
86
|
-
- lib/sqwiggle/error_handler.rb
|
87
|
-
- lib/sqwiggle/errors/authentication_error.rb
|
88
|
-
- lib/sqwiggle/errors/authorization_error.rb
|
89
|
-
- lib/sqwiggle/errors/bad_request_error.rb
|
90
|
-
- lib/sqwiggle/errors/not_found_error.rb
|
91
|
-
- lib/sqwiggle/errors/payment_required_error.rb
|
92
|
-
- lib/sqwiggle/errors/server_error.rb
|
98
|
+
- lib/sqwiggle/api/client.rb
|
99
|
+
- lib/sqwiggle/api/error_handler.rb
|
100
|
+
- lib/sqwiggle/api/errors/authentication_error.rb
|
101
|
+
- lib/sqwiggle/api/errors/authorization_error.rb
|
102
|
+
- lib/sqwiggle/api/errors/bad_request_error.rb
|
103
|
+
- lib/sqwiggle/api/errors/not_found_error.rb
|
104
|
+
- lib/sqwiggle/api/errors/payment_required_error.rb
|
105
|
+
- lib/sqwiggle/api/errors/server_error.rb
|
106
|
+
- lib/sqwiggle/api/resource.rb
|
107
|
+
- lib/sqwiggle/api/service.rb
|
108
|
+
- lib/sqwiggle/api_client.rb
|
109
|
+
- lib/sqwiggle/attachment.rb
|
110
|
+
- lib/sqwiggle/conversation.rb
|
111
|
+
- lib/sqwiggle/error.rb
|
93
112
|
- lib/sqwiggle/invite.rb
|
94
113
|
- lib/sqwiggle/message.rb
|
95
|
-
- lib/sqwiggle/
|
96
|
-
- lib/sqwiggle/
|
114
|
+
- lib/sqwiggle/notification.rb
|
115
|
+
- lib/sqwiggle/organization.rb
|
116
|
+
- lib/sqwiggle/room.rb
|
97
117
|
- lib/sqwiggle/user.rb
|
98
118
|
- lib/sqwiggle/version.rb
|
99
|
-
- spec/client_spec.rb
|
119
|
+
- spec/api/client_spec.rb
|
120
|
+
- spec/api/resource_spec.rb
|
121
|
+
- spec/conversation_spec.rb
|
100
122
|
- spec/errors_spec.rb
|
123
|
+
- spec/invite_spec.rb
|
101
124
|
- spec/message_spec.rb
|
102
125
|
- spec/resource_behaviour.rb
|
103
|
-
- spec/
|
126
|
+
- spec/room_spec.rb
|
104
127
|
- spec/spec_helper.rb
|
105
128
|
- spec/sqwiggle_spec.rb
|
106
129
|
- spec/user_spec.rb
|
107
130
|
- sqwiggle-ruby.gemspec
|
108
|
-
homepage:
|
131
|
+
homepage: http://github.com/sqwiggle/sqwiggle-ruby
|
109
132
|
licenses:
|
110
133
|
- MIT
|
111
134
|
metadata: {}
|
@@ -130,11 +153,14 @@ signing_key:
|
|
130
153
|
specification_version: 4
|
131
154
|
summary: Ruby wrapper for the sqwiggle API
|
132
155
|
test_files:
|
133
|
-
- spec/client_spec.rb
|
156
|
+
- spec/api/client_spec.rb
|
157
|
+
- spec/api/resource_spec.rb
|
158
|
+
- spec/conversation_spec.rb
|
134
159
|
- spec/errors_spec.rb
|
160
|
+
- spec/invite_spec.rb
|
135
161
|
- spec/message_spec.rb
|
136
162
|
- spec/resource_behaviour.rb
|
137
|
-
- spec/
|
163
|
+
- spec/room_spec.rb
|
138
164
|
- spec/spec_helper.rb
|
139
165
|
- spec/sqwiggle_spec.rb
|
140
166
|
- spec/user_spec.rb
|
data/LICENSE.txt
DELETED
@@ -1,22 +0,0 @@
|
|
1
|
-
Copyright (c) 2013 Luke Roberts
|
2
|
-
|
3
|
-
MIT License
|
4
|
-
|
5
|
-
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
-
a copy of this software and associated documentation files (the
|
7
|
-
"Software"), to deal in the Software without restriction, including
|
8
|
-
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
-
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
-
permit persons to whom the Software is furnished to do so, subject to
|
11
|
-
the following conditions:
|
12
|
-
|
13
|
-
The above copyright notice and this permission notice shall be
|
14
|
-
included in all copies or substantial portions of the Software.
|
15
|
-
|
16
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
-
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
-
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
-
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
-
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
-
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
-
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/lib/sqwiggle/client.rb
DELETED
@@ -1,47 +0,0 @@
|
|
1
|
-
module Sqwiggle
|
2
|
-
class Client
|
3
|
-
class NoTokenError < StandardError;end;
|
4
|
-
|
5
|
-
attr_accessor :token
|
6
|
-
attr_writer :url #for testing purposes
|
7
|
-
|
8
|
-
def initialize(token=nil)
|
9
|
-
unless @token = token || Sqwiggle.token
|
10
|
-
raise NoTokenError
|
11
|
-
end
|
12
|
-
end
|
13
|
-
|
14
|
-
def ==(val)
|
15
|
-
self.class == val.class && val.token == token
|
16
|
-
end
|
17
|
-
|
18
|
-
def get(endpoint, params={})
|
19
|
-
connection.get endpoint, params
|
20
|
-
end
|
21
|
-
|
22
|
-
def messages
|
23
|
-
Service.new Message, self
|
24
|
-
end
|
25
|
-
|
26
|
-
def users
|
27
|
-
Service.new User, self
|
28
|
-
end
|
29
|
-
|
30
|
-
private
|
31
|
-
|
32
|
-
def url
|
33
|
-
@url || 'https://api.sqwiggle.com/'
|
34
|
-
end
|
35
|
-
|
36
|
-
def connection
|
37
|
-
@connection ||= Faraday.new(url:url) do |f|
|
38
|
-
f.request :url_encoded # form-encode POST params
|
39
|
-
f.response :logger # log requests to STDOUT
|
40
|
-
f.adapter Faraday.default_adapter # make requests with Net::HTTP
|
41
|
-
f.basic_auth token, 'X'
|
42
|
-
f.use ErrorHandler
|
43
|
-
end
|
44
|
-
end
|
45
|
-
|
46
|
-
end
|
47
|
-
end
|
@@ -1,12 +0,0 @@
|
|
1
|
-
module Sqwiggle
|
2
|
-
class ErrorHandler < Faraday::Response::Middleware
|
3
|
-
def on_complete(env)
|
4
|
-
raise Errors::BadRequestError if env[:status] == 400
|
5
|
-
raise Errors::AuthenticationError if env[:status] == 401
|
6
|
-
raise Errors::PaymentRequiredError if env[:status] == 402
|
7
|
-
raise Errors::AuthorizationError if env[:status] == 403
|
8
|
-
raise Errors::NotFoundError if env[:status] == 404
|
9
|
-
raise Errors::ServerError if env[:status] == 500
|
10
|
-
end
|
11
|
-
end
|
12
|
-
end
|
data/lib/sqwiggle/resource.rb
DELETED
@@ -1,39 +0,0 @@
|
|
1
|
-
module Sqwiggle
|
2
|
-
class Resource
|
3
|
-
|
4
|
-
include Virtus.model
|
5
|
-
|
6
|
-
attribute :client, Client, default: :default_client
|
7
|
-
attribute :id, Integer
|
8
|
-
|
9
|
-
def default_client
|
10
|
-
Client.new
|
11
|
-
end
|
12
|
-
|
13
|
-
class << self
|
14
|
-
|
15
|
-
attr_accessor :endpoint
|
16
|
-
|
17
|
-
def all(client=Client.new)
|
18
|
-
[].tap do |result|
|
19
|
-
JSON.parse(client.get(self.endpoint).body).each do |item|
|
20
|
-
result << self.new(item)
|
21
|
-
end
|
22
|
-
end
|
23
|
-
end
|
24
|
-
|
25
|
-
def find(id, client=Client.new)
|
26
|
-
self.new(JSON.parse(client.get("#{endpoint}/#{id}").body))
|
27
|
-
end
|
28
|
-
|
29
|
-
def endpoint
|
30
|
-
@endpoint || default_endpoint
|
31
|
-
end
|
32
|
-
|
33
|
-
def default_endpoint
|
34
|
-
"/#{self.to_s.downcase.split('::').last}s"
|
35
|
-
end
|
36
|
-
|
37
|
-
end
|
38
|
-
end
|
39
|
-
end
|
data/lib/sqwiggle/service.rb
DELETED
@@ -1,19 +0,0 @@
|
|
1
|
-
module Sqwiggle
|
2
|
-
class Service
|
3
|
-
|
4
|
-
attr_reader :klass, :client
|
5
|
-
|
6
|
-
def initialize(klass, client)
|
7
|
-
@klass, @client = klass, client
|
8
|
-
end
|
9
|
-
|
10
|
-
def all
|
11
|
-
klass.all client
|
12
|
-
end
|
13
|
-
|
14
|
-
def find(id)
|
15
|
-
klass.find id, client
|
16
|
-
end
|
17
|
-
|
18
|
-
end
|
19
|
-
end
|