arena 0.1.3 → 0.2.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 (55) hide show
  1. checksums.yaml +5 -5
  2. data/.github/workflows/lint.yml +14 -0
  3. data/.github/workflows/test.yml +14 -0
  4. data/.rubocop.yml +12 -0
  5. data/.rubocop_todo.yml +49 -0
  6. data/Gemfile +12 -0
  7. data/README.md +1 -1
  8. data/Rakefile +9 -4
  9. data/arena.gemspec +16 -18
  10. data/lib/arena/account.rb +9 -7
  11. data/lib/arena/api.rb +42 -40
  12. data/lib/arena/base.rb +6 -5
  13. data/lib/arena/block.rb +5 -3
  14. data/lib/arena/cache/adaptor/generic.rb +3 -1
  15. data/lib/arena/cache/adaptor/null.rb +4 -2
  16. data/lib/arena/cache/adaptor/padrino.rb +3 -1
  17. data/lib/arena/cache/adaptor/rails.rb +7 -5
  18. data/lib/arena/cache.rb +11 -9
  19. data/lib/arena/channel.rb +8 -6
  20. data/lib/arena/channel_feed.rb +9 -8
  21. data/lib/arena/client.rb +25 -16
  22. data/lib/arena/comment.rb +7 -5
  23. data/lib/arena/configurable.rb +15 -13
  24. data/lib/arena/connectable.rb +21 -13
  25. data/lib/arena/connection.rb +6 -4
  26. data/lib/arena/core_ext/string.rb +7 -3
  27. data/lib/arena/creatable.rb +3 -3
  28. data/lib/arena/default.rb +9 -7
  29. data/lib/arena/entities/attachment.rb +4 -2
  30. data/lib/arena/entities/avatar.rb +8 -6
  31. data/lib/arena/entities/embed.rb +7 -5
  32. data/lib/arena/entities/image.rb +7 -2
  33. data/lib/arena/entities/provider.rb +2 -0
  34. data/lib/arena/entities/source.rb +2 -0
  35. data/lib/arena/entities/version.rb +2 -0
  36. data/lib/arena/entity.rb +2 -0
  37. data/lib/arena/error/cache_error.rb +4 -2
  38. data/lib/arena/error.rb +2 -0
  39. data/lib/arena/feed.rb +19 -18
  40. data/lib/arena/group.rb +6 -4
  41. data/lib/arena/results.rb +3 -1
  42. data/lib/arena/search_results.rb +16 -11
  43. data/lib/arena/story.rb +18 -15
  44. data/lib/arena/summary.rb +4 -2
  45. data/lib/arena/user.rb +12 -10
  46. data/lib/arena/user_feed.rb +9 -8
  47. data/lib/arena/version.rb +3 -1
  48. data/lib/arena.rb +12 -14
  49. data/test/cassettes/Arena/channel/handles_a_400_error_that_returns_JSON.yml +39 -0
  50. data/test/cassettes/Arena/channel/handles_a_403_error.yml +39 -0
  51. data/test/lib/arena/api_test.rb +15 -0
  52. data/test/lib/arena/version_test.rb +4 -4
  53. data/test/test_helper.rb +5 -1
  54. metadata +15 -73
  55. data/.travis.yml +0 -6
data/lib/arena/client.rb CHANGED
@@ -1,8 +1,10 @@
1
- require "arena/configurable"
2
- require "arena/error"
3
- require "arena/api"
4
- require "httparty"
5
- require "json"
1
+ # frozen_string_literal: true
2
+
3
+ require 'arena/configurable'
4
+ require 'arena/error'
5
+ require 'arena/api'
6
+ require 'httparty'
7
+ require 'json'
6
8
 
7
9
  module Arena
8
10
  class Client
@@ -10,7 +12,7 @@ module Arena
10
12
  include Arena::Configurable
11
13
  include Arena::API
12
14
 
13
- def initialize(options={})
15
+ def initialize(options = {})
14
16
  Arena::Configurable.keys.each do |key|
