arena 0.0.2 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -17,6 +17,13 @@ module Arena
17
17
  @client = Arena::Client.new(options)
18
18
  end
19
19
  end
20
+
21
+ private
22
+
23
+ def method_missing(method, *args, &block)
24
+ self.client.send(method, *args, &block)
25
+ end
26
+
20
27
  end
21
28
  end
22
29
 
@@ -0,0 +1,80 @@
1
+ require 'arena/user'
2
+ require 'arena/block'
3
+ require 'arena/channel'
4
+ require 'arena/results'
5
+ require 'arena/search_results'
6
+
7
+ module Arena
8
+ module API
9
+ # Returns paginated global list of Channels
10
+ def channels(options={})
11
+ object_from_response(Arena::ChannelResults, :get, "/channels", options)
12
+ end
13
+
14
+ # Returns a full Channel representation
15
+ def channel(id, options={})
16
+ object_from_response(Arena::Channel, :get, "/channels/#{id}", options)
17
+ end
18
+
19
+ # Posts a block to a channel
20
+ def channel_add_block(id, options={})
21
+ object_from_response(Arena::Block, :post, "/channels/#{id}/blocks", options)
22
+ end
23
+
24
+ # Remove a block from channel (destroy connection)
25
+ def channel_remove_block(channel_id, id, options={})
26
+ send(:delete, "/channels/#{channel_id}/blocks/#{id}", options)
27
+ end
28
+
29
+ # Select a block within the context of a channel
30
+ def channel_toggle_block_selection(id, options={})
31
+ send(:put, "/connections/#{id}/selection", options)
32
+ end
33
+
34
+ # Returns thumbnail representation (first 9 Blocks) for the given Channel
35
+ def channel_thumb(id, options={})
36
+ object_from_response(Arena::Channel, :get, "/channels/#{id}/thumb", options)
37
+ end
38
+
39
+ # Returns a paginated list of all Channels connected to the given Channel
40
+ def channel_channels(id, options={})
41
+ object_from_response(Arena::ChannelResults, :get, "/channels/#{id}/channels", options)
42
+ end
43
+
44
+ # Returns a full Block representation
45
+ def block(id, options={})
46
+ object_from_response(Arena::Block, :get, "/blocks/#{id}", options)
47
+ end
48
+
49
+ # Returns a User representation
50
+ def user(id, options={})
51
+ object_from_response(Arena::User, :get, "/users/#{id}", options)
52
+ end
53
+
54
+ # Returns a paginated list of all Channels a User has access to
55
+ def user_channels(id, options={})
56
+ object_from_response(Arena::ChannelResults, :get, "/users/#{id}/channels", options)
57
+ end
58
+
59
+ # Returns a Search object with an Array for Channels, Blocks and Users
60
+ def search(query, option={})
61
+ object_from_response(Arena::SearchResults, :get, "/search?q=#{query}", options)
62
+ end
63
+
64
+ private
65
+
66
+ def object_from_response(klass, request_method, url, options={})
67
+ response = send(request_method.to_sym, url, options)
68
+ klass.new(response)
69
+ end
70
+
71
+ def collection_from_array(klass, array)
72
+ array.collect { |element| klass.new(element) }
73
+ end
74
+
75
+ def collection_from_response(klass, request_method, url, options={}, selector)
76
+ collection_from_array(klass, send(request_method.to_sym, url, options)[selector])
77
+ end
78
+
79
+ end
80
+ end
@@ -0,0 +1,52 @@
1
+ module Arena
2
+ class Base
3
+ attr_reader :attrs
4
+ alias to_hash attrs
5
+
6
+ # Define methods that retrieve the value from an initialized instance variable Hash, using the attribute as a key
7
+ #
8
+ # @param attrs [Array, Set, Symbol]
9
+ def self.attr_reader(*attrs)
10
+ attrs.each do |attribute|
11
+ class_eval do
12
+ define_method attribute do
13
+ @attrs[attribute.to_s]
14
+ end
15
+ end
16
+ end
17
+ end
18
+
19
+ # Initializes a new object
20
+ #
21
+ # @param attrs [Hash]
22
+ # @return [Arena::Base]
23
+ def initialize(attrs={})
24
+ @attrs = attrs
25
+ end
26
+
27
+ # Update the attributes of an object
28
+ #
29
+ # @param attrs [Hash]
30
+ # @return [Arena::Base]
31
+ # def update(attrs)
32
+ # @attrs.update(attrs)
33
+ # self
34
+ # end
35
+
36
+ protected
37
+
38
+ # @param attr [Symbol]
39
+ # @param other [Arena::Base]
40
+ # @return [Boolean]
41
+ def attr_equal(attr, other)
42
+ self.class == other.class && !other.send(attr).nil? && send(attr) == other.send(attr)
43
+ end
44
+
45
+ # @param other [Arena::Base]
46
+ # @return [Boolean]
47
+ def attrs_equal(other)
48
+ self.class == other.class && !other.attrs.empty? && attrs == other.attrs
49
+ end
50
+
51
+ end
52
+ end
@@ -0,0 +1,51 @@
1
+ require 'arena/base'
2
+ require 'arena/entity'
3
+ require 'arena/entities/source'
4
+ require 'arena/entities/image'
5
+ require 'arena/entities/attachment'
6
+ require 'arena/creatable'
7
+
8
+ module Arena
9
+ class Block < Arena::Base
10
+ include Arena::Creatable
11
+
12
+ attr_reader :id, :title, :generated_title, :state, :comment_count, :content,
13
+ :content_html, :description, :description_html, :position, :selected,
14
+ :connection_id, :connected_at, :connected_by_user_id, :connected_by_username
15
+
16
+ def user
17
+ @user ||= Arena::User.new(@attrs.dup['user'])
18
+ end
19
+
20
+ def _class
21
+ @_class ||= @attrs.dup['class']
22
+ end
23
+
24
+ def _base_class
25
+ @_base_class ||= @attrs.dup['base_class']
26
+ end
27
+
28
+ def source
29
+ @source ||= Arena::Entity::Source.new(@attrs.dup['source'])
30
+ end
31
+
32
+ def image
33
+ @image ||= Arena::Entity::Image.new(@attrs.dup['image'])
34
+ end
35
+
36
+ def attachment
37
+ @attachment ||= Arena::Entity::Attachment.new(@attrs.dup['attachment'])
38
+ end
39
+
40
+ def connections
41
+ @connections ||= @attrs['connections'].collect { |channel| Arena::Channel.new(channel) }
42
+ end
43
+
44
+ end
45
+
46
+ class Text < Block; end
47
+ class Image < Block; end
48
+ class Link < Block; end
49
+ class Media < Block; end
50
+ class Attachment < Block; end
51
+ end
@@ -0,0 +1,35 @@
1
+ require 'arena/base'
2
+ require 'arena/creatable'
3
+ require 'arena/user'
4
+ require 'arena/block'
5
+
6
+ module Arena
7
+ class Channel < Arena::Base
8
+ include Arena::Creatable
9
+
10
+ attr_reader :id, :title, :published, :open, :collaboration,
11
+ :slug, :length, :kind, :status, :user_id, :total_pages,
12
+ :current_page, :per, :follower_count
13
+
14
+ def user
15
+ @user ||= Arena::User.new(@attrs['user'])
16
+ end
17
+
18
+ def _class
19
+ @attrs['class']
20
+ end
21
+
22
+ def _base_class
23
+ @attrs['base_class']
24
+ end
25
+
26
+ def contents
27
+ @contents ||= @attrs['contents'].collect { |object| "Arena::#{object['class']}".constantize.new(object) }
28
+ end
29
+
30
+ def collaborators
31
+ @collaborators ||= @attrs['collaborators'].collect { |user| Arena::User.new(user) }
32
+ end
33
+
34
+ end
35
+ end
@@ -1,4 +1,6 @@
1
1
  require 'arena/configurable'
