moostodon 0.1.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 (44) hide show
  1. checksums.yaml +7 -0
  2. data/lib/mastodon.rb +8 -0
  3. data/lib/mastodon/account.rb +62 -0
  4. data/lib/mastodon/app.rb +19 -0
  5. data/lib/mastodon/base.rb +52 -0
  6. data/lib/mastodon/card.rb +43 -0
  7. data/lib/mastodon/client.rb +32 -0
  8. data/lib/mastodon/collection.rb +30 -0
  9. data/lib/mastodon/emoji.rb +14 -0
  10. data/lib/mastodon/entities/app.rb +15 -0
  11. data/lib/mastodon/entities/hashtag.rb +15 -0
  12. data/lib/mastodon/entities/media.rb +31 -0
  13. data/lib/mastodon/entities/mention.rb +17 -0
  14. data/lib/mastodon/error.rb +37 -0
  15. data/lib/mastodon/headers.rb +20 -0
  16. data/lib/mastodon/instance.rb +25 -0
  17. data/lib/mastodon/list.rb +17 -0
  18. data/lib/mastodon/media.rb +37 -0
  19. data/lib/mastodon/notification.rb +32 -0
  20. data/lib/mastodon/relationship.rb +38 -0
  21. data/lib/mastodon/rest/accounts.rb +71 -0
  22. data/lib/mastodon/rest/api.rb +29 -0
  23. data/lib/mastodon/rest/apps.rb +28 -0
  24. data/lib/mastodon/rest/client.rb +13 -0
  25. data/lib/mastodon/rest/instances.rb +19 -0
  26. data/lib/mastodon/rest/media.rb +42 -0
  27. data/lib/mastodon/rest/notifications.rb +19 -0
  28. data/lib/mastodon/rest/relationships.rb +77 -0
  29. data/lib/mastodon/rest/request.rb +54 -0
  30. data/lib/mastodon/rest/search.rb +28 -0
  31. data/lib/mastodon/rest/statuses.rb +130 -0
  32. data/lib/mastodon/rest/suggestions.rb +19 -0
  33. data/lib/mastodon/rest/timelines.rb +47 -0
  34. data/lib/mastodon/rest/utils.rb +42 -0
  35. data/lib/mastodon/results.rb +19 -0
  36. data/lib/mastodon/status.rb +94 -0
  37. data/lib/mastodon/streaming/client.rb +103 -0
  38. data/lib/mastodon/streaming/connection.rb +47 -0
  39. data/lib/mastodon/streaming/deleted_status.rb +17 -0
  40. data/lib/mastodon/streaming/message_parser.rb +21 -0
  41. data/lib/mastodon/streaming/response.rb +45 -0
  42. data/lib/mastodon/version.rb +32 -0
  43. data/moostodon.gemspec +25 -0
  44. metadata +156 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 36477f3200abc7c0778ee098a84ec63f934918cc