15
17
  instance_variable_set(:"@#{key}", options[key] || Arena.instance_variable_get(:"@#{key}"))
16
18
  end
@@ -18,23 +20,23 @@ module Arena
18
20
 
19
21
  # Performs HTTP GET POST PUT and DELETE requests
20
22
  #
21
- %w(get post put delete).each do |method|
22
- define_method method do |path, options={}|
23
+ %w[get post put delete].each do |method|
24
+ define_method method do |path, options = {}|
23
25
  options = { query: options, headers: set_headers }
24
26
 
25
27
  request(__method__, path, options)
26
28
  end
27
29
  end
28
30
 
29
- private
31
+ private
30
32
 
31
33
  def set_headers
32
34
  if !@access_token.nil?
33
- { "Authorization" => "Bearer #{@access_token}" }
35
+ { 'Authorization' => "Bearer #{@access_token}" }
34
36
  elsif !@auth_token.nil?
35
- { "X-AUTH-TOKEN" => @auth_token }
37
+ { 'X-AUTH-TOKEN' => @auth_token }
36
38
  else
37
- { }
39
+ {}
38
40
  end
39
41
  end
40
42
 
@@ -48,13 +50,20 @@ module Arena
48
50
 
49
51
  def request(method, path, options)
50
52
  response = self.class.send(method, "https://#{@base_domain}/#{@api_version}#{path}", options)
51
- parsed = JSON.parse(response.body)
52
53
 
53
- raise Arena::Error.new(error_message(parsed)) if error?(response)
54
+ parsed = if response.content_type&.include?('application/json')
55
+ begin
56
+ JSON.parse(response.body)
57
+ rescue JSON::ParserError, TypeError
58
+ nil
59
+ end
60
+ else
61
+ response.body
62
+ end
63
+
64
+ raise Arena::Error, error_message(parsed) if error?(response)
54
65
 
55
66
  parsed
56
- rescue JSON::ParserError, TypeError
57
- nil
58
67
  end
59
68
  end
60
69
  end
data/lib/arena/comment.rb CHANGED
@@ -1,6 +1,8 @@
1
- require "arena/base"
2
- require "arena/user"
3
- require "arena/creatable"
1
+ # frozen_string_literal: true
2
+
3
+ require 'arena/base'
4
+ require 'arena/user'
5
+ require 'arena/creatable'
4
6
 
5
7
  module Arena
6
8
  class Comment < Arena::Base
@@ -9,11 +11,11 @@ module Arena
9
11
  attr_reader :id, :commentable_id, :commentable_type, :body
10
12
 
11
13
  def _class
12
- @_class ||= @attrs["class"]
14
+ @_class ||= @attrs['class']
13
15
  end
14
16
 
15
17
  def user
16
- @user ||= Arena::User.new(@attrs["user"])
18
+ @user ||= Arena::User.new(@attrs['user'])
17
19
  end
18
20
  end
19
21
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Arena
2
4
  module Configurable
3
5
  attr_accessor :base_domain,
@@ -12,21 +14,21 @@ module Arena
12
14
 
13
15
  class << self
14
16
  def keys
