fulcrum 0.1.6 → 0.2.0

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 (60) hide show
  1. checksums.yaml +8 -8
  2. data/LICENSE +2 -2
  3. data/README.md +189 -70
  4. data/Rakefile +5 -0
  5. data/fulcrum.gemspec +2 -3
  6. data/lib/fulcrum.rb +22 -14
  7. data/lib/fulcrum/actions/create.rb +15 -0
  8. data/lib/fulcrum/actions/delete.rb +12 -0
  9. data/lib/fulcrum/actions/find.rb +11 -0
  10. data/lib/fulcrum/actions/list.rb +19 -0
  11. data/lib/fulcrum/actions/update.rb +11 -0
  12. data/lib/fulcrum/changeset.rb +12 -0
  13. data/lib/fulcrum/choice_list.rb +6 -35
  14. data/lib/fulcrum/classification_set.rb +6 -36
  15. data/lib/fulcrum/client.rb +120 -0
  16. data/lib/fulcrum/form.rb +6 -35
  17. data/lib/fulcrum/layer.rb +6 -0
  18. data/lib/fulcrum/media_resource.rb +47 -0
  19. data/lib/fulcrum/membership.rb +5 -0
  20. data/lib/fulcrum/page.rb +17 -0
  21. data/lib/fulcrum/photo.rb +9 -29
  22. data/lib/fulcrum/project.rb +3 -9
  23. data/lib/fulcrum/record.rb +6 -35
  24. data/lib/fulcrum/resource.rb +40 -0
  25. data/lib/fulcrum/signature.rb +15 -0
  26. data/lib/fulcrum/version.rb +1 -1
  27. data/lib/fulcrum/video.rb +23 -0
  28. data/spec/client_helper.rb +9 -0
  29. data/spec/data/test.jpg +0 -0
  30. data/spec/data/test.mp4 +0 -0
  31. data/spec/data/test.png +0 -0
  32. data/spec/lib/choice_list_spec.rb +10 -115
  33. data/spec/lib/classification_set_spec.rb +11 -114
  34. data/spec/lib/client_spec.rb +9 -0
  35. data/spec/lib/form_spec.rb +10 -111
  36. data/spec/lib/layer_spec.rb +13 -0
  37. data/spec/lib/membership_spec.rb +12 -0
  38. data/spec/lib/photo_spec.rb +9 -88
  39. data/spec/lib/project_spec.rb +7 -23
  40. data/spec/lib/record_spec.rb +10 -115
  41. data/spec/lib/signature_spec.rb +16 -0
  42. data/spec/lib/video_spec.rb +16 -0
  43. data/spec/resource_examples.rb +147 -0
  44. data/spec/spec_helper.rb +2 -0
  45. metadata +37 -41
  46. data/.rvmrc +0 -1
  47. data/lib/fulcrum/api.rb +0 -99
  48. data/lib/fulcrum/member.rb +0 -16
  49. data/lib/fulcrum/validators/base_validator.rb +0 -31
  50. data/lib/fulcrum/validators/choice_list_validator.rb +0 -30
  51. data/lib/fulcrum/validators/classification_set_validator.rb +0 -38
  52. data/lib/fulcrum/validators/form_validator.rb +0 -150
  53. data/lib/fulcrum/validators/record_validator.rb +0 -18
  54. data/spec/data/form_data.json +0 -175
  55. data/spec/lib/api_spec.rb +0 -42
  56. data/spec/lib/member_spec.rb +0 -52
  57. data/spec/lib/validators/choice_list_validator_spec.rb +0 -73
  58. data/spec/lib/validators/classification_set_validator_spec.rb +0 -88
  59. data/spec/lib/validators/form_validator_spec.rb +0 -4
  60. data/spec/lib/validators/record_validator_spec.rb +0 -53