2
+ require 'arena/error'
3
+ require 'arena/api'
2
4
  require 'httparty'
3
5
  require 'json'
4
6
 
@@ -7,6 +9,7 @@ module Arena
7
9
  class Client
8
10
  include HTTParty
9
11
  include Arena::Configurable
12
+ include Arena::API
10
13
 
11
14
  def initialize(options={})
12
15
  Arena::Configurable.keys.each do |key|
@@ -14,30 +17,31 @@ module Arena
14
17
  end
15
18
  end
16
19
 
17
- # Wrapper for v1
18
- def channel(identifier, options={})
19
- get_json "/channels/#{identifier}", options
20
+ # Performs HTTP GET POST PUT and DELETE requests
21
+ %w(get post put delete).each do |method|
22
+ define_method method do |path, options={}|
23
+ options = { query: options,
24
+ headers: { 'Authorization' => "Bearer #{@access_token}" } }
25
+
26
+ request(__method__, path, options)
27
+ end
20
28
  end
21
29
 
22
- def users_channels(id, options={})
23
- get_json "/channels?user=#{id}", options
24
- end
30
+ private
25
31
 
26
- def block(id, options={})
27
- get_json "/blocks/#{id}", options
28
- end
32
+ def request(method, path, options)
33
+ JSON.parse self.class.send(method, "http://#{@base_domain}/#{@api_version}#{path}", options).body
29
34
 