15
- @keys ||= [
16
- :base_domain,
17
- :api_version,
18
- :application_id,
19
- :application_secret,
20
- :access_token,
21
- :auth_token,
22
- :use_caching,
23
- :expires_in
17
+ @keys ||= %i[
18
+ base_domain
19
+ api_version
20
+ application_id
21
+ application_secret
22
+ access_token
23
+ auth_token
24
+ use_caching
25
+ expires_in
24
26
  ]
25
27
  end
26
28
  end
27
29
 
28
30
  # Allows configuration options to be set in a block
29
- #
31
+ #
30
32
  def configure
31
33
  yield self
32
34
 
@@ -37,16 +39,16 @@ module Arena
37
39
  Arena::Configurable.keys.each do |key|
38
40
  instance_variable_set(:"@#{key}", Arena::Default.options[key])
39
41
  end
40
-
42
+
41
43
  self
42
44
  end
43
45
 
44
46
  alias setup reset!
45
47
 
46
- private
48
+ private
47
49
 
48
50
  def options
49
- Hash[Arena::Configurable.keys.map{|key| [key, instance_variable_get(:"@#{key}")]}]
51
+ Arena::Configurable.keys.to_h { |key| [key, instance_variable_get(:"@#{key}")] }
50
52
  end
51
53
  end
52
54
  end
@@ -1,37 +1,43 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'time'
2
4
 
3
5
  module Arena
4
6
  module Connectable
5
7
  def user
6
- @user ||= Arena::User.new(@attrs['user']) if !@attrs['user'].nil?
8
+ @user ||= Arena::User.new(@attrs['user']) unless @attrs['user'].nil?
7
9
  end
8
10
 
9
- %w(position selected connection_id).each do |method|
11
+ %w[position selected connection_id].each do |method|
10
12
  define_method method do
11
13
  instance_variable_set("@#{method}", @attrs[method]) unless instance_variable_get "@#{method}"
12
14
  instance_variable_get "@#{method}"
13
15
  end
14
16
  end
15
17
 
16
- %w(image text link media attachment channel).each do |kind|
18
+ %w[image text link media attachment channel].each do |kind|
17
19
  define_method "is_#{kind}?" do
18
20
  _class == kind.to_s.capitalize
19
21
  end
20
22
  end
21
23
 
22
24
  # Detect optional portions of the response
23
- %w(image attachment embed).each do |kind|
25
+ %w[image attachment embed].each do |kind|
24
26
  define_method "has_#{kind}?" do
25
27
  !@attrs[kind.to_s].nil?
26
28
  end
27
29
  end
28
30
 
29
31
  def is_block?
30
- _base_class == "Block"
32
+ _base_class == 'Block'
31
33
  end
32
34
 
33
35
  def connections
34
- @connections ||= @attrs['connections'].collect { |channel| Arena::Channel.new(channel) } if !@attrs['connections'].nil?
36
+ return if @attrs['connections'].nil?
37
+
38
+ @connections ||= @attrs['connections'].collect do |channel|
39
+ Arena::Channel.new(channel)
40
+ end
35
41
  end
36
42
 
37
43
  def connected_at
@@ -43,17 +49,19 @@ module Arena
43
49
  end
44
50
 
45
51
  def connected_by
52
+ return unless connected?
53
+
46
54
  @connected_by ||= Arena::User.new({
47
- 'id' => @attrs['connected_by_user_id'],
48
- 'username' => @attrs['connected_by_username'],
49
- 'full_name' => @attrs['connected_by_username']
50
- }) if connected?
55
+ 'id' => @attrs['connected_by_user_id'],
56
+ 'username' => @attrs['connected_by_username'],
57
+ 'full_name' => @attrs['connected_by_username']
58
+ })
51
59
  end
52
60
 
53
- private
61
+ private
54
62
 
55
63
  def connected?
56
64
  !@attrs['connected_at'].nil?
57
65
  end
58
- end # Connectable
59
- end # Arena
66
+ end
67
+ end
@@ -1,10 +1,12 @@
1
- require "arena/base"
1
+ # frozen_string_literal: true
2
+
3
+ require 'arena/base'
2
4
 
3
5
  module Arena
4
6
  class Connection < Arena::Base
5
7
  include Arena::Creatable
6
8
 
7
9
  attr_reader :id, :position, :selected, :user_id,
8
- :connectable_id, :connectable_type, :channel_id
9
- end # Connection
10
- end # Arena
10
+ :connectable_id, :connectable_type, :channel_id
11
+ end
12
+ end
@@ -1,5 +1,9 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class String
2
- def constantize
3
- self.split("::").inject(Module) {|acc, val| acc.const_get(val)}
4
- end unless method_defined?(:constantize)
4
+ unless method_defined?(:constantize)
5
+ def constantize
6
+ split('::').inject(Module) { |acc, val| acc.const_get(val) }
7
+ end
8
+ end
5
9
  end
@@ -1,8 +1,9 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'time'
2
4
 
3
5
  module Arena
