moostodon 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
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,47 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'http/parser'
4
+ require 'openssl'
5
+ require 'resolv'
6
+
7
+ module Mastodon
8
+ module Streaming
9
+ # Socket/Connection handler.
10
+ class Connection
11
+ attr_reader :tcp_socket_class, :ssl_socket_class
12
+
13
+ def initialize(options = {})
14
+ @tcp_socket_class = options.fetch(:tcp_socket_class) { TCPSocket }
15
+ @ssl_socket_class = options.fetch(:ssl_socket_class) do
16
+ OpenSSL::SSL::SSLSocket
17
+ end
18
+ @using_ssl = options.fetch(:using_ssl) { false }
19
+ end
20
+
21
+ def stream(request, response)
22
+ client = connect(request)
23
+ request.stream(client)
24
+ # rubocop:disable AssignmentInCondition
25
+ while body = client.readpartial(1024)
26
+ response << body
27
+ end
28
+ # rubocop:enable AssignmentInCondition
29
+ end
30
+
31
+ def connect(request)
32
+ client = new_tcp_socket(request.socket_host, request.socket_port)
33
+ return client if !@using_ssl || (!@using_ssl && request.using_proxy?)
34
+
35
+ client_context = OpenSSL::SSL::SSLContext.new
36
+ ssl_client = @ssl_socket_class.new(client, client_context)
37
+ ssl_client.connect
38
+ end
39
+
40
+ private
41
+
42
+ def new_tcp_socket(host, port)
43
+ @tcp_socket_class.new(Resolv.getaddress(host), port)
44
+ end
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Mastodon
4
+ module Streaming
5
+ # Handles deleted statuses.
6
+ class DeletedStatus
7
+ # @!attribute [r] id
8
+ # @return [Integer]
9
+
10
+ attr_reader :id
11
+
12
+ def initialize(id)
13
+ @id = id.to_i
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'mastodon/status'
4
+ require 'mastodon/streaming/deleted_status'
5
+
6
+ module Mastodon
7
+ module Streaming
8
+ class MessageParser
9
+ MESSAGE_TYPES = {
10
+ 'update' => Status,
11
+ 'notification' => Notification,
12
+ 'delete' => DeletedStatus
13
+ }.freeze
14
+
15
+ def self.parse(type, data)
16
+ klass = MESSAGE_TYPES[type]
17
+ klass&.new(data)
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,45 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'buftok'
4
+ require 'http'
5
+ require 'json'
6
+ require 'mastodon/error'
7
+
8
+ module Mastodon
9
+ module Streaming
10
+ # When someone responses to toots.
11
+ class Response
12
+ # Initializes a new Response object
13
+ #
14
+ # @return [Mastodon::Streaming::Response]
15
+ def initialize(&block)
16
+ @block = block
17
+ @parser = Http::Parser.new(self)
18
+ @tokenizer = BufferedTokenizer.new("\n\n")
19
+ @match = Regexp.new(/event: ([a-z]+)\ndata: (.+)/m)
20
+ end
21
+
22
+ def <<(data)
23
+ @parser << data
24
+ end
25
+
26
+ # @note this does get called
27
+ def on_headers_complete(_headers)
28
+ error = Mastodon::Error::ERRORS[@parser.status_code]
29
+ raise error if error
30
+ end
31
+
32
+ def on_body(data)
33
+ @tokenizer.extract(data).each do |line|
34
+ has_data = @match.match(line)
35
+ next if has_data.nil?
36
+
37
+ type = has_data[1]
38
+ data = has_data[2]
39
+
40
+ @block.call(type, JSON.parse(data))
41
+ end
42
+ end
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,32 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Mastodon
4
+ # Sets version information for this gem.
5
+ module Version
6
+ module_function
7
+
8
+ def major
9
+ 0
10
+ end
11
+
12
+ def minor
13
+ 1
14
+ end
15
+
16
+ def patch
17
+ 0
18
+ end
19
+
20
+ def pre
21
+ nil
22
+ end
23
+
24
+ def to_a
25
+ [major, minor, patch, pre].compact
26
+ end
27
+
28
+ def to_s
29
+ to_a.join('.')
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,25 @@
1
+ # frozen_string_literal: true
2
+
3
+ lib = File.expand_path('lib', __dir__)
4
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
+ require 'mastodon/version'
6
+
7
+ Gem::Specification.new do |spec|
8
+ spec.name = 'moostodon'
9
+ spec.description = 'A ruby interface to the Mastodon API'
10
+ spec.homepage = 'https://github.com/tootsuite/mastodon-api'
11
+ spec.email = 'eugen@zeonfederated.com'
12
+ spec.authors = ['Eugen Rochko', 'Maxine Michalski']
13
+ spec.summary = spec.description
14
+ spec.licenses = %w[MIT]
15
+ spec.files = %w[moostodon.gemspec] + Dir['lib/**/*.rb']
16
+ spec.require_paths = %w[lib]
17
+ spec.version = Mastodon::Version
18
+
19
+ spec.add_dependency 'addressable', '~> 2.5'
20
+ spec.add_dependency 'buftok'
21
+ spec.add_dependency 'http', '~> 3.0'
22
+ spec.add_dependency 'oj', '~> 3.3'
23
+
24
+ spec.add_development_dependency 'bundler', '~> 1.0'
25
+ end
metadata ADDED
@@ -0,0 +1,156 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: moostodon
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Eugen Rochko
8
+ - Maxine Michalski
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2018-08-28 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: addressable
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - "~>"
19
+ - !ruby/object:Gem::Version
20
+ version: '2.5'
21
+ type: :runtime
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - "~>"
26
+ - !ruby/object:Gem::Version
27
+ version: '2.5'
28
+ - !ruby/object:Gem::Dependency
29
+ name: buftok
30
+ requirement: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - ">="
33
+ - !ruby/object:Gem::Version
34
+ version: '0'
35
+ type: :runtime
36
+ prerelease: false
37
+ version_requirements: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ version: '0'
42
+ - !ruby/object:Gem::Dependency
43
+ name: http
44
+ requirement: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - "~>"
47
+ - !ruby/object:Gem::Version
48
+ version: '3.0'
49
+ type: :runtime
50
+ prerelease: false
51
+ version_requirements: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - "~>"
54
+ - !ruby/object:Gem::Version
55
+ version: '3.0'
56
+ - !ruby/object:Gem::Dependency
57
+ name: oj
58
+ requirement: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - "~>"
61
+ - !ruby/object:Gem::Version
62
+ version: '3.3'
63
+ type: :runtime
64
+ prerelease: false
65
+ version_requirements: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - "~>"
68
+ - !ruby/object:Gem::Version
69
+ version: '3.3'
70
+ - !ruby/object:Gem::Dependency
71
+ name: bundler
72
+ requirement: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - "~>"
75
+ - !ruby/object:Gem::Version
76
+ version: '1.0'
77
+ type: :development
78
+ prerelease: false
79
+ version_requirements: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - "~>"
82
+ - !ruby/object:Gem::Version
83
+ version: '1.0'
84
+ description: A ruby interface to the Mastodon API
85
+ email: eugen@zeonfederated.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - lib/mastodon.rb
91
+ - lib/mastodon/account.rb
92
+ - lib/mastodon/app.rb
93
+ - lib/mastodon/base.rb
94
+ - lib/mastodon/card.rb
95
+ - lib/mastodon/client.rb
96
+ - lib/mastodon/collection.rb
97
+ - lib/mastodon/emoji.rb
98
+ - lib/mastodon/entities/app.rb
99
+ - lib/mastodon/entities/hashtag.rb
100
+ - lib/mastodon/entities/media.rb
101
+ - lib/mastodon/entities/mention.rb
102
+ - lib/mastodon/error.rb
103
+ - lib/mastodon/headers.rb
104
+ - lib/mastodon/instance.rb
105
+ - lib/mastodon/list.rb
106
+ - lib/mastodon/media.rb
107
+ - lib/mastodon/notification.rb
108
+ - lib/mastodon/relationship.rb
109
+ - lib/mastodon/rest/accounts.rb
110
+ - lib/mastodon/rest/api.rb
111
+ - lib/mastodon/rest/apps.rb
112
+ - lib/mastodon/rest/client.rb
113
+ - lib/mastodon/rest/instances.rb
114
+ - lib/mastodon/rest/media.rb
115
+ - lib/mastodon/rest/notifications.rb
116
+ - lib/mastodon/rest/relationships.rb
117
+ - lib/mastodon/rest/request.rb
118
+ - lib/mastodon/rest/search.rb
119
+ - lib/mastodon/rest/statuses.rb
120
+ - lib/mastodon/rest/suggestions.rb
121
+ - lib/mastodon/rest/timelines.rb
122
+ - lib/mastodon/rest/utils.rb
123
+ - lib/mastodon/results.rb
124
+ - lib/mastodon/status.rb
125
+ - lib/mastodon/streaming/client.rb
126
+ - lib/mastodon/streaming/connection.rb
127
+ - lib/mastodon/streaming/deleted_status.rb
128
+ - lib/mastodon/streaming/message_parser.rb
129
+ - lib/mastodon/streaming/response.rb
130
+ - lib/mastodon/version.rb
131
+ - moostodon.gemspec
132
+ homepage: https://github.com/tootsuite/mastodon-api
133
+ licenses:
134
+ - MIT
135
+ metadata: {}
136
+ post_install_message:
137
+ rdoc_options: []
138
+ require_paths:
139
+ - lib
140
+ required_ruby_version: !ruby/object:Gem::Requirement
141
+ requirements:
142
+ - - ">="
143
+ - !ruby/object:Gem::Version
144
+ version: '0'
145
+ required_rubygems_version: !ruby/object:Gem::Requirement
146
+ requirements:
147
+ - - ">="
148
+ - !ruby/object:Gem::Version
149
+ version: '0'
150
+ requirements: []
151
+ rubyforge_project:
152
+ rubygems_version: 2.5.2.1
153
+ signing_key:
154
+ specification_version: 4
155
+ summary: A ruby interface to the Mastodon API
156
+ test_files: []