30
- def blocks(identifier, options={})
31
- get_json "/blocks?channel=#{identifier}", options
32
- end
35
+ rescue JSON::ParserError, TypeError
36
+ nil
33
37
 
34
- private
38
+ rescue Exception => exception
39
+ puts exception.inspect
40
+ puts exception.backtrace
35
41
 
36
- def get_json(path, options)
37
- JSON.parse(
38
- (self.class.get "http://#{@base_domain}/api/#{@api_version}#{path}", options).body
39
- )
42
+ raise Arena::Error
40
43
  end
44
+
41
45
  end
42
46
 
43
- end
47
+ end
@@ -1,16 +1,17 @@
1
1
  module Arena
2
2
  module Configurable
3
-
4
- CONFIG_KEYS = [
5
- :base_domain,
6
- :api_version,
7
- ] unless defined? CONFIG_KEYS
8
-
9
- attr_accessor *CONFIG_KEYS
3
+ attr_writer :application_id, :application_secret, :access_token
4
+ attr_accessor :base_domain, :api_version
10
5
 
11
6
  class << self
12
7
  def keys
13
- @keys ||= CONFIG_KEYS
8
+ @keys ||= [
9
+ :base_domain,
10
+ :api_version,
11
+ :application_id,
12
+ :application_secret,
13
+ :access_token
14
+ ]
14
15
  end
15
16
  end
16
17
 
@@ -38,4 +39,4 @@ module Arena
38
39
  end
39
40
 
40
41
  end
41
- end
42
+ end
@@ -0,0 +1,7 @@
1
+ class String
2
+
3
+ def constantize
4
+ self.split("::").inject(Module) {|acc, val| acc.const_get(val)}
5
+ end unless method_defined?(:constantize)
6
+
7
+ end
@@ -0,0 +1,31 @@
1
+ require 'time'
2
+
3
+ module Arena
4
+ module Creatable
5
+
6
+ # Time when the object was created on Arena
7
+ #
8
+ # @return [Time]
9
+ def created_at
10
+ @created_at ||= Time.parse(@attrs['created_at']) if created?
11
+ end
12
+
13
+ # Time when the object was last updated on Arena
14
+ #
15
+ # @return [Time]
16
+ def updated_at
17
+ @updated_at ||= Time.parse(@attrs['updated_at']) if updated?
18
+ end
19
+
20
+ private
21
+
22
+ def created?
23
+ !@attrs['created_at'].nil?
24
+ end
25
+
26
+ def updated?
27
+ !@attrs['updated_at'].nil?
28
+ end
29
+
30
+ end
31
+ end
@@ -1,7 +1,7 @@
1
1
  module Arena
2
2
  module Default
3
- BASE_DOMAIN = "are.na" unless defined? BASE_DOMAIN
4
- API_VERSION = "v1" unless defined? API_VERSION
3
+ BASE_DOMAIN = "api.are.na" unless defined? BASE_DOMAIN
4
+ API_VERSION = "v2" unless defined? API_VERSION
5
5
 
6
6
  class << self
7
7
  # @return [Hash]
@@ -16,6 +16,18 @@ module Arena
16
16
  def base_domain
17
17
  BASE_DOMAIN
18
18
  end
19
+
20
+ def application_id
21
+ ENV['APPLICATION_ID']
22
+ end
23
+
24
+ def application_secret
25
+ ENV['APPLICATION_SECRET']
26
+ end
27
+
28
+ def access_token
29
+ ENV['APPLICATION_SECRET']
30
+ end
19
31
  end
20
32
  end
21
33
  end
