manatoo 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. checksums.yaml +7 -0
  2. data/lib/manatoo.rb +55 -0
  3. data/lib/manatoo/task.rb +167 -0
  4. metadata +88 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 6ffdb7e9993e05645fa9ecef214cca0c6d592bf3
4
+ data.tar.gz: 322c2bb82014d524d2ab2665152e6c9cafbc7615
5
+ SHA512:
6
+ metadata.gz: b4da24fd572a88f2cd633a044b8023b394dd1004612555af630a6538b62522276b84f0cfbd9b877a7ffd9c0d5066609c60509baa682265219c1dbe57b2887a96
7
+ data.tar.gz: cc7235a035633911b20a82e0419fddc6d144cbe47e3e0b8a3c622575b07ccad9e3ffc0e584ef16b6d294291b5b0f121a28b259ec47a3a9b215010b006b58daad
@@ -0,0 +1,55 @@
1
+ require 'base64'
2
+ require 'faraday'
3
+
4
+ require 'manatoo/task'
5
+
6
+ module Manatoo
7
+ @api_base = 'https://manatoo.io/api'
8
+ @api_key = nil
9
+
10
+ class ApiKeyError < StandardError
11
+ def initialize
12
+ super('Missing Manatoo API KEY')
13
+ end
14
+ end
15
+
16
+ class << self
17
+ attr_accessor :api_key, :api_base
18
+ attr_reader :last_response_json
19
+ end
20
+
21
+ def self.conn
22
+ raise ApiKeyError.new if self.api_key.nil?
23
+
24
+ Faraday.new(self.api_base) do |conn|
25
+ conn.basic_auth(self.api_key, '')
26
+ conn.adapter(Faraday.default_adapter)
27
+ end
28
+ end
29
+
30
+ def self.get(url)
31
+ self.conn.get do |req|
32
+ req.url(url)
33
+ end
34
+ end
35
+
36
+ def self.request_with_payload(action, url, payload)
37
+ self.conn.send(action) do |req|
38
+ req.url(url)
39
+ req.headers['Content-Type'] = 'application/json'
40
+ req.body = payload.to_json
41
+ end
42
+ end
43
+
44
+ def self.post(url, payload)
45
+ self.request_with_payload(:post, url, payload)
46
+ end
47
+
48
+ def self.put(url, payload)
49
+ self.request_with_payload(:put, url, payload)
50
+ end
51
+
52
+ def self.delete(url, payload)
53
+ self.request_with_payload(:delete, url, payload)
54
+ end
55
+ end
@@ -0,0 +1,167 @@
1
+ require 'forwardable'
2
+ require 'json'
3
+ require 'plissken'
4
+
5
+ module Manatoo
6
+ class Task
7
+ extend Forwardable
8
+
9
+ TASK_KEYS = [
10
+ :id,
11
+ :title,
12
+ :data,
13
+ :description,
14
+ :slug,
15
+ :status,
16
+ :weight,
17
+ :duration,
18
+ :labels,
19
+ :finished_at,
20
+ :created_at,
21
+ :due_at,
22
+ :started_at,
23
+ :list_id,
24
+ :users,
25
+ ]
26
+
27
+ ATTRIBUTES_STRUCT = Struct.new(*TASK_KEYS)
28
+
29
+ attr_reader :attributes, :last_json_response
30
+ delegate TASK_KEYS => :attributes
31
+
32
+ def handle_snake_cased_json(snake_cased_json)
33
+ # if a user wanted the last_response_json, I assume they would want it
34
+ # in a snake_cased ruby usable fashion
35
+ @last_json_response = snake_cased_json
36
+ task_attrs = @last_json_response['data']
37
+ set_attributes_from_hash(task_attrs)
38
+ self
39
+ end
40
+
41
+ def set_attributes_from_hash(hash)
42
+ hash.each do |k, v|
43
+ attributes[k] = v
44
+ end
45
+ end
46
+
47
+ # list_id, title REQUIRED
48
+ def self.create(attrs, return_task=false)
49
+ raise ArgumentError.new('Task attributes must be passed in as a Hash') unless attrs.is_a?(Hash)
50
+
51
+ list_id = attrs[:list_id]
52
+ title = attrs[:title]
53
+ raise ArgumentError.new(
54
+ 'Both list_id and title must be passed in and not blank'
55
+ ) if list_id.nil? or list_id.empty? or title.nil? or title.empty?
56
+
57
+ url = "tasks"
58
+ resp = Manatoo.post(url, attrs.merge({
59
+ title: title,
60
+ list_id: list_id,
61
+ }))
62
+ snake_cased_json = JSON.parse(resp.body).to_snake_keys
63
+ attrs = snake_cased_json['data']
64
+ return Task.new(attrs, snake_cased_json) if return_task
65
+ return snake_cased_json
66
+ end
67
+
68
+ # task_id REQUIRED
69
+ def self.find(task_id, return_task=false)
70
+ url = "tasks/#{task_id}"
71
+ resp = Manatoo.get(url)
72
+ snake_cased_json = JSON.parse(resp.body).to_snake_keys
73
+ attrs = snake_cased_json['data']
74
+ return Task.new(attrs, snake_cased_json) if return_task
75
+ return snake_cased_json
76
+ end
77
+
78
+ def initialize(data, snake_cased_json)
79
+ @attributes = ATTRIBUTES_STRUCT.new
80
+ set_attributes_from_hash(data)
81
+ @last_json_response = snake_cased_json
82
+ self
83
+ end
84
+
85
+ # task_id REQUIRED
86
+ def self.update(task_id, attrs)
87
+ url = "tasks/#{task_id}"
88
+ resp = Manatoo.put(url, attrs)
89
+ JSON.parse(resp.body).to_snake_keys
90
+ end
91
+
92
+ def update(attrs)
93
+ snake_cased_json = Task.update(id, attrs)
94
+ handle_snake_cased_json(snake_cased_json)
95
+ end
96
+
97
+ # task_id, status REQUIRED
98
+ def self.update_status(task_id, status)
99
+ url = "tasks/#{task_id}/status"
100
+ resp = Manatoo.put(url, {
101
+ status: status
102
+ })
103
+ JSON.parse(resp.body).to_snake_keys
104
+ end
105
+
106
+ def update_status(status)
107
+ snake_cased_json = Task.update_status(id, status)
108
+ handle_snake_cased_json(snake_cased_json)
109
+ end
110
+
111
+ # labels should not be empty, should be array
112
+ def self.add_labels(task_id, labels)
113
+ url = "tasks/#{task_id}/labels"
114
+ resp = Manatoo.post(url, {
115
+ labels: labels
116
+ })
117
+ JSON.parse(resp.body).to_snake_keys
118
+ end
119
+
120
+ def add_labels(labels)
121
+ snake_cased_json = Task.add_labels(id, labels)
122
+ handle_snake_cased_json(snake_cased_json)
123
+ end
124
+
125
+ # weight REQUIRED, should be integer
126
+ def self.add_weight(task_id, weight)
127
+ url = "tasks/#{task_id}/weight"
128
+ resp = Manatoo.post(url, {
129
+ weight: weight
130
+ })
131
+ JSON.parse(resp.body).to_snake_keys
132
+ end
133
+
134
+ def add_weight(weight)
135
+ snake_cased_json = Task.add_weight(id, weight)
136
+ handle_snake_cased_json(snake_cased_json)
137
+ end
138
+
139
+ # users should not be empty, should be array
140
+ def self.add_users(task_id, users)
141
+ url = "tasks/#{task_id}/users"
142
+ resp = Manatoo.post(url, {
143
+ users: users
144
+ })
145
+ JSON.parse(resp.body).to_snake_keys
146
+ end
147
+
148
+ def add_users(users)
149
+ snake_cased_json = Task.add_users(id, weight)
150
+ handle_snake_cased_json(snake_cased_json)
151
+ end
152
+
153
+ # users should not be empty, should be array
154
+ def self.remove_users(task_id, users)
155
+ url = "tasks/#{task_id}/users"
156
+ resp = Manatoo.delete(url, {
157
+ users: users
158
+ })
159
+ JSON.parse(resp.body).to_snake_keys
160
+ end
161
+
162
+ def remove_members(users)
163
+ snake_cased_json = Task.remove_users(id, weight)
164
+ handle_snake_cased_json(snake_cased_json)
165
+ end
166
+ end
167
+ end
metadata ADDED
@@ -0,0 +1,88 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: manatoo
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Derek Zhou
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2018-02-16 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: faraday
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '0.10'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '0.10'
27
+ - !ruby/object:Gem::Dependency
28
+ name: plissken
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: Manatoo is an api first todo list that allows your products to tell you
56
+ what to do
57
+ email: developer@manatoo.io
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - lib/manatoo.rb
63
+ - lib/manatoo/task.rb
64
+ homepage: http://rubygems.org/gems/manatoo
65
+ licenses:
66
+ - MIT
67
+ metadata: {}
68
+ post_install_message:
69
+ rdoc_options: []
70
+ require_paths:
71
+ - lib
72
+ required_ruby_version: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - ">="
75
+ - !ruby/object:Gem::Version
76
+ version: 2.0.0
77
+ required_rubygems_version: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - ">="
80
+ - !ruby/object:Gem::Version
81
+ version: '0'
82
+ requirements: []
83
+ rubyforge_project:
84
+ rubygems_version: 2.5.1
85
+ signing_key:
86
+ specification_version: 4
87
+ summary: Manatoo api ruby bindings
88
+ test_files: []