adn-reborn 0.4.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (48) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +20 -0
  3. data/.travis.yml +8 -0
  4. data/CHANGELOG.md +34 -0
  5. data/Gemfile +8 -0
  6. data/Gemfile.lock +25 -0
  7. data/LICENSE +16 -0
  8. data/README.md +19 -0
  9. data/Rakefile +17 -0
  10. data/adn-reborn.gemspec +19 -0
  11. data/lib/adn-reborn/api/file.rb +36 -0
  12. data/lib/adn-reborn/api/filter.rb +10 -0
  13. data/lib/adn-reborn/api/message.rb +29 -0
  14. data/lib/adn-reborn/api/post.rb +51 -0
  15. data/lib/adn-reborn/api/response.rb +19 -0
  16. data/lib/adn-reborn/api/stream.rb +10 -0
  17. data/lib/adn-reborn/api/subscription.rb +11 -0
  18. data/lib/adn-reborn/api/token.rb +12 -0
  19. data/lib/adn-reborn/api/user.rb +19 -0
  20. data/lib/adn-reborn/api.rb +62 -0
  21. data/lib/adn-reborn/constants.rb +18 -0
  22. data/lib/adn-reborn/file.rb +62 -0
  23. data/lib/adn-reborn/message.rb +76 -0
  24. data/lib/adn-reborn/post.rb +76 -0
  25. data/lib/adn-reborn/recipes/broadcast_message_builder.rb +103 -0
  26. data/lib/adn-reborn/recipes.rb +5 -0
  27. data/lib/adn-reborn/user.rb +103 -0
  28. data/lib/adn-reborn/version.rb +9 -0
  29. data/lib/adn-reborn.rb +69 -0
  30. data/spec/adn-reborn/api/file_spec.rb +73 -0
  31. data/spec/adn-reborn/api/filter_spec.rb +9 -0
  32. data/spec/adn-reborn/api/message_spec.rb +73 -0
  33. data/spec/adn-reborn/api/post_spec.rb +147 -0
  34. data/spec/adn-reborn/api/response_spec.rb +13 -0
  35. data/spec/adn-reborn/api/stream_spec.rb +9 -0
  36. data/spec/adn-reborn/api/subscription_spec.rb +9 -0
  37. data/spec/adn-reborn/api/token_spec.rb +23 -0
  38. data/spec/adn-reborn/api/user_spec.rb +27 -0
  39. data/spec/adn-reborn/file_spec.rb +103 -0
  40. data/spec/adn-reborn/message_spec.rb +152 -0
  41. data/spec/adn-reborn/post_spec.rb +147 -0
  42. data/spec/adn-reborn/recipes/broadcast_message_builder_spec.rb +179 -0
  43. data/spec/adn-reborn/user_spec.rb +115 -0
  44. data/spec/adn-reborn_spec.rb +42 -0
  45. data/spec/fixtures/post.json +88 -0
  46. data/spec/fixtures/user.json +70 -0
  47. data/spec/spec_helper.rb +30 -0
  48. metadata +141 -0
