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
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 1a025eabc1c99ec543b87c5883fde1e1bdc98c2b
4
+ data.tar.gz: 537db2d68599e08bfa4eab2a9909a94b6d86d974
5
+ SHA512:
6
+ metadata.gz: 31e1091b7d4a11341c199dc091afea9a37c873d90b2bcd1f8335be7028469ac0c8caf8eaf4e7ac48593daafa735f04da946c87908f862c7927031d3e74b7e2a3
7
+ data.tar.gz: 93f102ff170e04c03f09c2697c6ed9b483afed152484af519d62f5c80bd6600cfc8b81617cf1406fdbb28c13f677ecd8ebf10bcd2e6fbfb42b211546d99a59a7
data/.gitignore ADDED
@@ -0,0 +1,20 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .DS_Store
6
+ coverage
7
+ InstalledFiles
8
+ lib/bundler/man
9
+ pkg
10
+ vendor/local
11
+ rdoc
12
+ spec/reports
13
+ test/tmp
14
+ test/version_tmp
15
+ tmp
16
+
17
+ # YARD artifacts
18
+ .yardoc
19
+ _yardoc
20
+ doc/
data/.travis.yml ADDED
@@ -0,0 +1,8 @@
1
+ language: ruby
2
+ bundler_args: --without documentation
3
+ rvm:
4
+ - 1.9.3
5
+ - 2.0.0
6
+ branches:
7
+ only:
8
+ - master
data/CHANGELOG.md ADDED
@@ -0,0 +1,34 @@
1
+ ### Changelog
2
+
3
+ ### ADN Reborn
4
+ * **Version 0.4.1** (13 February 2016)
5
+ * Fixed naming of module in spec directory
6
+ * **Version 0.4** (13 February 2016)
7
+ * Forked from adn-rb/adn
8
+
9
+ ### ADN
10
+ * **Version 0.3.10** (25 October 2014)
11
+ * Bugfix: Construct user object using data property
12
+ * **Version 0.3.9** (25 October 2014)
13
+ * Bugfix: Use of ADN.post/delete changed to ADN::API.post/delete
14
+ * **Version 0.3.8** (31 December 2013)
15
+ * Removed all style violations (cane --no-doc)
16
+ * Added files to broadcast builder
17
+ * **Version 0.3.7** (15 December 2013)
18
+ * Recipes now use the builder pattern
19
+ * Basic file functionality
20
+ * **Version 0.3.6** (11 December 2013)
21
+ * Added recipe for easy broadcast creation
22
+ * **Version 0.3.5** (23 October 2012)
23
+ * Added the unified stream
24
+ * **Version 0.3.1** (28 August 2012)
25
+ * Fixed naming conflict.
26
+ * **Version 0.3** (28 August 2012)
27
+ * Now includes a Post class that describes each post along with a bunch of new methods of accessing a post's replies and the post's original post.
28
+ * Users methods that return user details (follow, unfollow, following, followers, mute, unmute, mute_list) will now return either a User object back, or an array of User objects.
29
+ * Similarly, User methods that return post details (posts, mentions, stream) will return a Post object, or an array of Post objects.
30
+ * To accomplish all this, I've had to change the module structure which will break existing code if you've relied on the modules to access any data. Basically, all modules begin with ADN::API:: now (eg. ADN::API::Post, ADN::API::User).
31
+ * **Version 0.2** (27 August 2012)
32
+ * Changed all existing classes to modules and introduced the User class for easily accessing user details.
33
+ * **Version 0.1** (26 August 2012)
34
+ * Really basic functionality for all existing App.new API methods as per the spec.
data/Gemfile ADDED
@@ -0,0 +1,8 @@
1
+ source "https://rubygems.org"
2
+
3
+ gemspec
4
+
5
+ group :test do
6
+ gem 'rake'
7
+ gem 'minitest', '~> 5.1'
8
+ end
data/Gemfile.lock ADDED
@@ -0,0 +1,25 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ adn-reborn (0.4.1)
5
+ mime-types (~> 2.0)
6
+ multipart-post (~> 1.2.0)
7
+
8
+ GEM
9
+ remote: https://rubygems.org/
10
+ specs:
11
+ mime-types (2.99)
12
+ minitest (5.1.0)
13
+ multipart-post (1.2.0)
14
+ rake (10.1.0)
15
+
16
+ PLATFORMS
17
+ ruby
18
+
19
+ DEPENDENCIES
20
+ adn-reborn!
21
+ minitest (~> 5.1)
22
+ rake
23
+
24
+ BUNDLED WITH
25
+ 1.11.2
data/LICENSE ADDED
@@ -0,0 +1,16 @@
1
+ Copyright (c) 2016 trorornmn
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4
+
5
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6
+
7
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
8
+
9
+
10
+ Copyright (c) 2013 Kishyr Ramdial.
11
+
12
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
13
+
14
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,19 @@
1
+ ADN Reborn
2
+ ==========
3
+
4
+ A Ruby library for App.net by [@trorornmn](https://alpha.app.net/trorornmn).
5
+
6
+ ADN Reborn is a fork of adn-rb/adn
7
+
8
+ ### Contributors
9
+
10
+ ADN Reborn is brought to you by these very handsome gentlemen:
11
+
12
+ * trorornmn ([@trorornmn](https://alpha.app.net/trorornmn))
13
+ * Kishyr Ramdial ([@kishyr](https://alpha.app.net/kishyr))
14
+ * Dave Goodchild ([@buddhamagnet](https://alpha.app.net/buddhamagnet))
15
+ * Peter Hellberg ([@peterhellberg](https://alpha.app.net/peterhellberg))
16
+
17
+ ### License
18
+
19
+ This software is released under the MIT License, see LICENSE.
data/Rakefile ADDED
@@ -0,0 +1,17 @@
1
+ #!/usr/bin/env rake
2
+ require 'find'
3
+ require 'rake/testtask'
4
+ require 'bundler/gem_tasks'
5
+
6
+ task :default => :spec
7
+
8
+ Rake::TestTask.new(:spec) do |t|
9
+ t.test_files = FileList['spec/**/*_spec.rb']
10
+ end
11
+
12
+ desc "Run cane to check quality metrics"
13
+ task :quality do
14
+ Bundler.with_clean_env do
15
+ exec 'cane'
16
+ end
17
+ end
@@ -0,0 +1,19 @@
1
+ # encoding: UTF-8
2
+
3
+ require File.expand_path('../lib/adn-reborn/version', __FILE__)
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = 'adn-reborn'
7
+ s.version = ADNReborn::VERSION
8
+ s.authors = ["trorornmn", "Kishyr Ramdial", "Dave Goodchild", "Peter Hellberg"]
9
+ s.email = ["trorornmn.develop@gmail.com", "kishyr@gmail.com", "buddhamagnet@gmail.com", "peter@c7.se"]
10
+ s.description = "A simple and easy to use library to interact with App.net's API"
11
+ s.summary = "A Ruby library for App.net"
12
+ s.homepage = "https://github.com/trorornmn/adn-reborn"
13
+ s.files = `git ls-files`.split($\)
14
+ s.test_files = s.files.grep(%r{^(spec)/})
15
+
16
+ s.add_runtime_dependency "multipart-post", ["~> 1.2.0"]
17
+ s.add_runtime_dependency "mime-types", ["~> 2.0"]
18
+ s.required_ruby_version = '>= 1.9.3'
19
+ end
@@ -0,0 +1,36 @@
1
+ # encoding: UTF-8
2
+ require 'mime/types'
3
+ require 'net/http/post/multipart'
4
+
5
+ module ADNReborn
6
+ module API
7
+ module File
8
+ def self.create(filename, params)
9
+ string_io = StringIO.new(params.to_json)
10
+
11
+ content = UploadIO.new(filename, MIME::Types.type_for(filename)[0])
12
+ metadata = UploadIO.new(string_io, "application/json", "data")
13
+
14
+ http_params = {
15
+ "content" => content,
16
+ # make a fake file so we can still pass json
17
+ "metadata" => metadata
18
+ }
19
+
20
+ ADNReborn::API.post_multipart("#{ADNReborn::API_ENDPOINT_FILES}", http_params)
21
+ end
22
+
23
+ def self.retrieve(file_id)
24
+ ADNReborn::API.get("#{ADNReborn::API_ENDPOINT_FILES}/#{file_id}")
25
+ end
26
+
27
+ def self.by_id(file_id)
28
+ self.retrieve(file_id)
29
+ end
30
+
31
+ def self.delete(file_id)
32
+ ADNReborn::API.delete("#{ADNReborn::API_ENDPOINT_FILES}/#{file_id}")
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,10 @@
1
+ # encoding: UTF-8
2
+
3
+ module ADNReborn
4
+ module API
5
+ module Filter
6
+ # Not Yet Implemented
7
+ # https://github.com/appdotnet/api-spec/blob/master/resources/filters.md
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,29 @@
1
+ # encoding: UTF-8
2
+
3
+ module ADNReborn
4
+ module API
5
+ module Message
6
+ def self.create(channel_id, params)
7
+ path = "/#{channel_id}/messages"
8
+
9
+ ADNReborn::API.post(ADNReborn::API_ENDPOINT_CHANNELS + path, params)
10
+ end
11
+
12
+ def self.retrieve(channel_id, message_id)
13
+ path = "/#{channel_id}/messages/#{message_id}"
14
+
15
+ ADNReborn::API.get(ADNReborn::API_ENDPOINT_CHANNELS + path)
16
+ end
17
+
18
+ def self.by_id(channel_id, message_id)
19
+ self.retrieve(channel_id, message_id)
20
+ end
21
+
22
+ def self.delete(channel_id, message_id)
23
+ path = "/#{channel_id}/messages/#{message_id}"
24
+
25
+ ADNReborn::API.delete(ADNReborn::API_ENDPOINT_CHANNELS + path)
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,51 @@
1
+ # encoding: UTF-8
2
+
3
+ module ADNReborn
4
+ module API
5
+ module Post
6
+ def self.create(params)
7
+ ADNReborn::API.post("#{ADNReborn::API_ENDPOINT_POSTS}", params)
8
+ end
9
+
10
+ def self.retrieve(post_id)
11
+ ADNReborn::API.get("#{ADNReborn::API_ENDPOINT_POSTS}/#{post_id}")
12
+ end
13
+
14
+ def self.by_id(post_id)
15
+ self.retrieve(post_id)
16
+ end
17
+
18
+ def self.delete(post_id)
19
+ ADNReborn::API.delete("#{ADNReborn::API_ENDPOINT_POSTS}/#{post_id}")
20
+ end
21
+
22
+ def self.replies(post_id, params = nil)
23
+ ADNReborn::API.get("#{ADNReborn::API_ENDPOINT_POSTS}/#{post_id}/replies", params)
24
+ end
25
+
26
+ def self.by_user(user_id, params = nil)
27
+ ADNReborn::API.get("#{ADNReborn::API_ENDPOINT_USERS}/#{user_id}/posts", params)
28
+ end
29
+
30
+ def self.mentioning_user(user_id, params = nil)
31
+ ADNReborn::API.get("#{ADNReborn::API_ENDPOINT_USERS}/#{user_id}/mentions", params)
32
+ end
33
+
34
+ def self.stream(params = nil)
35
+ ADNReborn::API.get("#{ADNReborn::API_ENDPOINT_POSTS}/stream", params)
36
+ end
37
+
38
+ def self.global_stream(params = nil)
39
+ ADNReborn::API.get("#{ADNReborn::API_ENDPOINT_POSTS}/stream/global", params)
40
+ end
41
+
42
+ def self.unified_stream(params = nil)
43
+ ADNReborn::API.get("#{ADNReborn::API_ENDPOINT_POSTS}/stream/unified", params)
44
+ end
45
+
46
+ def self.by_hashtag(hashtag, params = nil)
47
+ ADNReborn::API.get("#{ADNReborn::API_ENDPOINT_POSTS}/tag/#{hashtag}", params)
48
+ end
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,19 @@
1
+ # encoding: UTF-8
2
+
3
+ require 'delegate'
4
+
5
+ module ADNReborn
6
+ module API
7
+ class Response < SimpleDelegator
8
+ def has_error?
9
+ self['meta'].nil? ||
10
+ self['meta']['code'].nil? ||
11
+ self['meta']['code'] >= HTTP_ERROR
12
+ end
13
+
14
+ def error_message
15
+ self.fetch('meta').fetch('error_message')
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,10 @@
1
+ # encoding: UTF-8
2
+
3
+ module ADNReborn
4
+ module API
5
+ module Stream
6
+ # Not Yet Implemented
7
+ # https://github.com/appdotnet/api-spec/blob/master/resources/streams.md
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,11 @@
1
+ # encoding: UTF-8
2
+
3
+ module ADNReborn
4
+ module API
5
+ module Subscription
6
+ # Not Yet Implemented
7
+ # https://github.com/appdotnet/api-spec/
8
+ # blob/master/resources/subscriptions.md
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,12 @@
1
+ # encoding: UTF-8
2
+
3
+ module ADNReborn
4
+ module API
5
+ module Token
6
+ def self.current
7
+ result = ADNReborn::API.get(ADNReborn::API_ENDPOINT_TOKEN)
8
+ result["data"]
9
+ end
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,19 @@
1
+ # encoding: UTF-8
2
+
3
+ module ADNReborn
4
+ module API
5
+ module User
6
+ def self.retrieve(user_id)
7
+ ADNReborn::API.get("#{ADNReborn::API_ENDPOINT_USERS}/#{user_id}")
8
+ end
9
+
10
+ def self.following(user_id)
11
+ ADNReborn::API.get("#{ADNReborn::API_ENDPOINT_USERS}/#{user_id}/following")
12
+ end
13
+
14
+ def self.followers(user_id)
15
+ ADNReborn::API.get("#{ADNReborn::API_ENDPOINT_USERS}/#{user_id}/followers")
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,62 @@
1
+ # encoding: UTF-8
2
+
3
+ %w{response filter post stream subscription token user message file}.each do |f|
4
+ require_relative "api/#{f}"
5
+ end
6
+
7
+ module ADNReborn
8
+ module API
9
+ Error = Class.new StandardError
10
+
11
+ class << self
12
+ def perform(request)
13
+ request.add_field("Authorization", "Bearer #{ADNReborn.token}")
14
+ response = JSON.parse ADNReborn::HTTP.request(request).body
15
+
16
+ Response.new(response).tap { |r|
17
+ raise ADNReborn::API::Error, r.error_message if r.has_error?
18
+ }
19
+ end
20
+
21
+ def construct_request(verb, url)
22
+ http_method = case verb
23
+ when :post then Net::HTTP::Post
24
+ when :put then Net::HTTP::Put
25
+ when :delete then Net::HTTP::Delete
26
+ else Net::HTTP::Get
27
+ end
28
+ http_method.new(url)
29
+ end
30
+
31
+ def get(url, params = nil)
32
+ url = params.nil? ? url : [url, URI.encode_www_form(params)].join("?")
33
+ request = construct_request(:get, url)
34
+ perform(request)
35
+ end
36
+
37
+ def post(url, params = nil)
38
+ request = construct_request(:post, url)
39
+ request.add_field('Content-Type', 'application/json')
40
+ request.body = params.to_json if params
41
+ perform(request)
42
+ end
43
+
44
+ def post_multipart(url, *http_args)
45
+ request = Net::HTTP::Post::Multipart.new(url, *http_args)
46
+ perform(request)
47
+ end
48
+
49
+ def put(url, params = nil)
50
+ request = construct_request(:put, url)
51
+ request.add_field('Content-Type', 'application/json')
52
+ request.body = params.to_json if params
53
+ perform(request)
54
+ end
55
+
56
+ def delete(url)
57
+ request = construct_request(:delete, url)
58
+ perform(request)
59
+ end
60
+ end
61
+ end
62
+ end
@@ -0,0 +1,18 @@
1
+ # encoding: UTF-8
2
+
3
+ require 'net/https'
4
+
5
+ module ADNReborn
6
+ API_HOST = "api.app.net"
7
+ API_ENDPOINT = "/stream/0"
8
+ API_ENDPOINT_POSTS = "#{API_ENDPOINT}/posts"
9
+ API_ENDPOINT_USERS = "#{API_ENDPOINT}/users"
10
+ API_ENDPOINT_TOKEN = "#{API_ENDPOINT}/token"
11
+ API_ENDPOINT_CHANNELS = "#{API_ENDPOINT}/channels"
12
+ API_ENDPOINT_FILES = "#{API_ENDPOINT}/files"
13
+ HTTP = Net::HTTP.new(API_HOST, 443)
14
+
15
+ HTTP_ERROR = 400
16
+
17
+ HTTP.use_ssl = true
18
+ end
@@ -0,0 +1,62 @@
1
+ # encoding: UTF-8
2
+
3
+ module ADNReborn
4
+ class File
5
+ attr_accessor :id, :file_id, :file_token, :sha1, :name, :source,
6
+ :url, :kind, :total_size, :url_expires, :size,
7
+ :type, :public, :mime_type, :complete
8
+
9
+ attr_writer :user, :created_at
10
+
11
+ def self.upload_file(filename, params)
12
+ result = ADNReborn::API::File.create(filename, params)
13
+ File.new(result["data"])
14
+ end
15
+
16
+ def self.by_id(id)
17
+ result = ADNReborn::API::File.by_id(id)
18
+ File.new(result["data"])
19
+ end
20
+
21
+ def initialize(raw_file)
22
+ if raw_file.respond_to?(:each_pair)
23
+ set_values(raw_file)
24
+ file_id = id
25
+ else
26
+ file_id = raw_file
27
+ file_details = details
28
+ if file_details.has_key? "data"
29
+ set_values(file_details["data"])
30
+ end
31
+ end
32
+ end
33
+
34
+ def details
35
+ if id
36
+ value = self.instance_variables.map do |i|
37
+ [i.to_s.slice(1..-1), self.instance_variable_get(i)]
38
+ end
39
+ Hash[value]
40
+ else
41
+ ADNReborn::API::File.by_id(file_id)
42
+ end
43
+ end
44
+
45
+ def created_at
46
+ DateTime.parse(@created_at)
47
+ end
48
+
49
+ def user
50
+ ADNReborn::User.new(@user)
51
+ end
52
+
53
+ def delete
54
+ result = ADNReborn::API::File.delete(id)
55
+ ADNReborn.create_instance(result["data"], File)
56
+ end
57
+
58
+ def set_values(values)
59
+ values.each_pair { |k, v| send("#{k}=", v) if respond_to?("#{k}=") }
60
+ end
61
+ end
62
+ end
@@ -0,0 +1,76 @@
1
+ # encoding: UTF-8
2
+
3
+ module ADNReborn
4
+ class Message
5
+ attr_accessor(
6
+ :id, :message_id, :channel_id, :text,
7
+ :html, :source, :machine_only,
8
+ :reply_to, :thread_id,
9
+ :num_replies, :annotations, :entities,
10
+ )
11
+
12
+ attr_writer :user, :created_at
13
+
14
+ def self.send_message(params)
15
+ channel_id = params.delete('channel_id')
16
+ result = ADNReborn::API::Message.create(channel_id, params)
17
+ Message.new(result["data"])
18
+ end
19
+
20
+ def self.by_id(channel_id, id)
21
+ result = ADNReborn::API::Message.by_id(channel_id, id)
22
+ Message.new(result["data"])
23
+ end
24
+
25
+ def initialize(raw_message)
26
+ set_values(raw_message)
27
+ message_id = id
28
+
29
+ if raw_message.length == 2 and
30
+ raw_message.key? :id and
31
+ raw_message.key? :channel_id
32
+
33
+ # If we only have the bare minimum data,
34
+ # assume we want to get values from the server
35
+ message_details = details
36
+
37
+ if message_details.has_key? "data"
38
+ set_values(message_details["data"])
39
+ end
40
+ end
41
+ end
42
+
43
+ def details
44
+ # if we have a source, then we've loaded
45
+ # stuff from the server
46
+ if source
47
+ value = self.instance_variables.map do |i|
48
+ [i.to_s.slice(1..-1), self.instance_variable_get(i)]
49
+ end
50
+
51
+ Hash[value]
52
+ else
53
+ ADNReborn::API::Message.by_id(channel_id, message_id)
54
+ end
55
+ end
56
+
57
+ def created_at
58
+ DateTime.parse(@created_at)
59
+ end
60
+
61
+ def user
62
+ ADNReborn::User.new(@user)
63
+ end
64
+
65
+ def delete
66
+ result = ADNReborn::API::Message.delete(channel_id, id)
67
+ ADNReborn.create_instance(result["data"], Message)
68
+ end
69
+
70
+ def set_values(values)
71
+ values.each_pair do |k, v|
72
+ send("#{k}=", v) if respond_to?("#{k}=")
73
+ end
74
+ end
75
+ end
76
+ end