4
6
  module Creatable
5
-
6
7
  # Time when the object was created on Arena
7
8
  #
8
9
  # @return [Time]
@@ -17,7 +18,7 @@ module Arena
17
18
  @updated_at ||= Time.parse(@attrs['updated_at']) if updated?
18
19
  end
19
20
 
20
- private
21
+ private
21
22
 
22
23
  def created?
23
24
  !@attrs['created_at'].nil?
@@ -26,6 +27,5 @@ module Arena
26
27
  def updated?
27
28
  !@attrs['updated_at'].nil?
28
29
  end
29
-
30
30
  end
31
31
  end
data/lib/arena/default.rb CHANGED
@@ -1,13 +1,15 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Arena
2
4
  module Default
3
- BASE_DOMAIN = "api.are.na" unless defined? BASE_DOMAIN
4
- API_VERSION = "v2" unless defined? API_VERSION
5
+ BASE_DOMAIN = 'api.are.na' unless defined? BASE_DOMAIN
6
+ API_VERSION = 'v2' unless defined? API_VERSION
5
7
  USE_CACHING = false unless defined? USE_CACHING
6
8
  EXPIRES_IN = 600 unless defined? EXPIRES_IN # 10 minutes
7
9
 
8
10
  class << self
9
11
  def options
10
- Hash[Arena::Configurable.keys.map{|key| [key, send(key)]}]
12
+ Arena::Configurable.keys.to_h { |key| [key, send(key)] }
11
13
  end
12
14
 
13
15
  def api_version
@@ -19,19 +21,19 @@ module Arena
19
21
  end
20
22
 
21
23
  def application_id
22
- ENV["APPLICATION_ID"]
24
+ ENV.fetch('APPLICATION_ID', nil)
23
25
  end
24
26
 
25
27
  def application_secret
26
- ENV["APPLICATION_SECRET"]
28
+ ENV.fetch('APPLICATION_SECRET', nil)
27
29
  end
28
30
 
29
31
  def access_token
30
- ENV["APPLICATION_SECRET"]
32
+ ENV.fetch('APPLICATION_SECRET', nil)
31
33
  end
32
34
 
33
35
  def auth_token
34
- ENV["AUTH_TOKEN"]
36
+ ENV.fetch('AUTH_TOKEN', nil)
35
37
  end
36
38
 
37
39
  def use_caching
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'arena/entity'
2
4
  require 'arena/entities/version'
3
5
 
@@ -5,7 +7,7 @@ module Arena
5
7
  class Entity
6
8
  class Attachment < Arena::Entity
7
9
  attr_reader :file_name, :file_size, :file_size_display,
8
- :content_type, :extension, :url
10
+ :content_type, :extension, :url
9
11
  end
10
12
  end
11
- end
13
+ end
@@ -1,13 +1,15 @@
1
- require "arena/entity"
1
+ # frozen_string_literal: true
2
+
3
+ require 'arena/entity'
2
4
 
3
5
  module Arena
4
6
  class Entity
5
7
  class Avatar < Arena::Entity
6
8
  attr_reader :thumb, :display
7
9
 
8
- def custom(size=40)
9
- thumb.gsub("s=40", "s=#{size}")
10
+ def custom(size = 40)
11
+ thumb.gsub('s=40', "s=#{size}")
10
12
  end
11
- end # Avatar
12
- end # Entity
13
- end # Arena
13
+ end
14
+ end
15
+ end
@@ -1,10 +1,12 @@
1
- require "arena/entity"
1
+ # frozen_string_literal: true
2
+
3
+ require 'arena/entity'
2
4
 
3
5
  module Arena
4
6
  class Entity
5
7
  class Embed < Arena::Entity
6
8
  attr_reader :embed, :url, :type, :title, :author_name, :author_url,
7
- :source_url, :thumbnail_url, :width, :height, :html
8
- end # Embed
9
- end # Entity
10
- end # Arena
9
+ :source_url, :thumbnail_url, :width, :height, :html
10
+ end
11
+ end
12
+ end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'arena/creatable'
2
4
  require 'arena/entity'