@@ -0,0 +1,11 @@
1
+ module Fulcrum
2
+ module Actions
3
+ module Find
4
+ extend ActiveSupport::Concern
5
+
6
+ def find(id)
7
+ call(:get, member(id))[resource_name]
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,19 @@
1
+ module Fulcrum
2
+ module Actions
3
+ module List
4
+ DEFAULT_PER_PAGE = 250
5
+
6
+ extend ActiveSupport::Concern
7
+
8
+ def default_list_params
9
+ { per_page: DEFAULT_PER_PAGE }
10
+ end
11
+
12
+ def all(params = default_list_params)
13
+ result = call(:get, collection, default_list_params.merge(params))
14
+
15
+ Page.new(result, resources_name)
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,11 @@
1
+ module Fulcrum
2
+ module Actions
3
+ module Update
4
+ extend ActiveSupport::Concern
5
+
6
+ def update(id, object)
7
+ call(:put, member(id), attributes_for_object(object))[resource_name]
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,12 @@
1
+ module Fulcrum
2
+ class Changeset < Resource
3
+ include Actions::List
4
+ include Actions::Find
5
+ include Actions::Create
6
+ include Actions::Update
7
+
8
+ def close(id, object={})
9
+ call(:put, member_action(id, 'close'), attributes_for_object(object))[resource_name]
10
+ end
11
+ end
12
+ end
@@ -1,38 +1,9 @@
1
1
  module Fulcrum
2
- class ChoiceList < Api
3
-
4
- class << self
5
-
6
- def all(opts = {})
7
- params = parse_opts([:page], opts)
8
- call(:get, 'choice_lists.json', params)
9
- end
10
-
11
- def find(id)
12
- call(:get, "choice_lists/#{id}.json")
13
- end
14
-
15
- def create(choice_list)
16
- validation = ChoiceListValidator.new(choice_list)
17
- if validation.valid?
18
- call(:post, 'choice_lists.json', choice_list)
19
- else
20
- { error: { validation: validation.errors } }
21
- end
22
- end
23
-
24
- def update(id, choice_list)
25
- validation = ChoiceListValidator.new(choice_list)
26
- if validation.valid?
27
- call(:put, "choice_lists/#{id}.json", choice_list)
28
- else
29
- { error: { validation: validation.errors } }
30
- end
31
- end
32
-
33
- def delete(id)
34
- call(:delete, "choice_lists/#{id}.json")
35
- end
36
- end
2
+ class ChoiceList < Resource
3
+ include Actions::List
4
+ include Actions::Find
5
+ include Actions::Create
6
+ include Actions::Update
7
+ include Actions::Delete
37
8
  end
38
9
  end
@@ -1,39 +1,9 @@
1
1
  module Fulcrum
2
- class ClassificationSet < Api
3
-
4
- class << self
5
-
6
- def all(opts = {})
7
- params = parse_opts([:page], opts)
8
- call(:get, 'classification_sets.json', params)
9
- end
10
-
11
- def find(id)
12
- call(:get, "classification_sets/#{id}.json")
13
- end
14
-
15
- def create(classification_set)
16
- validation = ClassificationSetValidator.new(classification_set)
17
- if validation.valid?
18
- call(:post, 'classification_sets.json', classification_set)
19
- else
20
- { error: { validation: validation.errors } }
21
- end
22
- end
23
-
24
- def update(id, classification_set)
25
- validation = ClassificationSetValidator.new(classification_set)
26
- if validation.valid?
27
- call(:put, "classification_sets/#{id}.json", classification_set)
28
- else
29
- { error: { validation: validation.errors } }
30
- end
31
- end
32
-
33
- def delete(id)
34
- call(:delete, "classification_sets/#{id}.json")
35
- end
36
- end
37
-
2
+ class ClassificationSet < Resource
3
+ include Actions::List
4
+ include Actions::Find
5
+ include Actions::Create
6
+ include Actions::Update
7
+ include Actions::Delete
38
8
  end
39
9
  end