4
+ data.tar.gz: 93f1c01eaf91c6fe442c6453facbcdf995bc5b81
5
+ SHA512:
6
+ metadata.gz: d06ad7a87c7cfddb9d00ca7062ddcf74e377d29b10dc345be6a49ebf52559bac098e8da587b8e931839b0ac557e2b1c865bb38b16a4c1b5d95ed0c0f56ce046b
7
+ data.tar.gz: 4dc8a68a77d43f11739788db6bf8c47c0908f7b3f10a658ee3bb5fcfd8af1884e2537c297c5b56b4423cb88a54aec8488233419e4f2c49a523d51828ba864042
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'addressable/uri'
4
+ require 'mastodon/base'
5
+ require 'mastodon/collection'
6
+ require 'mastodon/results'
7
+ require 'mastodon/rest/client'
8
+ require 'mastodon/streaming/client'
@@ -0,0 +1,62 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Mastodon
4
+ class Account < Mastodon::Base
5
+ # @!attribute [r] id
6
+ # @return [String]
7
+ # @!attribute [r] username
8
+ # @return [String]
9
+ # @!attribute [r] acct
10
+ # @return [String]
11
+ # @!attribute [r] display_name
12
+ # @return [String]
13
+ # @!attribute [r] url
14
+ # @return [String]
15
+ # @!attribute [r] avatar
16
+ # @return [String]
17
+ # @!attribute [r] avatar_static
18
+ # @return [String]
19
+ # @!attribute [r] header
20
+ # @return [String]
21
+ # @!attribute [r] header_static
22
+ # @return [String]
23
+ # @!attribute [r] note
24
+ # @return [String]
25
+ # @!attribute [r] followers_count
26
+ # @return [Integer]
27
+ # @!attribute [r] following_count
28
+ # @return [Integer]
29
+ # @!attribute [r] statuses_count
30
+ # @return [Integer]
31
+ # @!attribute [r] created_at
32
+ # @return [String]
33
+ # @!attribute [r] locked?
34
+ # @return [Boolean]
35
+ # @!attribute [r] moved
36
+ # @return [Mastodon::Account]
37
+
38
+ normal_attr_reader :id,
39
+ :username,
40
+ :acct,
41
+ :display_name,
42
+ :created_at,
43
+ :url,
44
+ :avatar,
45
+ :avatar_static,
46
+ :header,
47
+ :header_static,
48
+ :note,
49
+ :followers_count,
50
+ :following_count,
51
+ :statuses_count
52
+
53
+ predicate_attr_reader :locked
54
+
55
+ object_attr_reader :moved, Mastodon::Account
56
+
57
+ def initialize(attributes = {})
58
+ attributes.fetch('id')
59
+ super
60
+ end
61
+ end
62
+ end
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Mastodon
4
+ class App < Mastodon::Base
5
+ # @!attribute [r] client_id
6
+ # @return [String]
7
+ # @!attribute [r] client_secret
8
+ # @return [String]
9
+ # @!attribute [r] name
10
+ # @return [String]
11
+ # @!attribute [r] website
12
+ # @return [String]
13
+
14
+ normal_attr_reader :client_id,
15
+ :client_secret,
16
+ :name,
17
+ :website
18
+ end
19
+ end
@@ -0,0 +1,52 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Mastodon
4
+ class Base
5
+ attr_reader :attributes
6
+
7
+ alias to_h attributes
8
+ alias to_hash attributes
9
+
10
+ def initialize(attributes = {})
11
+ @attributes = attributes
12
+ end
13
+
14
+ class << self
15
+ def normal_attr_reader(*attributes)
16
+ attributes.each do |attribute|
17
+ define_attribute_method(attribute)
18
+ end
19
+ end
20
+
21
+ def object_attr_reader(attribute, klass)
22
+ define_method(attribute) do
23
+ klass.new(@attributes[attribute.to_s])
24
+ end
25
+ end
26
+
27
+ def collection_attr_reader(attribute, klass)
28
+ define_method(attribute) do
29
+ Mastodon::Collection.new(@attributes[attribute.to_s], klass)
30
+ end
31
+ end
32
+
33
+ def predicate_attr_reader(*attributes)
34
+ attributes.each do |attribute|
35
+ define_predicate_method(attribute)
36
+ end
37
+ end
38
+
39
+ def define_predicate_method(key)
40
+ define_method("#{key}?") do
41
+ @attributes[key.to_s]
42
+ end
43
+ end
44
+
45
+ def define_attribute_method(key)
46
+ define_method(key) do
47
+ @attributes[key.to_s]
48
+ end
49
+ end
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,43 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Mastodon
4
+ class Card < Mastodon::Base
5
+ # @!attribute [r] url
6
+ # @return [String]
7
+ # @!attribute [r] title
8
+ # @return [String]
9
+ # @!attribute [r] description
10
+ # @return [String]
11
+ # @!attribute [r] image
12
+ # @return [String]
13
+ # @!attribute [r] type
14
+ # @return [String]
15
+ # @!attribute [r] author_name
16
+ # @return [String]
17
+ # @!attribute [r] author_url
18
+ # @return [String]
19
+ # @!attribute [r] provider_name
20
+ # @return [String]
21
+ # @!attribute [r] provider_url
22
+ # @return [String]
23
+ # @!attribute [r] html
24
+ # @return [String]
25
+ # @!attribute [r] width
26
+ # @return [String]
27
+ # @!attribute [r] height
28
+ # @return [String]
29
+
30
+ normal_attr_reader :url,
31
+ :title,
32
+ :description,
33
+ :image,
34
+ :type,
35
+ :author_name,
36
+ :author_url,
37
+ :provider_name,
38
+ :provider_url,
39
+ :html,
40
+ :width,
41
+ :height
42
+ end
43
+ end
@@ -0,0 +1,32 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'mastodon/version'
4
+
5
+ module Mastodon
6
+ class Client
7
+ DEFAULT_TIMEOUT = {
8
+ connect: 2,
9
+ read: 5,
10
+ write: 20
11
+ }.freeze
12
+
13
+ attr_reader :base_url, :bearer_token, :timeout
14
+
15
+ # @param options [Hash]
16
+ # @option options :base_url [String] URL of the instance you want to
17
+ # connect to
18
+ # @option options :bearer_token [String] OAuth access token for your
19
+ # authenticated user
20
+ def initialize(options = {})
21
+ @base_url = options[:base_url]
22
+ @bearer_token = options[:bearer_token]
23
+ @timeout = DEFAULT_TIMEOUT.merge(options[:timeout] || {})
24
+ end
25
+
26
+ # User agent of the client
27
+ # @return [String]
28
+ def user_agent
29
+ @user_agent ||= "MastodonRubyGem/#{Mastodon::Version}"
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,30 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Mastodon
4
+ # Mastodon collections. Used in place of arrays.
5
+ class Collection
6
+ include ::Enumerable
7
+
8
+ def initialize(items, klass)
9
+ @collection = items.map { |attributes| klass.new(attributes) }
10
+ end
11
+
12
+ def each(start = 0)
13
+ return to_enum(:each, start) unless block_given?
14
+
15
+ Array(@collection[start..-1]).each do |element|
16
+ yield(element)
17
+ end
18
+
19
+ self
20
+ end
21
+
22
+ def size
23
+ @collection.size
24
+ end
25
+
26
+ def last
27
+ @collection.last
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Mastodon
4
+ class Emoji < Mastodon::Base
5
+ # @!attribute [r] shortcode
6
+ # @return [String]
7
+ # @!attribute [r] static_url
8
+ # @return [String]
9
+ # @!attribute [r] url
10
+ # @return [String]
11
+
12
+ normal_attr_reader :shortcode, :static_url, :url
13
+ end
14
+ end
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Mastodon
4
+ module Entities
5
+ # The application a toot was sent by.
6
+ class App < Mastodon::Base
7
+ # @!attribute [r] name
8
+ # @return [String]
9
+ # @!attribute [r] website
10
+ # @return [String]
11
+
12
+ normal_attr_reader :name, :website
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Mastodon
4
+ module Entities
5
+ # Hashtags are the only searchable item inside toots.
6
+ class Hashtag < Mastodon::Base
7
+ # @!attribute [r] name
8
+ # @return [String]
9
+ # @!attribute [r] url
10
+ # @return [String]
11
+
12
+ normal_attr_reader :name, :url
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,31 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Mastodon
4
+ module Entities
5
+ # Media parts of a toot, like pictures or videos.
6
+ class Media < Mastodon::Base
7
+ # @!attribute [r] id
8
+ # @return [Integer]
9
+ # @!attribute [r] url
10
+ # @return [String]
11
+ # @!attribute [r] remote_url
12
+ # @return [String]
13
+ # @!attribute [r] preview_url
14
+ # @return [String]
15
+ # @!attribute [r] type
16
+ # @return [String]
17
+ # @!attribute [r] meta
18
+ # @return [Hash]
19
+ # @!attribute [r] description
20
+ # @return [String]
21
+
22
+ normal_attr_reader :id,
23
+ :url,
24
+ :remote_url,
25
+ :preview_url,
26
+ :type,
27
+ :meta,
28
+ :description
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Mastodon
4
+ module Entities
5
+ # When talking about another user.
6
+ class Mention < Mastodon::Base
7
+ # @!attribute [r] id
8
+ # @return [String] Account ID
9
+ # @!attribute [r] acct
10
+ # @return [String]
11
+ # @!attribute [r] url
12
+ # @return [String]
13
+
14
+ normal_attr_reader :id, :acct, :url
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,37 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Mastodon
4
+ # General error class for Mastodon.
5
+ class Error < StandardError
6
+ ClientError = Class.new(self)
7
+ BadRequest = Class.new(ClientError)
8
+ Unauthorized = Class.new(ClientError)
9
+ Forbidden = Class.new(ClientError)
10
+ UnprocessableEntity = Class.new(ClientError)
11
+ TooManyRequests = Class.new(ClientError)
12
+
13
+ ServerError = Class.new(self)
14
+ InternalServerError = Class.new(ServerError)
15
+ BadGateway = Class.new(ServerError)
16
+ ServiceUnavailable = Class.new(ServerError)
17
+ GatewayTimeout = Class.new(ServerError)
18
+
19
+ ERRORS = {
20
+ 400 => Mastodon::Error::BadRequest,
21
+ 401 => Mastodon::Error::Unauthorized,
22
+ 403 => Mastodon::Error::Forbidden,
23
+ 422 => Mastodon::Error::UnprocessableEntity,
24
+ 429 => Mastodon::Error::TooManyRequests,
25
+ 500 => Mastodon::Error::InternalServerError,
26
+ 502 => Mastodon::Error::BadGateway,
27
+ 503 => Mastodon::Error::ServiceUnavailable,
28
+ 504 => Mastodon::Error::GatewayTimeout
29
+ }.freeze
30
+
31
+ class << self
32
+ def from_response(body)
33
+ new(body['error'])
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Mastodon
4
+ # Class to handle headers. Important for OAuth2 to work properly.
5
+ class Headers
6
+ # @param client [Mastodon::Client]
7
+ def initialize(client)
8
+ @client = client
9
+ end
10
+
11
+ # @return [Hash]
12
+ def request_headers
13
+ {
14
+ user_agent: @client.user_agent,
15
+ accept: '*/*',
16
+ authorization: "Bearer #{@client.bearer_token}"
17
+ }
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,25 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Mastodon
4
+ class Instance < Mastodon::Base
5
+ # @!attribute [r] uri
6
+ # @return [String]
7
+ # @!attribute [r] title
8
+ # @return [String]
9
+ # @!attribute [r] description
10
+ # @return [String]
11
+ # @!attribute [r] email
12
+ # @return [String]
13
+ # @!attribute [r] version
14
+ # @return [String]
15
+ # @!attribute [r] urls
16
+ # @return [Hash]
17
+
18
+ normal_attr_reader :uri,
19
+ :title,
20
+ :description,
21
+ :email,
22
+ :version,
23
+ :urls
24
+ end
25
+ end