k_sequencing 0.1.24 → 0.1.25

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 (38) hide show
  1. checksums.yaml +5 -5
  2. data/.gitignore +4 -0
  3. data/.rubocop.yml +23 -0
  4. data/Gemfile +1 -1
  5. data/Gemfile.lock +27 -1
  6. data/README.md +17 -9
  7. data/Rakefile +12 -2
  8. data/docs/documentation.md +32 -35
  9. data/k_sequencing.gemspec +8 -6
  10. data/lib/k_sequencing.rb +0 -4
  11. data/lib/k_sequencing/client.rb +1 -3
  12. data/lib/k_sequencing/client_response.rb +4 -9
  13. data/lib/k_sequencing/connection.rb +24 -16
  14. data/lib/k_sequencing/faraday/raise_http_exception.rb +2 -8
  15. data/lib/k_sequencing/predictions.rb +2 -4
  16. data/lib/k_sequencing/version.rb +1 -1
  17. data/test/fixtures/image_choice/all.json +27 -0
  18. data/test/fixtures/image_choice/create.json +25 -0
  19. data/test/fixtures/image_closed_question/all.json +21 -0
  20. data/test/fixtures/image_closed_question/create.json +19 -0
  21. data/test/fixtures/image_message/all.json +22 -0
  22. data/test/fixtures/image_message/create.json +20 -0
  23. data/test/fixtures/image_photo_tag/all.json +22 -0
  24. data/test/fixtures/image_photo_tag/create.json +20 -0
  25. data/test/fixtures/prediction/all.json +21 -0
  26. data/test/fixtures/prediction/create.json +19 -0
  27. data/test/helper/file_reader.rb +11 -0
  28. data/test/k_sequencing/client_response_test.rb +29 -0
  29. data/test/k_sequencing/client_test.rb +189 -0
  30. data/test/k_sequencing/connection_test.rb +85 -0
  31. data/test/k_sequencing/image_choices_test.rb +38 -0
  32. data/test/k_sequencing/image_closed_questions_test.rb +38 -0
  33. data/test/k_sequencing/image_messages_test.rb +38 -0
  34. data/test/k_sequencing/image_photo_tags_test.rb +38 -0
  35. data/test/k_sequencing/predictions_test.rb +39 -0
  36. data/test/k_sequencing_test.rb +39 -0
  37. data/test/test_helper.rb +41 -0
  38. metadata +101 -17