@@ -0,0 +1,120 @@
1
+ require 'faraday'
2
+ require 'faraday_middleware'
3
+
4
+ module Fulcrum
5
+
6
+ class Client
7
+
8
+ DEFAULT_URL = 'https://api.fulcrumapp.com/api/v2'
9
+
10
+ DEFAULT_USER_AGENT = "Fulcrum Ruby API Client, Version #{Fulcrum::VERSION}"
11
+
12
+ attr_accessor :connection
13
+ attr_accessor :response
14
+ attr_accessor :key
15
+ attr_accessor :url
16
+
17
+ def initialize(key = nil, url = nil)
18
+ self.key = key
19
+ self.url = url || DEFAULT_URL
20
+ end
21
+
22
+ def key=(key)
23
+ @key = key
24
+ reset
25
+ end
26
+
27
+ def url=(url)
28
+ @url = url
29
+ reset
30
+ end
31
+
32
+ def reset
33
+ @response = nil
34
+ @connection = nil
35
+ end
36
+
37
+ def call(method = :get, path = '', params = {})
38
+ @response = connection.send(method.to_sym, path, params.with_indifferent_access)
39
+ @response.body
40
+ end
41
+
42
+ def connection
43
+ @connection ||= self.class.create_connection(url, key)
44
+ end
45
+
46
+ def self.authenticate(username, password, url=DEFAULT_URL)
47
+ connection = create_connection(url)
48
+
49
+ connection.basic_auth(username, password)
50
+
51
+ resp = connection.get('users.json')
52
+
53
+ resp.body['user']['contexts'].map do |context|
54
+ { id: context['id'],
55
+ name: context['name'],
56
+ token: context['api_token'] }
57
+ end
58
+ end
59
+
60
+ def self.create_connection(url, key = nil)
61
+ Faraday.new(url) do |connection|
62
+ connection.request :multipart
63
+ connection.request :json
64
+
65
+ connection.response :raise_error
66
+ connection.response :json, content_type: 'application/json'
67
+
68
+ connection.adapter Faraday.default_adapter
69
+
70
+ connection.headers['X-ApiToken'] = key if key
71
+ connection.headers['X-Require-Media'] = 'false'
72
+ connection.headers['User-Agent'] = DEFAULT_USER_AGENT
73
+ end
74
+ end
75
+
76
+ def choice_lists
77
+ @choice_lists ||= Fulcrum::ChoiceList.new(self)
78
+ end
79
+
80
+ def classification_sets
81
+ @classification_sets ||= Fulcrum::ClassificationSet.new(self)
82
+ end
83
+
84
+ def forms
85
+ @forms ||= Fulcrum::Form.new(self)
86
+ end
87
+
88
+ def photos
89
+ @photos ||= Fulcrum::Photo.new(self)
90
+ end
91
+
92
+ def videos
93
+ @videos ||= Fulcrum::Video.new(self)
94
+ end
95
+
96
+ def signatures
97
+ @signatures ||= Fulcrum::Signature.new(self)
98
+ end
99
+
100
+ def projects
101
+ @projects ||= Fulcrum::Project.new(self)
102
+ end
103
+
104
+ def records
105
+ @records ||= Fulcrum::Record.new(self)
106
+ end
107
+
108
+ def memberships
109
+ @memberships ||= Fulcrum::Membership.new(self)
110
+ end
111
+
112
+ def layers
113
+ @layers ||= Fulcrum::Layer.new(self)
114
+ end
115
+
116
+ def changesets
117
+ @changesets ||= Fulcrum::Changeset.new(self)
118
+ end
119
+ end
120
+ end
data/lib/fulcrum/form.rb CHANGED
@@ -1,38 +1,9 @@
1
1
  module Fulcrum
2
- class Form < Api
3
- class << self
4
-
5
- def all(opts = {})
6
- params = self.parse_opts([:page, :schema], opts)
7
- self.call(:get, 'forms.json', params)
8
- end
9
-
10
- def find(id, opts = {})
11
- params = parse_opts([:include_foreign_elements], opts)
12
- call(:get, "forms/#{id}.json", params)
13
- end
14
-
15
- def create(form)
16
- validation = FormValidator.new(form)
17
- if validation.valid?
18
- call(:post, "forms.json", form)
19
- else
20
- { error: { validation: validation.errors } }
21
- end
22
- end
23
-
24
- def update(id, form)
25
- validation = FormValidator.new(form)
26
- if validation.valid?
27
- call(:put, "forms/#{id}.json", form)
28
- else
29
- { error: { validation: validation.errors } }
30
- end
31
- end
32
-
33
- def delete(id)
34
- call(:delete, "forms/#{id}.json")
35
- end
36
- end
2
+ class Form < Resource
3
+ include Actions::List
4
+ include Actions::Find
5
+ include Actions::Create
6
+ include Actions::Update
7
+ include Actions::Delete
37
8
  end
