hackeroo 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (52) hide show
  1. checksums.yaml +7 -0
  2. data/Rakefile +11 -0
  3. data/hackeroo.gemspec +26 -0
  4. data/lib/hackeroo.rb +40 -0
  5. data/lib/hackeroo/action/favorite.rb +19 -0
  6. data/lib/hackeroo/action/follow.rb +30 -0
  7. data/lib/hackeroo/action/list_member_added.rb +39 -0
  8. data/lib/hackeroo/action/mention.rb +46 -0
  9. data/lib/hackeroo/action/reply.rb +27 -0
  10. data/lib/hackeroo/action/retweet.rb +27 -0
  11. data/lib/hackeroo/action/tweet.rb +20 -0
  12. data/lib/hackeroo/api/arguments.rb +13 -0
  13. data/lib/hackeroo/api/artists.rb +22 -0
  14. data/lib/hackeroo/api/performances.rb +22 -0
  15. data/lib/hackeroo/api/search.rb +37 -0
  16. data/lib/hackeroo/api/stages.rb +22 -0
  17. data/lib/hackeroo/api/users.rb +27 -0
  18. data/lib/hackeroo/api/utils.rb +187 -0
  19. data/lib/hackeroo/artist.rb +7 -0
  20. data/lib/hackeroo/base.rb +125 -0
  21. data/lib/hackeroo/client.rb +73 -0
  22. data/lib/hackeroo/configurable.rb +87 -0
  23. data/lib/hackeroo/core_ext/enumerable.rb +10 -0
  24. data/lib/hackeroo/core_ext/kernel.rb +6 -0
  25. data/lib/hackeroo/cursor.rb +87 -0
  26. data/lib/hackeroo/default.rb +77 -0
  27. data/lib/hackeroo/error.rb +32 -0
  28. data/lib/hackeroo/error/bad_gateway.rb +11 -0
  29. data/lib/hackeroo/error/bad_request.rb +10 -0
  30. data/lib/hackeroo/error/client_error.rb +35 -0
  31. data/lib/hackeroo/error/configuration_error.rb +8 -0
  32. data/lib/hackeroo/error/decode_error.rb +9 -0
  33. data/lib/hackeroo/error/forbidden.rb +10 -0
  34. data/lib/hackeroo/error/gateway_timeout.rb +11 -0
  35. data/lib/hackeroo/error/identity_map_key_error.rb +9 -0
  36. data/lib/hackeroo/error/internal_server_error.rb +11 -0
  37. data/lib/hackeroo/error/not_acceptable.rb +10 -0
  38. data/lib/hackeroo/error/not_found.rb +10 -0
  39. data/lib/hackeroo/error/server_error.rb +28 -0
  40. data/lib/hackeroo/error/service_unavailable.rb +11 -0
  41. data/lib/hackeroo/error/too_many_requests.rb +12 -0
  42. data/lib/hackeroo/error/unauthorized.rb +10 -0
  43. data/lib/hackeroo/error/unprocessable_entity.rb +10 -0
  44. data/lib/hackeroo/media/photo.rb +21 -0
  45. data/lib/hackeroo/performance.rb +7 -0
  46. data/lib/hackeroo/rate_limit.rb +45 -0
  47. data/lib/hackeroo/response/parse_json.rb +25 -0
  48. data/lib/hackeroo/response/raise_error.rb +31 -0
  49. data/lib/hackeroo/stage.rb +7 -0
  50. data/lib/hackeroo/user.rb +7 -0
  51. data/lib/hackeroo/version.rb +18 -0
  52. metadata +157 -0