@@ -0,0 +1,19 @@
1
+ {
2
+ "data": {
3
+ "image": {
4
+ "id": "5a44671ab3957c2ab5c33326",
5
+ "answer": "approved",
6
+ "credit_charged": 0,
7
+ "custom_id": null,
8
+ "data": "https://s3-ap-northeast-1.amazonaws.com/marinchat/uploads/user/6951939/profile_icon/veOSbIBCR.jpg",
9
+ "postback_url": "http://localhost:3000/projects",
10
+ "processed_at": "2017-11-16T10:10:56.282+07:00",
11
+ "project_id": 93,
12
+ "status": "processed"
13
+ }
14
+ },
15
+ "meta": {
16
+ "code": 200,
17
+ "message": "success"
18
+ }
19
+ }
@@ -0,0 +1,11 @@
1
+ class FileReader
2
+ attr_reader :file
3
+
4
+ def initialize(file)
5
+ @file = file
6
+ end
7
+
8
+ def read_json
9
+ JSON.parse(File.read(file))
10
+ end
11
+ end
@@ -0,0 +1,29 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'test_helper'
4
+
5
+ module KSequencing
6
+ class ResponseTest < Minitest::Test
7
+ def test_new_success_response
8
+ response = Response.new('response', 200, 'success', 'meta', 5)
9
+ refute_nil(response)
10
+ refute_nil(response.data)
11
+ assert_equal(true, response.successful?)
12
+ assert_equal('response', response.data)
13
+ assert_equal('success', response.message)
14
+ assert_equal('meta', response.meta)
15
+ assert_equal(5, response.total)
16
+ end
17
+
18
+ def test_failure_response
19
+ response = Response.new('response', 500, 'failure', 'meta')
20
+ refute_nil(response)
21
+ refute_nil(response.data)
22
+ assert_equal(false, response.successful?)
23
+ assert_equal('response', response.data)
24
+ assert_equal('failure', response.message)
25
+ assert_equal('meta', response.meta)
26
+ assert_equal(0, response.total)
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,189 @@
1
+ require 'test_helper'
2
+
3
+ module KSequencing
4
+ class ClientTest < TestBase
5
+ def test_create_image_choices
6
+ stub_request(:post, IMAGE_CHOICES_URL)
7
+ .with(headers: { Authorization: options[:token] })
8
+ .to_return(body: JSON.generate(image_choice), status: 200)
9
+ response = client.create_image_choices(options)
10
+ assert_instance_of(Response, response)
11
+ assert_equal(200, response.status)
12
+ refute_nil(response.data)
13
+ refute_nil(response.meta)
14
+ end
15
+
16
+ def test_create_image_closed_question
17
+ stub_request(:post, IMAGE_CLOSED_QUESTIONS_URL)
18
+ .with(headers: { Authorization: options[:token] })
19
+ .to_return(body: JSON.generate(image_closed_question), status: 200)
20
+ response = client.create_image_closed_questions(options)
21
+ assert_instance_of(Response, response)
22
+ assert_equal(200, response.status)
23
+ refute_nil(response.data)
24
+ refute_nil(response.meta)
25
+ end
26
+
27
+ def test_create_image_messages
28
+ stub_request(:post, IMAGE_MESSAGES_URL)
29
+ .with(headers: { Authorization: options[:token] })
30
+ .to_return(body: JSON.generate(image_message), status: 200)
31
+ response = client.create_image_messages(options)
32
+ assert_instance_of(Response, response)
33
+ assert_equal(200, response.status)
34
+ refute_nil(response.data)
35
+ refute_nil(response.meta)
36
+ end
37
+
38
+ def test_create_image_photo_tag
39
+ stub_request(:post, IMAGE_PHOTO_TAGS)
40
+ .with(headers: { Authorization: options[:token] })
41
+ .to_return(body: JSON.generate(image_photo_tags), status: 200)
42
+ response = client.create_image_photo_tags(options)
43
+ assert_instance_of(Response, response)
44
+ assert_equal(200, response.status)
45
+ refute_nil(response.data)
46
+ refute_nil(response.meta)
47
+ end
48
+
49
+ def test_create_prediction
50
+ stub_request(:post, PREDICTIONS_URL)
51
+ .with(headers: { Authorization: options[:token] })
52
+ .to_return(body: JSON.generate(prediction), status: 200)
53
+ response = client.create_prediction(options)
54
+ assert_instance_of(Response, response)
55
+ assert_equal(200, response.status)
56
+ refute_nil(response.data)
57
+ refute_nil(response.meta)
58
+ end
59
+
60
+ def test_list_image_choice
61
+ stub_request(:get, IMAGE_CHOICES_URL)
62
+ .with(query: { token: options[:token] })
63
+ .to_return(body: JSON.generate(image_choices), status: 200)
64
+ response = client.get_image_choice(options)
65
+ assert_instance_of(Response, response)
66
+ assert_equal(200, response.status)
67
+ refute_nil(response.data)
68
+ refute_nil(response.meta)
69
+ end
70
+
71
+ def test_list_image_closed_question
72
+ stub_request(:get, IMAGE_CLOSED_QUESTIONS_URL)
73
+ .with(query: { token: options[:token] })
74
+ .to_return(body: JSON.generate(image_closed_questions), status: 200)
75
+ response = client.get_image_closed_question(options)
76
+ assert_instance_of(Response, response)
77
+ assert_equal(200, response.status)
78
+ refute_nil(response.data)
79
+ refute_nil(response.meta)
80
+ end
81
+
82
+ def test_list_image_message
83
+ stub_request(:get, IMAGE_MESSAGES_URL)
84
+ .with(query: { token: options[:token] })
85
+ .to_return(body: JSON.generate(image_messages), status: 200)
86
+ response = client.get_image_message(options)
87
+ assert_instance_of(Response, response)
88
+ assert_equal(200, response.status)
89
+ refute_nil(response.data)
90
+ refute_nil(response.meta)
91
+ end
92
+
93
+ def test_list_image_photo_tag
94
+ stub_request(:get, IMAGE_PHOTO_TAGS)
95
+ .with(query: { token: options[:token] })
96
+ .to_return(body: JSON.generate(image_photo_tags), status: 200)
97
+ response = client.get_image_photo_tag(options)
98
+ assert_instance_of(Response, response)
99
+ assert_equal(200, response.status)
100
+ refute_nil(response.data)
101
+ refute_nil(response.meta)
102
+ end
103
+
104
+ def test_list_prediction
105
+ stub_request(:get, PREDICTIONS_URL)
106
+ .with(query: { token: options[:token] })
107
+ .to_return(body: JSON.generate(prediction), status: 200)
108
+ response = client.get_prediction(options)
109
+ assert_instance_of(Response, response)
110
+ assert_equal(200, response.status)
111
+ refute_nil(response.data)
112
+ refute_nil(response.meta)
113
+ end
114
+
115
+ def test_find_image_choice
116
+ stub_request(:get, IMAGE_CHOICE_URL)
117
+ .with(query: { token: options[:token] })
118
+ .to_return(body: JSON.generate(image_choice), status: 200)
119
+ response = client.find_by_id_image_choice(options)
120
+ assert_instance_of(Response, response)
121
+ assert_equal(200, response.status)
122
+ refute_nil(response.data)
123
+ refute_nil(response.meta)
124
+ end
125
+
126
+ def test_find_image_closed_question
127
+ stub_request(:get, IMAGE_CLOSED_QUESTION_URL)
128
+ .with(query: { token: options[:token] })
129
+ .to_return(body: JSON.generate(image_closed_question), status: 200)
130
+ response = client.find_by_id_image_closed_question(options)
131
+ assert_instance_of(Response, response)
132
+ assert_equal(200, response.status)
133
+ refute_nil(response.data)
134
+ refute_nil(response.meta)
135
+ end
136
+
137
+ def test_find_image_message
138
+ stub_request(:get, IMAGE_MESSAGE_URL)
139
+ .with(query: { token: options[:token] })
140
+ .to_return(body: JSON.generate(image_message), status: 200)
141
+ response = client.find_by_id_image_message(options)
142
+ assert_instance_of(Response, response)
143
+ assert_equal(200, response.status)
144
+ refute_nil(response.data)
145
+ refute_nil(response.meta)
146
+ end
147
+
148
+ def test_find_photo_tag
149
+ stub_request(:get, IMAGE_PHOTO_TAG)
150
+ .with(query: { token: options[:token] })
151
+ .to_return(body: JSON.generate(image_photo_tag), status: 200)
152
+ response = client.find_by_id_image_photo_tag(options)
153
+ assert_instance_of(Response, response)
154
+ assert_equal(200, response.status)
155
+ refute_nil(response.data)
156
+ refute_nil(response.meta)
157
+ end
158
+
159
+ def test_find_prediction
160
+ mock_id = '1'
161
+ stub_request(:get, PREDICTIONS_URL + '/' + mock_id)
162
+ .with(query: { token: options[:token] })
163
+ .to_return(body: JSON.generate(prediction), status: 200)
164
+ response = client.find_by_id_prediction(mock_id, options)
165
+ assert_instance_of(Response, response)
166
+ assert_equal(200, response.status)
167
+ refute_nil(response.data)
168
+ refute_nil(response.meta)
169
+ end
170
+
171
+ def test_find_image
172
+ mock_id = '1'
173
+ stub_request(:get, IMAGE_URL + '/' + mock_id)
174
+ .with(query: { token: options[:token] })
175
+ .to_return(body: JSON.generate(prediction), status: 200)
176
+ response = client.find_image(mock_id, options)
177
+ assert_instance_of(Response, response)
178
+ assert_equal(200, response.status)
179
+ refute_nil(response.data)
180
+ refute_nil(response.meta)
181
+ end
182
+
183
+ private
184
+
185
+ def client
186
+ @client ||= KSequencing.client
187
+ end
188
+ end
189
+ end
@@ -0,0 +1,85 @@
1
+ require 'test_helper'
2
+
3
+ module KSequencing
4
+ class ConnectionTest < Minitest::Test
5
+ def test_get_success
6
+ stub_request(:get, 'http://localhost:3000')
7
+ .to_return(body: JSON.generate(data: 'foo', meta: { message: 'success', code: 200 }), status: 200)
8
+ response = connection.get('http://localhost:3000', {})
9
+ assert_instance_of(Response, response)
10
+ assert_equal(200, response.status)
11
+ assert_equal('success', response.message)
12
+ assert_equal('success', response.meta['message'])
13
+ assert_equal(200, response.status)
14
+ end
15
+
16
+ def test_get_failed
17
+ stub_request(:get, 'http://localhost:3000')
18
+ .to_return(body: JSON.generate(data: '', meta: { message: 'Internal Server Error', code: 500 }), status: 500)
19
+ response = connection.get('http://localhost:3000', {})
20
+ assert_instance_of(Response, response)
21
+ assert_equal('500', response.status)
22
+ end
23
+
24
+ def test_post_success
25
+ stub_request(:post, 'http://localhost:3000')
26
+ .to_return(body: JSON.generate(data: 'foo', meta: { code: 200 }), status: 200)
27
+ response = connection.post('http://localhost:3000', {})
28
+ assert_instance_of(Response, response)
29
+ assert_equal(200, response.status)
30
+ end
31
+
32
+ def test_post_failed
33
+ stub_request(:post, 'http://localhost:3000')
34
+ .to_return(body: JSON.generate(data: '', meta: { code: 500, message: 'Internal Server Error' }), status: 500)
35
+ response = connection.post('http://localhost:3000', {})
36
+ assert_instance_of(Response, response)
37
+ assert_equal('500', response.status)
38
+ end
39
+
40
+ def test_connection_failed
41
+ stub_request(:get, 'http://localhost:3000')
42
+ .to_raise(Faraday::ConnectionFailed.new('connection failed'))
43
+ response = connection.get('http://localhost:3000')
44
+ assert_instance_of(Response, response)
45
+ end
46
+
47
+ def test_bad_request_status
48
+ stub_request(:get, 'http://localhost:3000')
49
+ .to_return(status: 400, body: JSON.generate(data: '', meta: { message: 'bad request', code: 400 }))
50
+ response = connection.get('http://localhost:3000', {})
51
+ assert_instance_of(Response, response)
52
+ assert_equal('400', response.status)
53
+ end
54
+
55
+ def test_forbidden_status
56
+ stub_request(:get, 'http://localhost:3000')
57
+ .to_return(body: JSON.generate(data: '', meta: { message: 'forbidden', code: 403 }), status: 403)
58
+ response = connection.get('http://localhost:3000', {})
59
+ assert_instance_of(Response, response)
60
+ assert_equal('403', response.status)
61
+ end
62
+
63
+ def test_not_found_status
64
+ stub_request(:get, 'http://localhost:3000')
65
+ .to_return(body: JSON.generate(data: '', meta: { message: 'not found', code: 404 }), status: 404)
66
+ response = connection.get('http://localhost:3000', {})
67
+ assert_instance_of(Response, response)
68
+ assert_equal('404', response.status)
69
+ end
70
+
71
+ def test_edge_case_error
72
+ stub_request(:get, 'http://localhost:3000')
73
+ .to_return(body: JSON.generate(data: '', meta: { message: 'version not supported', code: 505 }), status: 505)
74
+ response = connection.get('http://localhost:3000', {})
75
+ assert_instance_of(Response, response)
76
+ assert_equal('505', response.status)
77
+ end
78
+
79
+ private
80
+
81
+ def connection
82
+ @connection ||= KSequencing::Connection.new
83
+ end
84
+ end
85
+ end
@@ -0,0 +1,38 @@
1
+ require 'test_helper'
2
+
3
+ module KSequencing
4
+ class ImageChoiceTest < TestBase
5
+ def test_all
6
+ stub_request(:get, IMAGE_CHOICES_URL)
7
+ .with(query: { token: options[:token] })
8
+ .to_return(body: JSON.generate(image_choices), status: 200)
9
+ response = ImageChoice.new.all(options)
10
+ assert_instance_of(Response, response)
11
+ assert_equal(200, response.status)
12
+ refute_nil(response.data)
13
+ refute_nil(response.meta)
14
+ end
15
+
16
+ def test_create
17
+ stub_request(:post, IMAGE_CHOICES_URL)
18
+ .with(headers: { Authorization: options[:token] })
19
+ .to_return(body: JSON.generate(image_choice), status: 200)
20
+ response = ImageChoice.new.create(options)
21
+ assert_instance_of(Response, response)
22
+ assert_equal(200, response.status)
23
+ refute_nil(response.data)
24
+ refute_nil(response.meta)
25
+ end
26
+
27
+ def test_find
28
+ stub_request(:get, IMAGE_CHOICE_URL)
29
+ .with(query: { token: options[:token] })
30
+ .to_return(body: JSON.generate(image_choice), status: 200)
31
+ response = ImageChoice.new.find_by(options)
32
+ assert_instance_of(Response, response)
33
+ assert_equal(200, response.status)
34
+ refute_nil(response.data)
35
+ refute_nil(response.meta)
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,38 @@
1
+ require 'test_helper'
2
+
3
+ module KSequencing
4
+ class ImageClosedQuestionTest < TestBase
5
+ def test_all
6
+ stub_request(:get, IMAGE_CLOSED_QUESTIONS_URL)
7
+ .with(query: { token: options[:token] })
8
+ .to_return(body: JSON.generate(image_closed_questions), status: 200)
9
+ response = ImageClosedQuestion.new.all(options)
10
+ assert_instance_of(Response, response)
11
+ assert_equal(200, response.status)
12
+ refute_nil(response.data)
13
+ refute_nil(response.meta)
14
+ end
15
+
16
+ def test_create
17
+ stub_request(:post, IMAGE_CLOSED_QUESTIONS_URL)
18
+ .with(headers: { Authorization: options[:token] })
19
+ .to_return(body: JSON.generate(image_closed_question), status: 200)
20
+ response = ImageClosedQuestion.new.create(options)
21
+ assert_instance_of(Response, response)
22
+ assert_equal(200, response.status)
23
+ refute_nil(response.data)
24
+ refute_nil(response.meta)
25
+ end
26
+
27
+ def test_find
28
+ stub_request(:get, IMAGE_CLOSED_QUESTION_URL)
29
+ .with(query: { token: options[:token] })
30
+ .to_return(body: JSON.generate(image_closed_question), status: 200)
31
+ response = ImageClosedQuestion.new.find_by(options)
32
+ assert_instance_of(Response, response)
33
+ assert_equal(200, response.status)
34
+ refute_nil(response.data)
35
+ refute_nil(response.meta)
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,38 @@
1
+ require 'test_helper'
2
+
3
+ module KSequencing
4
+ class ImageMessageTest < TestBase
5
+ def test_all
6
+ stub_request(:get, IMAGE_MESSAGES_URL)
7
+ .with(query: { token: options[:token] })
8
+ .to_return(body: JSON.generate(image_messages), status: 200)
9
+ response = ImageMessage.new.all(options)
10
+ assert_instance_of(Response, response)
11
+ assert_equal(200, response.status)
12
+ refute_nil(response.data)
13
+ refute_nil(response.meta)
14
+ end
15
+
16
+ def test_create
17
+ stub_request(:post, IMAGE_MESSAGES_URL)
18
+ .with(headers: { Authorization: options[:token] })
19
+ .to_return(body: JSON.generate(image_message), status: 200)
20
+ response = ImageMessage.new.create(options)
21
+ assert_instance_of(Response, response)
22
+ assert_equal(200, response.status)
23
+ refute_nil(response.data)
24
+ refute_nil(response.meta)
25
+ end
26
+
27
+ def test_find
28
+ stub_request(:get, IMAGE_MESSAGE_URL)
29
+ .with(query: { token: options[:token] })
30
+ .to_return(body: JSON.generate(image_message), status: 200)
31
+ response = ImageMessage.new.find_by(options)
32
+ assert_instance_of(Response, response)
33
+ assert_equal(200, response.status)
34
+ refute_nil(response.data)
35
+ refute_nil(response.meta)
36
+ end
37
+ end
38
+ end