3
5
  require 'arena/entities/version'
@@ -9,9 +11,12 @@ module Arena
9
11
 
10
12
  attr_reader :filename, :content_type
11
13
 
12
- %w(thumb square display large original).each do |method|
14
+ %w[thumb square display large original].each do |method|
13
15
  define_method method do
14
- instance_variable_set("@#{method}", Arena::Entity::Version.new(@attrs[method])) unless instance_variable_get "@#{method}"
16
+ unless instance_variable_get "@#{method}"
17
+ instance_variable_set("@#{method}",
18
+ Arena::Entity::Version.new(@attrs[method]))
19
+ end
15
20
  instance_variable_get "@#{method}"
16
21
  end
17
22
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'arena/entity'
2
4
 
3
5
  module Arena
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'arena/entity'
2
4
  require 'arena/entities/provider'
3
5
 
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'arena/entity'
2
4
 
3
5
  module Arena
data/lib/arena/entity.rb CHANGED
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'arena/base'
2
4
 
3
5
  module Arena
@@ -1,9 +1,11 @@
1
- require "arena/error"
1
+ # frozen_string_literal: true
2
+
3
+ require 'arena/error'
2
4
 
3
5
  module Arena
4
6
  class Error
5
7
  # Raised when Cache engine is unavailable
6
- #
8
+ #
7
9
  class CacheError < Arena::Error
8
10
  end
9
11
  end
data/lib/arena/error.rb CHANGED
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Arena
2
4
  class Error < StandardError
3
5
  end
data/lib/arena/feed.rb CHANGED
@@ -1,36 +1,37 @@
1
- require "arena/base"
2
- require "arena/story"
3
- require "arena/group"
1
+ # frozen_string_literal: true
2
+
3
+ require 'arena/base'
4
+ require 'arena/story'
5
+ require 'arena/group'
4
6
 
5
7
  module Arena
6
8
  class Feed < Arena::Base
7
9
  attr_reader :type, :limit, :total, :offset, :range_start, :range_end
8
10
 
9
11
  def stories
10
- @stories ||= @attrs["items"].collect { |item| Arena::Story.new(item) } if stories?
12
+ @stories ||= @attrs['items'].collect { |item| Arena::Story.new(item) } if stories?
11
13
  end
12
-
14
+
13
15
  alias items stories
14
16
 
15
17
  def groups
16
- if stories?
17
- groups = items.group_by do |item|
18
- target = "#{item.target.id}_" if !item.target.nil?
18
+ return unless stories?
19
19
 
20
- "#{item.user.id}_#{target}#{item.action.gsub(' ', '_')}"
21
- end
20
+ groups = items.group_by do |item|
21
+ target = "#{item.target.id}_" unless item.target.nil?
22
22
 
23
- @groups ||=
24
- groups.collect { |key, stories| Arena::Group.new(key, stories) }.
25
- sort_by { |group| group.summary.indicative.created_at }.reverse!
23
+ "#{item.user.id}_#{target}#{item.action.gsub(' ', '_')}"
26
24
  end
25
+
26
+ @groups ||=
27
+ groups.collect { |key, stories| Arena::Group.new(key, stories) }
28
+ .sort_by { |group| group.summary.indicative.created_at }.reverse!
27
29
  end
28
30
 
29
- private
31
+ private
30
32
 
31
33
  def stories?
32
- !@attrs["items"].nil?
34
+ !@attrs['items'].nil?
33
35
  end
34
-
35
- end # Feed
36
- end # Arena
36
+ end
37
+ end
data/lib/arena/group.rb CHANGED
@@ -1,5 +1,7 @@
1
- require "arena/story"
2
- require "arena/summary"
1
+ # frozen_string_literal: true
2
+
3
+ require 'arena/story'
4
+ require 'arena/summary'
3
5
 
4
6
  module Arena
5
7
  class Group
@@ -17,5 +19,5 @@ module Arena
17
19
  def summary
18
20
  @summary ||= Arena::Summary.new(stories)