@@ -0,0 +1,11 @@
1
+ require 'arena/entity'
2
+ require 'arena/entities/version'
3
+
4
+ module Arena
5
+ class Entity
6
+ class Attachment < Arena::Entity
7
+ attr_reader :file_name, :file_size, :file_size_display,
8
+ :content_type, :extension, :url
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,20 @@
1
+ require 'arena/creatable'
2
+ require 'arena/entity'
3
+ require 'arena/entities/version'
4
+
5
+ module Arena
6
+ class Entity
7
+ class Image < Arena::Entity
8
+ include Arena::Creatable
9
+
10
+ attr_reader :filename, :content_type
11
+
12
+ %w(thumb display original).each do |method|
13
+ define_method method do
14
+ instance_variable_set("@#{method}", Arena::Entity::Version.new(@attrs.dup[method])) unless instance_variable_get "@#{method}"
15
+ instance_variable_get "@#{method}"
16
+ end
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,9 @@
1
+ require 'arena/entity'
2
+
3
+ module Arena
4
+ class Entity
5
+ class Provider < Arena::Entity
6
+ attr_reader :name, :url
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,14 @@
1
+ require 'arena/entity'
2
+ require 'arena/entities/provider'
3
+
4
+ module Arena
5
+ class Entity
6
+ class Source < Arena::Entity
7
+ attr_reader :url
8
+
9
+ def provider
10
+ @provider ||= Arena::Entity::Provider.new(@attrs.dup['provider'])
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,9 @@
1
+ require 'arena/entity'
2
+
3
+ module Arena
4
+ class Entity
5
+ class Version < Arena::Entity
6
+ attr_reader :url, :file_size, :file_display
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,6 @@
1
+ require 'arena/base'
2
+
3
+ module Arena
4
+ class Entity < Arena::Base
5
+ end
6
+ end
@@ -0,0 +1,4 @@
1
+ module Arena
2
+ class Error < StandardError
3
+ end
4
+ end
@@ -0,0 +1,14 @@
1
+ require 'arena/base'
2
+ require 'arena/channel'
3
+
4
+ module Arena
5
+ class Results < Arena::Base
6
+ attr_reader :length, :total_pages, :current_page, :per
7
+ end
8
+
9
+ class ChannelResults < Results
10
+ def channels
11
+ @channels ||= @attrs.dup['channels'].collect { |channel| Arena::Channel.new(channel) }
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,22 @@
1
+ require 'arena/core_ext/string'
2
+ require 'arena/base'
3
+ require 'arena/channel'
4
+ require 'arena/user'
5
+ require 'arena/block'
6
+
7
+ module Arena
8
+ class SearchResults < Arena::Base
9
+ def term
10
+ @attrs['term'] unless @attrs['term'].nil?
11
+ end
12
+
13
+ %w(user channel block).each do |method|
14
+ define_method "#{method}s" do
15
+ instance_variable_set("@#{method}s",
16
+ @attrs.dup["#{method}s"].collect { |element| "Arena::#{method.capitalize}".constantize.new(element) }
17
+ ) unless instance_variable_get "@#{method}s"
18
+ instance_variable_get "@#{method}s"
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,16 @@
1
+ require 'arena/base'
2
+ require 'arena/creatable'
3
+
4
+ module Arena
5
+ class User < Arena::Base
6
+ include Arena::Creatable
7
+
8
+ attr_reader :id, :slug, :username, :first_name, :last_name,
9
+ :full_name, :avatar, :email, :channel_count, :following_count,
10
+ :profile_id, :follower_count, :initials
11
+
12
+ def _class
13
+ @attrs.dup['class']
14
+ end
15
+ end
16
+ end
@@ -1,3 +1,7 @@
1
1
  module Arena
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.4"
3
+
4
+ def self.version
5
+ VERSION
6
+ end
3
7
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: arena
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.4
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-08-07 00:00:00.000000000 Z
12
+ date: 2012-12-03 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rake
@@ -73,9 +73,25 @@ files:
73
73
  - Rakefile
74
74
  - arena.gemspec
75
75
  - lib/arena.rb
76
+ - lib/arena/api.rb
77
+ - lib/arena/base.rb
78
+ - lib/arena/block.rb
79
+ - lib/arena/channel.rb
76
80
  - lib/arena/client.rb
77
81
  - lib/arena/configurable.rb
82
+ - lib/arena/core_ext/string.rb
83
+ - lib/arena/creatable.rb
78
84
  - lib/arena/default.rb
85
+ - lib/arena/entities/attachment.rb
86
+ - lib/arena/entities/image.rb
87
+ - lib/arena/entities/provider.rb
88
+ - lib/arena/entities/source.rb
89
+ - lib/arena/entities/version.rb
90
+ - lib/arena/entity.rb
91
+ - lib/arena/error.rb
92
+ - lib/arena/results.rb
93
+ - lib/arena/search_results.rb
94
+ - lib/arena/user.rb
79
95
  - lib/arena/version.rb
80
96
  - test/lib/arena/version_test.rb
81
97
  - test/test_helper.rb