38
9
  end
@@ -0,0 +1,6 @@
1
+ module Fulcrum
2
+ class Layer < Resource
3
+ include Actions::List
4
+ include Actions::Find
5
+ end
6
+ end
@@ -0,0 +1,47 @@
1
+ require 'open-uri'
2
+
3
+ module Fulcrum
4
+ class MediaResource < Resource
5
+ include Actions::List
6
+ include Actions::Find
7
+ include Actions::Create
8
+
9
+ def default_content_type
10
+ raise NotImplementedError,
11
+ 'default_content_type must be implemented in derived classes'
12
+ end
13
+
14
+ def attributes_for_upload(file, content_type = nil, attributes = {})
15
+ attributes ||= {}
16
+
17
+ file = Faraday::UploadIO.new(file, content_type || default_content_type)
18
+
19
+ attributes[:file] = file
20
+ attributes[:access_key] ||= new_access_key
21
+
22
+ media_attributes = {}
23
+ media_attributes[resource_name] = attributes
24
+ media_attributes
25
+ end
26
+
27
+ def create(file, content_type = nil, attrs = {})
28
+ call(:post, create_action, attributes_for_upload(file, content_type, attrs))
29
+ end
30
+
31
+ def download(url, &blk)
32
+ open(url, "rb", &blk)
33
+ end
34
+
35
+ def download_version(access_key, version, &blk)
36
+ download(find(access_key)[version], &blk)
37
+ end
38
+
39
+ def original(access_key, &blk)
40
+ download_version(access_key, 'original', &blk)
41
+ end
42
+
43
+ def new_access_key
44
+ SecureRandom.uuid
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,5 @@
1
+ module Fulcrum
2
+ class Membership < Resource
3
+ include Actions::List
4
+ end
5
+ end
@@ -0,0 +1,17 @@
1
+ module Fulcrum
2
+ class Page
3
+ attr_accessor :objects
4
+ attr_accessor :current_page
5
+ attr_accessor :total_pages
6
+ attr_accessor :total_count
7
+ attr_accessor :per_page
8
+
9
+ def initialize(data, resources_name)
10
+ self.objects = data[resources_name]
11
+ self.current_page = data['current_page']
12
+ self.total_pages = data['total_pages']
13
+ self.total_count = data['total_count']
14
+ self.per_page = data['per_page']
15
+ end
16
+ end
17
+ end
data/lib/fulcrum/photo.rb CHANGED
@@ -1,35 +1,15 @@
1
1
  module Fulcrum
2
- class Photo < Api
3
-
4
- ALLOWED_FORMATS = %w(jpg json)
5
-
6
- class << self
7
-
8
- def find(id, opts = {})
9
- format = image_opts(opts)
10
- call(:get, "photos/#{id}.#{format}")
11
- end
12
-
13
- def thumbnail(id, opts = {})
14
- format = image_opts(opts)
15
- call(:get, "photos/#{id}/thumbnail.#{format}")
16
- end
17
-
18
- def create(file, content_type, id, label = '')
19
- photo = Faraday::UploadIO.new(file, content_type)
20
- call(:post, 'photos.json', { photo: { file: photo, access_key: id, label: label}})
21
- end
2
+ class Photo < MediaResource
3
+ def default_content_type
4
+ 'image/jpeg'
5
+ end
22
6
 
23
- def delete(id)
24
- call(:delete, "photos/#{id}.json")
25
- end
7
+ def large(id, &blk)
8
+ download_version(id, 'large', &blk)
9
+ end
26
10
 
27
- def image_opts(opts = {})
28
- opts = opts.with_indifferent_access
29
- format = opts.delete(:format) || 'json'
30
- raise ArgumentError, "#{format} is not an allowed format, use either 'json' or 'jpg'" unless ALLOWED_FORMATS.include?(format)
31
- format
32
- end
11
+ def thumbnail(id, &blk)
12
+ download_version(id, 'thumbnail', &blk)
33
13
  end
34
14
  end
35
15
  end