@@ -0,0 +1,9 @@
1
+ # encoding: UTF-8
2
+
3
+ require_relative '../../spec_helper'
4
+
5
+ describe ADNReborn::API::Subscription do
6
+ subject { ADNReborn::API::Subscription }
7
+
8
+ # Not Implemented Yet
9
+ end
@@ -0,0 +1,23 @@
1
+ # encoding: UTF-8
2
+
3
+ require_relative '../../spec_helper'
4
+
5
+ describe ADNReborn::API::Token do
6
+ subject { ADNReborn::API::Token }
7
+
8
+ describe "current" do
9
+ it "does not return the current token if it has an error" do
10
+ ADNReborn::API.stub(
11
+ :get, ADNReborn::API::Response.new("error" => "error message")) do
12
+ subject.current.must_equal nil
13
+ end
14
+ end
15
+
16
+ it "retrieves the current token" do
17
+ ADNReborn::API.stub(
18
+ :get, ADNReborn::API::Response.new("data" => "example_token")) do
19
+ subject.current.must_equal "example_token"
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,27 @@
1
+ # encoding: UTF-8
2
+
3
+ require_relative '../../spec_helper'
4
+
5
+ describe ADNReborn::API::User do
6
+ subject { ADNReborn::API::User }
7
+
8
+ let(:base_path) { '/stream/0/users/' }
9
+
10
+ describe "retrieve" do
11
+ it "retrieves the user" do
12
+ arg(:get) { subject.retrieve(55).must_equal base_path + "55" }
13
+ end
14
+ end
15
+
16
+ describe "following" do
17
+ it "retrieves the following list" do
18
+ arg(:get) { subject.following(44).must_equal base_path + "44/following" }
19
+ end
20
+ end
21
+
22
+ describe "followers" do
23
+ it "retrieves the followers list" do
24
+ arg(:get) { subject.following(66).must_equal base_path + "66/following" }
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,103 @@
1
+ # encoding: UTF-8
2
+
3
+ require_relative '../spec_helper'
4
+
5
+ def d(data)
6
+ { 'data' => data }
7
+ end
8
+
9
+ describe ADNReborn::File do
10
+ subject { ADNReborn::File }
11
+
12
+ let(:file) { subject.new(file_data) }
13
+ let(:example_user) { { username: 'peterhellberg' } }
14
+ let(:file_data) {{
15
+ created_at: '1999-12-31T23:59:59Z',
16
+ id: 10001,
17
+ user: example_user,
18
+ file_token: "abc123",
19
+ sha1: "d5e7c7a123108739a28d721eb34133600a70a7fa",
20
+ name: "foo.txt",
21
+ source: { link: "https://alpha.app.net", name: "Alpha" },
22
+ url: "https://adn-uf-01.s3.amazonaws.com/adn-uf-01/really_long_filepath",
23
+ kind: "other",
24
+ total_size: 2248,
25
+ url_expires: "2013-12-14T02:00:00Z",
26
+ size: 2248,
27
+ type: "net.app.testing",
28
+ public: false,
29
+ mime_type: "text/plain",
30
+ complete: true
31
+ }}
32
+
33
+ describe "upload_file" do
34
+ it "creates a message via the API" do
35
+ ADNReborn::API::File.stub(:create, ADNReborn::API::Response.new(d(file_data))) do
36
+ m = subject.upload_file("foo.txt", {type: "net.app.testing"})
37
+ m.type.must_equal 'net.app.testing'
38
+ m.name.must_equal 'foo.txt'
39
+ end
40
+ end
41
+ end
42
+
43
+ describe "initialize" do
44
+ it "populates the accessors using the passed in hash" do
45
+ m = subject.new id: 123
46
+ m.id.must_equal 123
47
+ end
48
+
49
+ it "populates the accessors based on the passed in id" do
50
+ ADNReborn::API::File.stub(:by_id, ->(i){ d({ "total_size" => 1234}) }) do
51
+ f = subject.new 123
52
+ f.total_size.must_equal 1234
53
+ end
54
+ end
55
+ end
56
+
57
+ describe "details" do
58
+ it "returns the details for the file" do
59
+ file.details.keys.must_equal [
60
+ "created_at",
61
+ "id",
62
+ "user",
63
+ "file_token",
64
+ "sha1",
65
+ "name",
66
+ "source",
67
+ "url",
68
+ "kind",
69
+ "total_size",
70
+ "url_expires",
71
+ "size",
72
+ "type",
73
+ "public",
74
+ "mime_type",
75
+ "complete"
76
+ ]
77
+ end
78
+ end
79
+
80
+ describe "created_at" do
81
+ it "returns the date and time the message was created" do
82
+ file.created_at.to_s.must_equal '1999-12-31T23:59:59+00:00'
83
+ end
84
+ end
85
+
86
+ describe "user" do
87
+ it "returns the user" do
88
+ file.user.username.must_equal 'peterhellberg'
89
+ end
90
+ end
91
+
92
+ describe "delete" do
93
+ it "deletes the file via the API and returns the file" do
94
+ delete_stub = ->(id){
95
+ ADNReborn::API::Response.new("data" => { "id" => id })
96
+ }
97
+
98
+ ADNReborn::API::File.stub(:delete, delete_stub) do
99
+ file.delete.id.must_equal 10001
100
+ end
101
+ end
102
+ end
103
+ end
@@ -0,0 +1,152 @@
1
+ # encoding: UTF-8
2
+
3
+ require_relative '../spec_helper'
4
+
5
+ def d(data)
6
+ { 'data' => data }
7
+ end
8
+
9
+ describe ADNReborn::Message do
10
+ subject { ADNReborn::Message }
11
+
12
+ let(:empty_message) do
13
+ subject.new({})
14
+ end
15
+
16
+ let(:message_with_id) do
17
+ subject.new({
18
+ channel_id: '456',
19
+ message_id: '123'
20
+ })
21
+ end
22
+
23
+ let(:msg) do
24
+ subject.new(message_data)
25
+ end
26
+
27
+ let(:example_user) do
28
+ {
29
+ username: 'peterhellberg'
30
+ }
31
+ end
32
+
33
+ let(:message_data) {
34
+ {
35
+ created_at: '1999-12-31T23:59:59Z',
36
+ entities: {},
37
+ html: '<b>The sky above the port…</b>',
38
+ id: 10001,
39
+ channel_id: 65432,
40
+ num_replies: 1,
41
+ reply_to: 66,
42
+ source: { link: "https://alpha.app.net", name: "Alpha" },
43
+ text: "The sky above the port…",
44
+ thread_id: "301856",
45
+ user: example_user
46
+ }
47
+ }
48
+
49
+ describe "send_message" do
50
+ it "creates a message via the API" do
51
+ api_response = ADNReborn::API::Response.new(d(message_data))
52
+
53
+ ADNReborn::API::Message.stub(:create, api_response) do
54
+ m = subject.send_message({})
55
+ m.text.must_equal 'The sky above the port…'
56
+ end
57
+ end
58
+ end
59
+
60
+ describe "initialize" do
61
+ it "populates the accessors using the passed in hash" do
62
+ m = subject.new text: 'foo', id: 123, channel_id: 456
63
+ m.text.must_equal 'foo'
64
+ end
65
+
66
+ # TODO: Change this behavior (Add a find method instead)
67
+ it "populates the accessors based on the passed in id" do
68
+ by_id = ->(c_id, i){ d({ "text" => 'bar'}) }
69
+
70
+ ADNReborn::API::Message.stub(:by_id, by_id) do
71
+ m = subject.new id: 123, channel_id: 456
72
+ m.text.must_equal 'bar'
73
+ end
74
+ end
75
+ end
76
+
77
+ describe "details" do
78
+ it "returns the details for the message" do
79
+ msg.details.keys.must_equal [
80
+ "created_at", "entities", "html",
81
+ "id", "channel_id", "num_replies", "reply_to",
82
+ "source", "text", "thread_id", "user"
83
+ ]
84
+ end
85
+
86
+ it "returns the message from the api if it has no id" do
87
+ by_id = ->(c_id, i){ d({ "id" => i, "channel_id" => c_id}) }
88
+
89
+ ADNReborn::API::Message.stub(:by_id, by_id) do
90
+ message_with_id.details.must_equal({
91
+ "data" => {
92
+ "id" => '123',
93
+ "channel_id" => '456'
94
+ }
95
+ })
96
+ end
97
+ end
98
+ end
99
+
100
+ describe "created_at" do
101
+ it "returns the date and time the message was created" do
102
+ msg.created_at.to_s.must_equal '1999-12-31T23:59:59+00:00'
103
+ end
104
+ end
105
+
106
+ describe "user" do
107
+ it "returns the user" do
108
+ msg.user.username.must_equal 'peterhellberg'
109
+ end
110
+ end
111
+
112
+ describe "delete" do
113
+ it "deletes the message via the API and returns the message" do
114
+ delete_stub = ->(c_id, id){
115
+ ADNReborn::API::Response.new("data" => {
116
+ "channel_id" => c_id,
117
+ "id" => id
118
+ })
119
+ }
120
+
121
+ ADNReborn::API::Message.stub(:delete, delete_stub) do
122
+ msg.delete.id.must_equal 10001
123
+ end
124
+ end
125
+ end
126
+
127
+ describe "set_values" do
128
+ it "sets values with accessors" do
129
+ m = empty_message
130
+
131
+ m.set_values(message_data)
132
+
133
+ m.entities.must_equal message_data[:entities]
134
+ m.html.must_equal message_data[:html]
135
+ m.id.must_equal message_data[:id]
136
+ m.num_replies.must_equal message_data[:num_replies]
137
+ m.reply_to.must_equal message_data[:reply_to]
138
+ m.source.must_equal message_data[:source]
139
+ m.text.must_equal message_data[:text]
140
+ m.thread_id.must_equal message_data[:thread_id]
141
+
142
+ m.created_at.must_equal DateTime.parse(message_data[:created_at])
143
+
144
+ m.user.username.must_equal message_data[:user][:username]
145
+ end
146
+
147
+ it "does not set arbitrary values" do
148
+ m = subject.new foo: 'bar'
149
+ -> { m.foo }.must_raise NoMethodError
150
+ end
151
+ end
152
+ end
@@ -0,0 +1,147 @@
1
+ # encoding: UTF-8
2
+
3
+ require_relative '../spec_helper'
4
+
5
+ def d(data)
6
+ { 'data' => data }
7
+ end
8
+
9
+ describe ADNReborn::Post do
10
+ subject { ADNReborn::Post }
11
+
12
+ let(:empty_post) { subject.new({}) }
13
+ let(:post_with_id) { subject.new({ post_id: 123 }) }
14
+ let(:post) { subject.new(post_data) }
15
+
16
+ let(:example_user) { { username: 'peterhellberg' } }
17
+ let(:post_data) {
18
+ {
19
+ created_at: '1999-12-31T23:59:59Z',
20
+ entities: {},
21
+ html: '<b>The sky above the port…</b>',
22
+ id: 10001,
23
+ num_replies: 1,
24
+ reply_to: 66,
25
+ source: { link: "https://alpha.app.net", name: "Alpha" },
26
+ text: "The sky above the port…",
27
+ thread_id: "301856",
28
+ user: example_user
29
+ }
30
+ }
31
+
32
+ describe "send_post" do
33
+ it "creates a post via the API" do
34
+ ADNReborn::API::Post.stub(:create, ADNReborn::API::Response.new(d(post_data))) do
35
+ p = subject.send_post({})
36
+ p.text.must_equal 'The sky above the port…'
37
+ end
38
+ end
39
+ end
40
+
41
+ describe "initialize" do
42
+ it "populates the accessors using the passed in hash" do
43
+ p = subject.new text: 'foo', id: 123
44
+ p.text.must_equal 'foo'
45
+ end
46
+
47
+ # TODO: Change this behavior (Add a find method instead)
48
+ it "populates the accessors based on the passed in id" do
49
+ ADNReborn::API::Post.stub(:by_id, ->(i){ d({ "text" => 'bar'}) }) do
50
+ p = subject.new 456
51
+ p.text.must_equal 'bar'
52
+ end
53
+ end
54
+ end
55
+
56
+ describe "details" do
57
+ it "returns the details for the post" do
58
+ post.details.keys.must_equal [
59
+ "created_at", "entities", "html",
60
+ "id", "num_replies", "reply_to",
61
+ "source", "text", "thread_id", "user"
62
+ ]
63
+ end
64
+
65
+ it "returns the post from the api if it has no id" do
66
+
67
+ ADNReborn::API::Post.stub(:by_id, ->(i){ d({ "id" => i}) }) do
68
+ post_with_id.details.
69
+ must_equal({ "data" => { "id" => 123 } })
70
+ end
71
+ end
72
+ end
73
+
74
+ describe "created_at" do
75
+ it "returns the date and time the post was created" do
76
+ post.created_at.to_s.must_equal '1999-12-31T23:59:59+00:00'
77
+ end
78
+ end
79
+
80
+ describe "user" do
81
+ it "returns the user" do
82
+ post.user.username.must_equal 'peterhellberg'
83
+ end
84
+ end
85
+
86
+ describe "reply_to_post" do
87
+ it "returns the post that was replied to" do
88
+ ADNReborn::API::Post.stub(:by_id, ->(id){
89
+ ADNReborn::API::Response.new("data" => { "id" => id })
90
+ }) do
91
+ post.reply_to_post.id.must_equal 66
92
+ end
93
+ end
94
+ end
95
+
96
+ describe "replies" do
97
+ it "returns a list of posts" do
98
+ response = ADNReborn::API::Response.new(
99
+ "data" => [{ text: "foo"}, {text: "bar"}])
100
+
101
+ ADNReborn::API::Post.stub(:replies, ->(*a){ response }) do
102
+ r = post.replies
103
+ r.size.must_equal 2
104
+ r[0].text.must_equal 'foo'
105
+ r[1].text.must_equal 'bar'
106
+ end
107
+ end
108
+ end
109
+
110
+ describe "delete" do
111
+ it "deletes the post via the API and returns a new post" do
112
+ delete_stub = ->(id){
113
+ ADNReborn::API::Response.new("data" => { "id" => id*2 })
114
+ }
115
+
116
+ ADNReborn::API::Post.stub(:delete, delete_stub) do
117
+ post.delete.id.must_equal 20002
118
+ end
119
+ end
120
+ end
121
+
122
+ describe "set_values" do
123
+ it "sets values with accessors" do
124
+ p = empty_post
125
+
126
+ p.set_values(post_data)
127
+
128
+ p.entities.must_equal post_data[:entities]
129
+ p.html.must_equal post_data[:html]
130
+ p.id.must_equal post_data[:id]
131
+ p.num_replies.must_equal post_data[:num_replies]
132
+ p.reply_to.must_equal post_data[:reply_to]
133
+ p.source.must_equal post_data[:source]
134
+ p.text.must_equal post_data[:text]
135
+ p.thread_id.must_equal post_data[:thread_id]
136
+
137
+ p.created_at.must_equal DateTime.parse(post_data[:created_at])
138
+
139
+ p.user.username.must_equal post_data[:user][:username]
140
+ end
141
+
142
+ it "does not set arbitrary values" do
143
+ p = subject.new foo: 'bar'
144
+ -> { p.foo }.must_raise NoMethodError
145
+ end
146
+ end
147
+ end
@@ -0,0 +1,179 @@
1
+ # encoding: UTF-8
2
+
3
+ require_relative '../../spec_helper'
4
+
5
+ describe ADNReborn::Recipes::BroadcastMessageBuilder do
6
+ subject { ADNReborn::Recipes::BroadcastMessageBuilder.new }
7
+
8
+ describe "annotations" do
9
+ describe "with a read more link" do
10
+ it "generates a crosspost annotation" do
11
+ subject.headline = "foo"
12
+ subject.read_more_link = "http://app.net"
13
+ subject.annotations.must_equal([
14
+ {
15
+ type: "net.app.core.broadcast.message.metadata",
16
+ value: { subject: "foo" }
17
+ },
18
+ {
19
+ type: "net.app.core.crosspost",
20
+ value: { canonical_url: "http://app.net" }
21
+ }
22
+ ])
23
+ end
24
+ end
25
+
26
+ describe "with only a headline" do
27
+ it "generates a message metadata annotation" do
28
+ subject.headline = "foo"
29
+ subject.annotations.must_equal([
30
+ {
31
+ type: "net.app.core.broadcast.message.metadata",
32
+ value: { subject: "foo" }
33
+ },
34
+ ])
35
+ end
36
+ end
37
+
38
+ describe "with a photo" do
39
+ it "generates an oembed annotation" do
40
+ api_response = ADNReborn::API::Response.new({
41
+ "data" => {
42
+ id: "1",
43
+ file_token: "1234"
44
+ }
45
+ })
46
+
47
+ ADNReborn::API::File.stub(:create, api_response) do
48
+ subject.headline = "foo"
49
+ subject.photo = "foo.jpg"
50
+ subject.annotations.must_equal([
51
+ {
52
+ type: "net.app.core.broadcast.message.metadata",
53
+ value: { subject: "foo" }
54
+ },
55
+ {
56
+ type: "net.app.core.oembed",
57
+ value: { "+net.app.core.file" => {
58
+ file_id: "1",
59
+ file_token: "1234",
60
+ format: "oembed"
61
+ }}
62
+ }
63
+ ])
64
+ end
65
+ end
66
+ end
67
+
68
+ describe "with an attachment" do
69
+ it "generates an attachment annotation" do
70
+ api_response = ADNReborn::API::Response.new({
71
+ "data" => {
72
+ id: "1",
73
+ file_token: "1234"
74
+ }
75
+ })
76
+
77
+ ADNReborn::API::File.stub(:create, api_response) do
78
+ subject.headline = "foo"
79
+ subject.attachment = "foo.txt"
80
+ subject.annotations.must_equal([
81
+ {
82
+ type: "net.app.core.broadcast.message.metadata",
83
+ value: { subject: "foo" }
84
+ },
85
+ {
86
+ type: "net.app.core.attachments",
87
+ value: { "+net.app.core.file_list" => [
88
+ {
89
+ file_id: "1",
90
+ file_token: "1234",
91
+ format: "metadata"
92
+ }
93
+ ]}
94
+ }
95
+ ])
96
+ end
97
+ end
98
+ end
99
+ end
100
+
101
+ describe "message" do
102
+ describe "with text" do
103
+ it "includes the extra text" do
104
+ subject.headline = "foo"
105
+ subject.text = "bar"
106
+ subject.message[:text].must_equal("bar")
107
+ subject.message[:machine_only].must_equal(nil)
108
+ end
109
+ end
110
+
111
+ describe "without text" do
112
+ it "generates a machine only message" do
113
+ subject.headline = "foo"
114
+ subject.message[:text].must_be_nil
115
+ subject.message[:machine_only].must_equal(true)
116
+ end
117
+
118
+ it "includes the annotations" do
119
+ subject.headline = "bar"
120
+ subject.message[:annotations].must_equal([
121
+ {
122
+ type: "net.app.core.broadcast.message.metadata",
123
+ value: { subject: "bar" }
124
+ },
125
+ ])
126
+ end
127
+ end
128
+
129
+ it "allows you to parse links" do
130
+ subject.headline = "foo"
131
+ subject.parse_links = true
132
+ subject.message[:entities].must_equal({
133
+ parse_links: true,
134
+ parse_markdown_links: false
135
+ })
136
+ end
137
+
138
+ it "parses links when you ask for parsing markdown links" do
139
+ subject.headline = "foo"
140
+ subject.parse_markdown_links = true
141
+ subject.message[:entities].must_equal({
142
+ parse_links: true,
143
+ parse_markdown_links: true
144
+ })
145
+ end
146
+ end
147
+
148
+ describe "send" do
149
+ it "calls the api with the message" do
150
+ headline = "foo"
151
+ channel_id = "123"
152
+ response_body = %Q{{
153
+ "meta": {
154
+ "code": 200
155
+ },
156
+ "data": {
157
+ "annotations": [
158
+ {
159
+ "type": "net.app.core.broadcast.message.metadata",
160
+ "value": {"subject": "#{headline}"}
161
+ }
162
+ ],
163
+ "channel_id": "#{channel_id}"
164
+ }
165
+ }}
166
+
167
+ response = OpenStruct.new(:body => response_body)
168
+
169
+ ADNReborn::HTTP.stub(:request, response) do
170
+ subject.headline = headline
171
+ subject.channel_id = channel_id
172
+
173
+ message = subject.send
174
+ message.channel_id.must_equal channel_id
175
+ message.annotations[0]["value"]["subject"].must_equal headline
176
+ end
177
+ end
178
+ end
179
+ end