@@ -0,0 +1,10 @@
1
+ module Enumerable
2
+
3
+ def threaded_map
4
+ threads = map do |object|
5
+ Thread.new { yield object }
6
+ end
7
+ threads.map(&:value)
8
+ end
9
+
10
+ end
@@ -0,0 +1,6 @@
1
+ module Kernel
2
+
3
+ # Returns the object's singleton class (exists in Ruby 1.9.2)
4
+ def singleton_class; class << self; self; end; end unless method_defined?(:singleton_class)
5
+
6
+ end
@@ -0,0 +1,87 @@
1
+ require 'hackeroo/core_ext/kernel'
2
+
3
+ module Hackeroo
4
+ class Cursor
5
+ include Enumerable
6
+ attr_reader :attrs, :collection
7
+ alias to_hash attrs
8
+
9
+ # Initializes a new Cursor object
10
+ #
11
+ # @param response [Hash]
12
+ # @param collection_name [String, Symbol] The name of the method to return the collection
13
+ # @param klass [Class] The class to instantiate object in the collection
14
+ # @param client [Hackeroo::Client]
15
+ # @param method_name [String, Symbol]
16
+ # @param method_options [Hash]
17
+ # @return [Hackeroo::Cursor]
18
+ def self.from_response(response, collection_name, klass, client, method_name, method_options)
19
+ new(response[:body], collection_name, klass, client, method_name, method_options)
20
+ end
21
+
22
+ # Initializes a new Cursor
23
+ #
24
+ # @param attrs [Hash]
25
+ # @param collection_name [String, Symbol] The name of the method to return the collection
26
+ # @param klass [Class] The class to instantiate object in the collection
27
+ # @param client [Hackeroo::Client]
28
+ # @param method_name [String, Symbol]
29
+ # @param method_options [Hash]
30
+ # @return [Hackeroo::Cursor]
31
+ def initialize(attrs, collection_name, klass, client, method_name, method_options)
32
+ @attrs = attrs
33
+ @client = client
34
+ @method_name = method_name
35
+ @method_options = method_options
36
+ @collection = Array(attrs[collection_name.to_sym]).map do |item|
37
+ if klass
38
+ klass.fetch_or_new(item)
39
+ else
40
+ item
41
+ end
42
+ end
43
+ singleton_class.class_eval do
44
+ alias_method(collection_name.to_sym, :collection)
45
+ end
46
+ end
47
+
48
+ # @param collection [Array]
49
+ # @param cursor [Integer]
50
+ # @return [Array]
51
+ def all(collection=collection, cursor=next_cursor)
52
+ cursor = @client.send(@method_name.to_sym, @method_options.merge(:cursor => cursor))
53
+ collection += cursor.collection
54
+ cursor.last? ? collection.flatten : all(collection, cursor.next_cursor)
55
+ end
56
+
57
+ # @return [Enumerable]
58
+ def each
59
+ all(collection, next_cursor).each do |element|
60
+ yield element
61
+ end
62
+ end
63
+
64
+ def next_cursor
65
+ @attrs[:next_cursor] || -1
66
+ end
67
+ alias next next_cursor
68
+
69
+ def previous_cursor
70
+ @attrs[:previous_cursor]
71
+ end
72
+ alias previous previous_cursor
73
+
74
+ # @return [Boolean]
75
+ def first?
76
+ previous_cursor.zero?
77
+ end
78
+ alias first first?
79
+
80
+ # @return [Boolean]
81
+ def last?
82
+ next_cursor.zero?
83
+ end
84
+ alias last last?
85
+
86
+ end
87
+ end
@@ -0,0 +1,77 @@
1
+ require 'faraday'
2
+ require 'faraday/request/multipart'
3
+ require 'hackeroo/configurable'
4
+ require 'hackeroo/error/client_error'
5
+ require 'hackeroo/error/server_error'
6
+ require 'hackeroo/response/parse_json'
7
+ require 'hackeroo/response/raise_error'
8
+ require 'hackeroo/version'
9
+
10
+ module Hackeroo
11
+ module Default
12
+ ENDPOINT = 'http://api.hackeroo.io' unless defined? Hackeroo::Default::ENDPOINT
13
+ CONNECTION_OPTIONS = {
14
+ :headers => {
15
+ :accept => 'application/json',
16
+ :user_agent => "Hackeroo Ruby Gem #{Hackeroo::Version}",
17
+ },
18
+ :request => {
19
+ :open_timeout => 5,
20
+ :timeout => 10,
21
+ },
22
+ } unless defined? Hackeroo::Default::CONNECTION_OPTIONS
23
+ IDENTITY_MAP = false unless defined? Hackeroo::Default::IDENTITY_MAP
24
+ MIDDLEWARE = Faraday::Builder.new do |builder|
25
+ # Convert request params to "www-form-urlencoded"
26
+ builder.use Faraday::Request::UrlEncoded
27
+ # Handle 4xx server responses
28
+ builder.use Hackeroo::Response::RaiseError, Hackeroo::Error::ClientError
29
+ # Parse JSON response bodies using MultiJson
30
+ builder.use Hackeroo::Response::ParseJson
31
+ # Handle 5xx server responses
32
+ builder.use Hackeroo::Response::RaiseError, Hackeroo::Error::ServerError
33
+ # Set Faraday's HTTP adapter
34
+ builder.adapter Faraday.default_adapter
35
+ end unless defined? Hackeroo::Default::MIDDLEWARE
36
+
37
+ class << self
38
+
39
+ # @return [Hash]
40
+ def options
41
+ Hash[Hackeroo::Configurable.keys.map{|key| [key, send(key)]}]
42
+ end
43
+
44
+ # @return [String]
45
+ def clientkey
46
+ ENV['HACKEROO_CLIENTKEY']
47
+ end
48
+
49
+ # @note This is configurable in case you want to use a Hackeroo-compatible endpoint.
50
+ # @see http://status.net/wiki/Hackeroo-compatible_API
51
+ # @see http://en.blog.wordpress.com/2009/12/12/hackeroo-api/
52
+ # @see http://staff.tumblr.com/post/287703110/api
53
+ # @see http://developer.typepad.com/typepad-hackeroo-api/hackeroo-api.html
54
+ # @return [String]
55
+ def endpoint
56
+ ENDPOINT
57
+ end
58
+
59
+ def connection_options
60
+ CONNECTION_OPTIONS
61
+ end
62
+
63
+ def identity_map
64
+ IDENTITY_MAP
65
+ end
66
+
67
+ # @note Faraday's middleware stack implementation is comparable to that of Rack middleware. The order of middleware is important: the first middleware on the list wraps all others, while the last middleware is the innermost one.
68
+ # @see https://github.com/technoweenie/faraday#advanced-middleware-usage
69
+ # @see http://mislav.uniqpath.com/2011/07/faraday-advanced-http/
70
+ # @return [Faraday::Builder]
71
+ def middleware
72
+ MIDDLEWARE
73
+ end
74
+
75
+ end
76
+ end
77
+ end
@@ -0,0 +1,32 @@
1
+ module Hackeroo
2
+ # Custom error class for rescuing from all Hackeroo errors
3
+ class Error < StandardError
4
+ attr_reader :rate_limit, :wrapped_exception
5
+
6
+ # @return [Hash]
7
+ def self.errors
8
+ @errors ||= Hash[descendants.map{|klass| [klass.const_get(:HTTP_STATUS_CODE), klass]}]
9
+ end
10
+
11
+ # @return [Array]
12
+ def self.descendants
13
+ ObjectSpace.each_object(::Class).select{|klass| klass < self}
14
+ end
15
+
16
+ # Initializes a new Error object
17
+ #
18
+ # @param exception [Exception, String]
19
+ # @param response_headers [Hash]
20
+ # @return [Hackeroo::Error]
21
+ def initialize(exception=$!, response_headers={})
22
+ @rate_limit = Hackeroo::RateLimit.new(response_headers)
23
+ @wrapped_exception = exception
24
+ exception.respond_to?(:backtrace) ? super(exception.message) : super(exception.to_s)
25
+ end
26
+
27
+ def backtrace
28
+ @wrapped_exception.respond_to?(:backtrace) ? @wrapped_exception.backtrace : super
29
+ end
30
+
31
+ end
32
+ end
@@ -0,0 +1,11 @@
1
+ require 'hackeroo/error/server_error'
2
+
3
+ module Hackeroo
4
+ class Error
5
+ # Raised when Hackeroo returns the HTTP status code 502
6
+ class BadGateway < Hackeroo::Error::ServerError
7
+ HTTP_STATUS_CODE = 502
8
+ MESSAGE = "Hackeroo is down or being upgraded."
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,10 @@
1
+ require 'hackeroo/error/client_error'
2
+
3
+ module Hackeroo
4
+ class Error
5
+ # Raised when Hackeroo returns the HTTP status code 400
6
+ class BadRequest < Hackeroo::Error::ClientError
7
+ HTTP_STATUS_CODE = 400
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,35 @@
1
+ require 'hackeroo/error'
2
+
3
+ module Hackeroo
4
+ class Error
5
+ # Raised when Hackeroo returns a 4xx HTTP status code or there's an error in Faraday
6
+ class ClientError < Hackeroo::Error
7
+
8
+ # Create a new error from an HTTP environment
9
+ #
10
+ # @param response [Hash]
11
+ # @return [Hackeroo::Error]
12
+ def self.from_response(response={})
13
+ new(parse_error(response[:body]), response[:response_headers])
14
+ end
15
+
16
+ private
17
+
18
+ def self.parse_error(body)
19
+ if body.nil?
20
+ ''
21
+ elsif body[:error]
22
+ body[:error]
23
+ elsif body[:errors]
24
+ first = Array(body[:errors]).first
25
+ if first.is_a?(Hash)
26
+ first[:message].chomp
27
+ else
28
+ first.chomp
29
+ end
30
+ end
31
+ end
32
+
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,8 @@
1
+ require 'hackeroo/error'
2
+
3
+ module Hackeroo
4
+ class Error
5
+ class ConfigurationError < ::ArgumentError
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,9 @@
1
+ require 'hackeroo/error'
2
+
3
+ module Hackeroo
4
+ class Error
5
+ # Raised when JSON parsing fails
6
+ class DecodeError < Hackeroo::Error
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,10 @@
1
+ require 'hackeroo/error/client_error'
2
+
3
+ module Hackeroo
4
+ class Error
5
+ # Raised when Hackeroo returns the HTTP status code 403
6
+ class Forbidden < Hackeroo::Error::ClientError
7
+ HTTP_STATUS_CODE = 403
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,11 @@
1
+ require 'hackeroo/error/server_error'
2
+
3
+ module Hackeroo
4
+ class Error
5
+ # Raised when Hackeroo returns the HTTP status code 504
6
+ class GatewayTimeout < Hackeroo::Error::ServerError
7
+ HTTP_STATUS_CODE = 504
8
+ MESSAGE = "The Hackeroo servers are up, but the request couldn't be serviced due to some failure within our stack. Try again later."
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,9 @@
1
+ require 'hackeroo/error'
2
+
3
+ module Hackeroo
4
+ class Error
5
+ # Inherit from KeyError when Ruby 1.8 compatibility is removed
6
+ class IdentityMapKeyError < ::IndexError
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,11 @@
1
+ require 'hackeroo/error/server_error'
2
+
3
+ module Hackeroo
4
+ class Error
5
+ # Raised when Hackeroo returns the HTTP status code 500
6
+ class InternalServerError < Hackeroo::Error::ServerError
7
+ HTTP_STATUS_CODE = 500
8
+ MESSAGE = "Something is technically wrong."
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,10 @@
1
+ require 'hackeroo/error/client_error'
2
+
3
+ module Hackeroo
4
+ class Error
5
+ # Raised when Hackeroo returns the HTTP status code 406
6
+ class NotAcceptable < Hackeroo::Error::ClientError
7
+ HTTP_STATUS_CODE = 406
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,10 @@
1
+ require 'hackeroo/error/client_error'
2
+
3
+ module Hackeroo
4
+ class Error
5
+ # Raised when Hackeroo returns the HTTP status code 404
6
+ class NotFound < Hackeroo::Error::ClientError
7
+ HTTP_STATUS_CODE = 404
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,28 @@
1
+ require 'hackeroo/error'
2
+
3
+ module Hackeroo
4
+ class Error
5
+ # Raised when Hackeroo returns a 5xx HTTP status code
6
+ class ServerError < Hackeroo::Error
7
+ MESSAGE = "Server Error"
8
+
9
+ # Create a new error from an HTTP environment
10
+ #
11
+ # @param response [Hash]
12
+ # @return [Hackeroo::Error]
13
+ def self.from_response(response={})
14
+ new(nil, response[:response_headers])
15
+ end
16
+
17
+ # Initializes a new ServerError object
18
+ #
19
+ # @param message [String]
20
+ # @param response_headers [Hash]
21
+ # @return [Hackeroo::Error::ServerError]
22
+ def initialize(message=nil, response_headers={})
23
+ super((message || self.class.const_get(:MESSAGE)), response_headers)
24
+ end
25
+
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,11 @@
1
+ require 'hackeroo/error/server_error'
2
+
3
+ module Hackeroo
4
+ class Error
5
+ # Raised when Hackeroo returns the HTTP status code 503
6
+ class ServiceUnavailable < Hackeroo::Error::ServerError
7
+ HTTP_STATUS_CODE = 503
8
+ MESSAGE = "(__-){ Hackeroo is over capacity."
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,12 @@
1
+ require 'hackeroo/error/client_error'
2
+
3
+ module Hackeroo
4
+ class Error
5
+ # Raised when Hackeroo returns the HTTP status code 429
6
+ class TooManyRequests < Hackeroo::Error::ClientError
7
+ HTTP_STATUS_CODE = 429
8
+ end
9
+ EnhanceYourCalm = TooManyRequests
10
+ RateLimited = TooManyRequests
11
+ end
12
+ end
@@ -0,0 +1,10 @@
1
+ require 'hackeroo/error/client_error'
2
+
3
+ module Hackeroo
4
+ class Error
5
+ # Raised when Hackeroo returns the HTTP status code 401
6
+ class Unauthorized < Hackeroo::Error::ClientError
7
+ HTTP_STATUS_CODE = 401
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,10 @@
1
+ require 'hackeroo/error/client_error'
2
+
3
+ module Hackeroo
4
+ class Error
5
+ # Raised when Hackeroo returns the HTTP status code 404
6
+ class UnprocessableEntity < Hackeroo::Error::ClientError
7
+ HTTP_STATUS_CODE = 422
8
+ end
9
+ end
10
+ end