19
21
  end
20
- end # Group
21
- end # Arena
22
+ end
23
+ end
data/lib/arena/results.rb CHANGED
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'arena/base'
2
4
  require 'arena/channel'
3
5
 
@@ -11,4 +13,4 @@ module Arena
11
13
  @channels ||= @attrs['channels'].collect { |channel| Arena::Channel.new(channel) }
12
14
  end
13
15
  end
14
- end
16
+ end
@@ -1,8 +1,10 @@
1
- require "arena/core_ext/string"
2
- require "arena/base"
3
- require "arena/channel"
4
- require "arena/user"
5
- require "arena/block"
1
+ # frozen_string_literal: true
2
+
3
+ require 'arena/core_ext/string'
4
+ require 'arena/base'
5
+ require 'arena/channel'
6
+ require 'arena/user'
7
+ require 'arena/block'
6
8
 
7
9
  module Arena
8
10
  class SearchResults < Arena::Base
@@ -13,13 +15,16 @@ module Arena
13
15
  :length,
14
16
  :authenticated
15
17
 
16
- %w(user channel block).each do |method|
18
+ %w[user channel block].each do |method|
17
19
  define_method "#{method}s" do
18
- instance_variable_set("@#{method}s",
19
- @attrs["#{method}s"].collect { |element| "Arena::#{method.capitalize}".constantize.new(element) }
20
- ) unless instance_variable_get "@#{method}s"
20
+ unless instance_variable_get "@#{method}s"
21
+ instance_variable_set("@#{method}s",
22
+ @attrs["#{method}s"].collect do |element|
23
+ "Arena::#{method.capitalize}".constantize.new(element)
24
+ end)
25
+ end
21
26
  instance_variable_get "@#{method}s"
22
27
  end
23
28
  end
24
- end # SearchResults
25
- end # Arena
29
+ end
30
+ end
data/lib/arena/story.rb CHANGED
@@ -1,9 +1,11 @@
1
- require "arena/base"
2
- require "arena/creatable"
3
- require "arena/user"
4
- require "arena/block"
5
- require "arena/channel"
6
- require "arena/connection"
1
+ # frozen_string_literal: true
2
+
3
+ require 'arena/base'
4
+ require 'arena/creatable'
5
+ require 'arena/user'
6
+ require 'arena/block'
7
+ require 'arena/channel'
8
+ require 'arena/connection'
7
9
 
8
10
  module Arena
9
11
  class Story < Arena::Base
@@ -12,20 +14,21 @@ module Arena
12
14
  attr_reader :id, :action, :bulletin_id, :connector, :created_at
13
15
 
14
16
  def user
15
- @user ||= Arena::User.new(@attrs["user"])
17
+ @user ||= Arena::User.new(@attrs['user'])
16
18
  end
17
19
 
18
- %w(item target parent).each do |method|
20
+ %w[item target parent].each do |method|
19
21
  define_method method do
20
- if !@attrs[method].nil?
21
- type = "#{method}_type"
22
+ return if @attrs[method].nil?
23
+
24
+ type = "#{method}_type"
22
25
 
26
+ unless instance_variable_get "@#{method}"
23
27
  instance_variable_set("@#{method}",
24
- "Arena::#{@attrs[type]}".constantize.new(@attrs[method])
25
- ) unless instance_variable_get "@#{method}"
26
- instance_variable_get "@#{method}"
28
+ "Arena::#{@attrs[type]}".constantize.new(@attrs[method]))
27
29
  end
30
+ instance_variable_get "@#{method}"
28
31
  end
29
32
  end
30
- end # Story
31
- end # Arena
33
+ end
34
+ end
data/lib/arena/summary.rb CHANGED
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Arena
2
4
  class Summary
3
5
  attr_reader :indicative, :kinds, :size
@@ -7,5 +9,5 @@ module Arena
7
9
  @kinds = stories.collect { |story| story.item._class }.uniq
8
10
  @size = stories.size
9
11
  end
10
- end # Summary
11
- end # Arena
12
+ end
13
+ end