quill-api-client 0.0.2 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/quill/activity_model.rb +3 -18
- data/lib/quill/activity_session.rb +4 -4
- data/lib/quill/base_model.rb +15 -12
- data/lib/quill/client.rb +65 -42
- data/lib/quill/configuration.rb +33 -0
- data/lib/quill/endpoint_definitions.rb +13 -0
- data/lib/quill/oauth.rb +11 -0
- data/lib/quill.rb +11 -4
- metadata +17 -3
data/lib/quill/activity_model.rb
CHANGED
@@ -3,28 +3,13 @@ class Quill::ActivityModel < Quill::BaseModel
|
|
3
3
|
|
4
4
|
special_attrs :name, :description
|
5
5
|
|
6
|
-
def self.key_attributes
|
7
|
-
[:_cid, :_uid]
|
8
|
-
end
|
9
|
-
|
10
|
-
def filter_params params
|
11
|
-
params[:cid] = _cid
|
12
|
-
params[:data].delete(:_cid)
|
13
|
-
params[:data].delete(:_uid)
|
14
|
-
params
|
15
|
-
end
|
16
|
-
|
17
|
-
def key_present?
|
18
|
-
_cid.present? && _uid.present?
|
19
|
-
end
|
20
|
-
|
21
6
|
def find
|
22
|
-
api.activities.find(
|
7
|
+
api.activities.find(id)
|
23
8
|
end
|
24
9
|
|
25
10
|
def persist_params params
|
26
|
-
if
|
27
|
-
api.activities.update(
|
11
|
+
if id.present?
|
12
|
+
api.activities.update(id, params)
|
28
13
|
else
|
29
14
|
api.activities.create(params)
|
30
15
|
end
|
@@ -2,18 +2,18 @@ class Quill::ActivitySession < Quill::BaseModel
|
|
2
2
|
special_attrs :percentage, :time_spent, :state, :completed_at, :activity_uid, :anonymous
|
3
3
|
|
4
4
|
def find
|
5
|
-
if anonymous &&
|
5
|
+
if anonymous && id.blank?
|
6
6
|
persist
|
7
7
|
else
|
8
|
-
api.activity_sessions.find(
|
8
|
+
api.activity_sessions.find(id)
|
9
9
|
end
|
10
10
|
end
|
11
11
|
|
12
|
-
def activity
|
12
|
+
def activity
|
13
13
|
raise if activity_uid.blank?
|
14
14
|
return nil if activity_uid.blank?
|
15
15
|
return @activity if @activity.present?
|
16
|
-
@activity = Story.new(
|
16
|
+
@activity = Story.new(id: activity_uid, access_token: access_token)
|
17
17
|
end
|
18
18
|
|
19
19
|
def key_present?
|
data/lib/quill/base_model.rb
CHANGED
@@ -1,5 +1,6 @@
|
|
1
1
|
class Quill::BaseModel
|
2
2
|
include ActiveModel::Model
|
3
|
+
attr_accessor :id, :access_token
|
3
4
|
|
4
5
|
def self.special_attrs *attrs
|
5
6
|
return @special_attrs unless attrs.any?
|
@@ -14,7 +15,7 @@ class Quill::BaseModel
|
|
14
15
|
return @attributes if defined?(@attributes)
|
15
16
|
@attributes = attrs
|
16
17
|
|
17
|
-
|
18
|
+
attrs.dup.unshift(*@special_attrs).each do |attr|
|
18
19
|
class_eval <<-CODE
|
19
20
|
def #{attr}
|
20
21
|
@data[:#{attr}]
|
@@ -27,10 +28,6 @@ class Quill::BaseModel
|
|
27
28
|
end
|
28
29
|
end
|
29
30
|
|
30
|
-
def self.key_attributes
|
31
|
-
[:_uid]
|
32
|
-
end
|
33
|
-
|
34
31
|
def initialize *args
|
35
32
|
@data = {}
|
36
33
|
super
|
@@ -42,7 +39,7 @@ class Quill::BaseModel
|
|
42
39
|
end
|
43
40
|
|
44
41
|
def persist
|
45
|
-
data = @data.except(*self.class.special_attrs.dup
|
42
|
+
data = @data.except(*self.class.special_attrs.dup)
|
46
43
|
serialized_data = {}
|
47
44
|
|
48
45
|
data.each do |key, value|
|
@@ -56,7 +53,9 @@ class Quill::BaseModel
|
|
56
53
|
end
|
57
54
|
|
58
55
|
params = filter_params(params) if respond_to?(:filter_params)
|
59
|
-
persist_params params
|
56
|
+
object = persist_params params
|
57
|
+
self.id = object.uid if id.blank?
|
58
|
+
true
|
60
59
|
end
|
61
60
|
|
62
61
|
def load_model_attributes
|
@@ -66,7 +65,7 @@ class Quill::BaseModel
|
|
66
65
|
object.data = {} if object.data.nil?
|
67
66
|
attrs = {}
|
68
67
|
|
69
|
-
|
68
|
+
self.id = object.uid
|
70
69
|
|
71
70
|
# load attributes defined on the superclass. These attributes
|
72
71
|
# are designated by Quill.
|
@@ -87,22 +86,26 @@ class Quill::BaseModel
|
|
87
86
|
save || raise
|
88
87
|
end
|
89
88
|
|
89
|
+
def inspect
|
90
|
+
%Q|#<#{self.class.name} #{@data.map{|k,v| "#{k}=#{v.inspect}"}.join(' ')}>|
|
91
|
+
end
|
92
|
+
|
90
93
|
protected
|
91
94
|
|
92
95
|
def key_present?
|
93
|
-
|
96
|
+
id.present?
|
94
97
|
end
|
95
98
|
|
96
99
|
def persist_params params
|
97
|
-
if
|
98
|
-
api.activity_sessions.update(
|
100
|
+
if id.present?
|
101
|
+
api.activity_sessions.update(id, params)
|
99
102
|
else
|
100
103
|
api.activity_sessions.create(params)
|
101
104
|
end
|
102
105
|
end
|
103
106
|
|
104
107
|
def api
|
105
|
-
@api ||= Quill::Client.new
|
108
|
+
@api ||= Quill::Client.new(access_token: access_token)
|
106
109
|
end
|
107
110
|
|
108
111
|
def perform_validations(options={})
|
data/lib/quill/client.rb
CHANGED
@@ -1,60 +1,83 @@
|
|
1
|
-
|
2
|
-
class
|
3
|
-
|
1
|
+
module Quill
|
2
|
+
class Client
|
3
|
+
class InvalidKeys < Exception ; end
|
4
|
+
attr_accessor :token, :client, :config
|
5
|
+
delegate :get, :post, :put, to: :token
|
6
|
+
include EndpointDefinitions
|
4
7
|
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
+
def initialize options = {}
|
9
|
+
@config = Configuration.new(options, api_url: 'http://api.quill.org/')
|
10
|
+
raise ArgumentError if config.access_token.blank?
|
8
11
|
end
|
9
|
-
|
10
|
-
def activity_sessions
|
11
|
-
Endpoint.new(self, "activity_sessions", {"attributes"=>{"percentage"=>nil, "time_spent"=>nil, "state"=>nil, "completed_at"=>nil, "data"=>{}, "temporary"=>nil, "activity_uid"=>nil}, "readonly_attributes"=>{"anonymous"=>nil}})
|
12
|
-
end
|
13
|
-
|
14
12
|
|
15
|
-
|
16
|
-
|
17
|
-
conn.request :json
|
18
|
-
conn.response :json
|
19
|
-
conn.adapter Faraday.default_adapter
|
13
|
+
def token
|
14
|
+
@token ||= OAuth2::AccessToken.new client, config.access_token
|
20
15
|
end
|
21
|
-
end
|
22
|
-
|
23
|
-
class Endpoint
|
24
|
-
attr_accessor :name, :api
|
25
16
|
|
26
|
-
def
|
27
|
-
@
|
28
|
-
|
29
|
-
|
30
|
-
|
17
|
+
def client
|
18
|
+
@client ||= OAuth2::Client.new(config.client_id, config.client_secret, site: config.api_url, raise_errors: false) do |conn|
|
19
|
+
conn.request :json
|
20
|
+
conn.response :json
|
21
|
+
conn.adapter Faraday.default_adapter
|
22
|
+
end
|
31
23
|
end
|
32
24
|
|
33
|
-
def
|
34
|
-
|
35
|
-
response = api.get([name,id].join('/'), params)
|
36
|
-
Hashie::Mash.new(response.body['object'])
|
25
|
+
def get path, params = {}
|
26
|
+
request :get, path, params
|
37
27
|
end
|
38
28
|
|
39
|
-
def
|
40
|
-
|
41
|
-
response = api.post(name, params)
|
42
|
-
Hashie::Mash.new(response.body['object'])
|
29
|
+
def post path, params = {}
|
30
|
+
request :post, path, params
|
43
31
|
end
|
44
32
|
|
45
|
-
def
|
46
|
-
|
47
|
-
response = api.put([name,id].join('/'), params)
|
48
|
-
Hashie::Mash.new(response.body['object'])
|
33
|
+
def put path, params = {}
|
34
|
+
request :put, path, params
|
49
35
|
end
|
50
36
|
|
51
|
-
def
|
37
|
+
def request verb, path, params = {}
|
38
|
+
key = if verb == :get then :params else :body end
|
39
|
+
response = token.request verb, path, key => params
|
40
|
+
raise response.error if response.status == 401
|
41
|
+
response
|
52
42
|
end
|
53
43
|
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
44
|
+
class Endpoint
|
45
|
+
attr_accessor :name, :api
|
46
|
+
|
47
|
+
def initialize api, name, definition
|
48
|
+
@api = api
|
49
|
+
@name = name
|
50
|
+
@attributes = definition['attributes'].keys
|
51
|
+
@readonly_attributes = definition['readonly_attributes'].try(:keys) || []
|
52
|
+
end
|
53
|
+
|
54
|
+
def find id, params = {}
|
55
|
+
check_keys(params)
|
56
|
+
response = api.get([name,id].join('/'), params)
|
57
|
+
Hashie::Mash.new(response.body['object'])
|
58
|
+
end
|
59
|
+
|
60
|
+
def create params
|
61
|
+
check_keys(params)
|
62
|
+
response = api.post(name, params)
|
63
|
+
Hashie::Mash.new(response.body['object'])
|
64
|
+
end
|
65
|
+
|
66
|
+
def update id, params
|
67
|
+
check_keys(params)
|
68
|
+
response = api.put([name,id].join('/'), params)
|
69
|
+
Hashie::Mash.new(response.body['object'])
|
70
|
+
end
|
71
|
+
|
72
|
+
def list
|
73
|
+
end
|
74
|
+
|
75
|
+
def check_keys params
|
76
|
+
# raise @readonly_attributes.inspect
|
77
|
+
invalid_keys = params.stringify_keys.keys - @attributes - @readonly_attributes
|
78
|
+
raise InvalidKeys, "Endpoint ##{name} does not support key(s) #{invalid_keys.join(', ')}" unless invalid_keys.empty?
|
79
|
+
end
|
58
80
|
end
|
59
81
|
end
|
82
|
+
|
60
83
|
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
module Quill
|
2
|
+
|
3
|
+
class Configuration
|
4
|
+
def initialize options = {}, defaults = {}
|
5
|
+
options.assert_valid_keys(*@@keys)
|
6
|
+
@config = {}
|
7
|
+
|
8
|
+
@@keys.each do |key|
|
9
|
+
@config[key] = options[key] || @@env["#{@@namespace.to_s.upcase}_#{key.to_s.upcase}"] || defaults[key]
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
def method_missing method
|
14
|
+
super unless @@keys.include? method
|
15
|
+
@config[method]
|
16
|
+
end
|
17
|
+
|
18
|
+
class << self
|
19
|
+
def keys *args
|
20
|
+
@@keys = args
|
21
|
+
end
|
22
|
+
|
23
|
+
def namespace namespace
|
24
|
+
@@namespace = namespace
|
25
|
+
end
|
26
|
+
|
27
|
+
def env env
|
28
|
+
@@env = env
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
module Quill
|
2
|
+
module EndpointDefinitions
|
3
|
+
|
4
|
+
def activities
|
5
|
+
Client::Endpoint.new(self, "activities", {"attributes"=>{"data"=>{}, "name"=>nil, "description"=>nil}, "readonly_attributes"=>{"cid"=>nil}})
|
6
|
+
end
|
7
|
+
|
8
|
+
def activity_sessions
|
9
|
+
Client::Endpoint.new(self, "activity_sessions", {"attributes"=>{"percentage"=>nil, "time_spent"=>nil, "state"=>nil, "completed_at"=>nil, "data"=>{}, "temporary"=>nil, "activity_uid"=>nil}, "readonly_attributes"=>{"anonymous"=>nil}})
|
10
|
+
end
|
11
|
+
|
12
|
+
end
|
13
|
+
end
|
data/lib/quill/oauth.rb
ADDED
@@ -0,0 +1,11 @@
|
|
1
|
+
module Quill
|
2
|
+
|
3
|
+
class Oauth < OAuth2::Client
|
4
|
+
|
5
|
+
def initialize(client_id=nil, client_secret=nil, opts={}, &block)
|
6
|
+
config = Configuration.new({}, site_url: 'http://www.quill.org/')
|
7
|
+
super(client_id || config.client_id, client_secret || config.client_secret, opts.reverse_merge(site: config.site_url), &block)
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
end
|
data/lib/quill.rb
CHANGED
@@ -1,6 +1,13 @@
|
|
1
1
|
module Quill
|
2
|
-
autoload :ActivityModel,
|
3
|
-
autoload :ActivitySession,
|
4
|
-
autoload :BaseModel,
|
5
|
-
autoload :Client,
|
2
|
+
autoload :ActivityModel, 'quill/activity_model'
|
3
|
+
autoload :ActivitySession, 'quill/activity_session'
|
4
|
+
autoload :BaseModel, 'quill/base_model'
|
5
|
+
autoload :Client, 'quill/client'
|
6
|
+
autoload :Configuration, 'quill/configuration'
|
7
|
+
autoload :EndpointDefinitions, 'quill/endpoint_definitions'
|
8
|
+
autoload :Oauth, 'quill/oauth'
|
9
|
+
|
10
|
+
Configuration.namespace :quill
|
11
|
+
Configuration.keys :api_url, :client_id, :client_secret, :access_token, :site_url
|
12
|
+
Configuration.env ENV
|
6
13
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: quill-api-client
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.1.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,8 +9,19 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2014-
|
13
|
-
dependencies:
|
12
|
+
date: 2014-02-06 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: oauth2
|
16
|
+
requirement: &70318755727340 !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>'
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *70318755727340
|
14
25
|
description: Wrapper to Quill API
|
15
26
|
email: quinn@tastehoneyco.com
|
16
27
|
executables: []
|
@@ -21,6 +32,9 @@ files:
|
|
21
32
|
- lib/quill/activity_session.rb
|
22
33
|
- lib/quill/base_model.rb
|
23
34
|
- lib/quill/client.rb
|
35
|
+
- lib/quill/configuration.rb
|
36
|
+
- lib/quill/endpoint_definitions.rb
|
37
|
+
- lib/quill/oauth.rb
|
24
38
|
- lib/quill-api-client.rb
|
25
39
|
- lib/quill.rb
|
26
40
|
homepage: https://github.com/empirical/quill-platform
|