quill-api-client 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.
@@ -0,0 +1,32 @@
1
+ class Quill::ActivityModel < Quill::BaseModel
2
+ include ActiveModel::Model
3
+
4
+ special_attrs :name, :description
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
+ def find
22
+ api.activities.find(_uid, cid: _cid)
23
+ end
24
+
25
+ def persist_params params
26
+ if _uid.present?
27
+ api.activities.update(_uid, params)
28
+ else
29
+ api.activities.create(params)
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,23 @@
1
+ class Quill::ActivitySession < Quill::BaseModel
2
+ special_attrs :percentage, :time_spent, :state, :completed_at, :activity_uid, :anonymous
3
+
4
+ def find
5
+ if anonymous && _uid.blank?
6
+ persist
7
+ else
8
+ api.activity_sessions.find(_uid)
9
+ end
10
+ end
11
+
12
+ def activity(cid)
13
+ raise if activity_uid.blank?
14
+ return nil if activity_uid.blank?
15
+ return @activity if @activity.present?
16
+ @activity = Story.new(_uid: activity_uid, _cid: cid)
17
+ end
18
+
19
+ def key_present?
20
+ return true if anonymous
21
+ super
22
+ end
23
+ end
@@ -0,0 +1,111 @@
1
+ class Quill::BaseModel
2
+ include ActiveModel::Model
3
+
4
+ def self.special_attrs *attrs
5
+ return @special_attrs unless attrs.any?
6
+ @special_attrs = attrs
7
+ end
8
+
9
+ def self.inherited subclass
10
+ subclass.special_attrs *@special_attrs
11
+ end
12
+
13
+ def self.attributes *attrs
14
+ return @attributes if defined?(@attributes)
15
+ @attributes = attrs
16
+
17
+ (attrs.dup.unshift(*@special_attrs).unshift(*key_attributes)).each do |attr|
18
+ class_eval <<-CODE
19
+ def #{attr}
20
+ @data[:#{attr}]
21
+ end
22
+
23
+ def #{attr}= value
24
+ @data[:#{attr}] = value
25
+ end
26
+ CODE
27
+ end
28
+ end
29
+
30
+ def self.key_attributes
31
+ [:_uid]
32
+ end
33
+
34
+ def initialize *args
35
+ @data = {}
36
+ super
37
+ load_model_attributes
38
+ end
39
+
40
+ def save(options={})
41
+ perform_validations(options) ? persist : false
42
+ end
43
+
44
+ def persist
45
+ data = @data.except(*self.class.special_attrs.dup << :_uid)
46
+ serialized_data = {}
47
+
48
+ data.each do |key, value|
49
+ serialized_data[key] = value.to_yaml
50
+ end
51
+
52
+ params = { data: serialized_data }
53
+
54
+ self.class.special_attrs.each do |attr|
55
+ params[attr] = send(attr)
56
+ end
57
+
58
+ params = filter_params(params) if respond_to?(:filter_params)
59
+ persist_params params
60
+ end
61
+
62
+ def load_model_attributes
63
+ return unless key_present?
64
+
65
+ object = find
66
+ object.data = {} if object.data.nil?
67
+ attrs = {}
68
+
69
+ attrs[:_uid] = object.uid
70
+
71
+ # load attributes defined on the superclass. These attributes
72
+ # are designated by Quill.
73
+ self.class.special_attrs.each do |attr|
74
+ attrs[attr] = object.send(attr)
75
+ end
76
+
77
+ # load user defined attributes. This is arbitrary data that the app
78
+ # has stored for this record.
79
+ self.class.attributes.each do |attr|
80
+ attrs[attr] = YAML.load(object.data[attr]) if object.data[attr].present?
81
+ end
82
+
83
+ @data.reverse_merge!(attrs)
84
+ end
85
+
86
+ def save!
87
+ save || raise
88
+ end
89
+
90
+ protected
91
+
92
+ def key_present?
93
+ _uid.present?
94
+ end
95
+
96
+ def persist_params params
97
+ if _uid.present?
98
+ api.activity_sessions.update(_uid, params)
99
+ else
100
+ api.activity_sessions.create(params)
101
+ end
102
+ end
103
+
104
+ def api
105
+ @api ||= Quill::Client.new
106
+ end
107
+
108
+ def perform_validations(options={})
109
+ options[:validate] == false || valid?(options[:context])
110
+ end
111
+ end
@@ -0,0 +1,60 @@
1
+ class Quill::Client
2
+ class InvalidKeys < Exception ; end
3
+ delegate :get, :post, :put, to: :connection
4
+
5
+
6
+ def activities
7
+ Endpoint.new(self, "activities", {"attributes"=>{"data"=>{}, "name"=>nil, "description"=>nil}, "readonly_attributes"=>{"cid"=>nil}})
8
+ 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
+
15
+ def connection
16
+ Faraday.new(url: ENV['QUILL_API_URL']) do |conn|
17
+ conn.request :json
18
+ conn.response :json
19
+ conn.adapter Faraday.default_adapter
20
+ end
21
+ end
22
+
23
+ class Endpoint
24
+ attr_accessor :name, :api
25
+
26
+ def initialize api, name, definition
27
+ @api = api
28
+ @name = name
29
+ @attributes = definition['attributes'].keys
30
+ @readonly_attributes = definition['readonly_attributes'].try(:keys) || []
31
+ end
32
+
33
+ def find id, params = {}
34
+ check_keys(params)
35
+ response = api.get([name,id].join('/'), params)
36
+ Hashie::Mash.new(response.body['object'])
37
+ end
38
+
39
+ def create params
40
+ check_keys(params)
41
+ response = api.post(name, params)
42
+ Hashie::Mash.new(response.body['object'])
43
+ end
44
+
45
+ def update id, params
46
+ check_keys(params)
47
+ response = api.put([name,id].join('/'), params)
48
+ Hashie::Mash.new(response.body['object'])
49
+ end
50
+
51
+ def list
52
+ end
53
+
54
+ def check_keys params
55
+ # raise @readonly_attributes.inspect
56
+ invalid_keys = params.stringify_keys.keys - @attributes - @readonly_attributes
57
+ raise InvalidKeys, "Endpoint ##{name} does not support key(s) #{invalid_keys.join(', ')}" unless invalid_keys.empty?
58
+ end
59
+ end
60
+ end
metadata ADDED
@@ -0,0 +1,49 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: quill-api-client
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Quinn Shanahan
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2014-01-28 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: Wrapper to Quill API
15
+ email: quinn@tastehoneyco.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - lib/quill/activity_model.rb
21
+ - lib/quill/activity_session.rb
22
+ - lib/quill/base_model.rb
23
+ - lib/quill/client.rb
24
+ homepage: https://github.com/empirical/quill-platform
25
+ licenses: []
26
+ post_install_message:
27
+ rdoc_options: []
28
+ require_paths:
29
+ - lib
30
+ required_ruby_version: !ruby/object:Gem::Requirement
31
+ none: false
32
+ requirements:
33
+ - - ! '>='
34
+ - !ruby/object:Gem::Version
35
+ version: '0'
36
+ required_rubygems_version: !ruby/object:Gem::Requirement
37
+ none: false
38
+ requirements:
39
+ - - ! '>='
40
+ - !ruby/object:Gem::Version
41
+ version: '0'
42
+ requirements: []
43
+ rubyforge_project:
44
+ rubygems_version: 1.8.11
45
+ signing_key:
46
+ specification_version: 3
47
+ summary: Wrapper to Quill API
48
+ test_files: []
49
+ has_rdoc: