peerindex 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gemtest +0 -0
- data/.gitignore +11 -0
- data/.rspec +3 -0
- data/.travis.yml +8 -0
- data/.yardopts +9 -0
- data/Gemfile +7 -0
- data/HISTORY.md +6 -0
- data/LICENSE.md +21 -0
- data/README.md +40 -0
- data/Rakefile +25 -0
- data/lib/faraday/request/gateway.rb +18 -0
- data/lib/faraday/request/multipart_with_file.rb +36 -0
- data/lib/faraday/request/phoenix.rb +18 -0
- data/lib/faraday/response/raise_http_4xx.rb +45 -0
- data/lib/faraday/response/raise_http_5xx.rb +24 -0
- data/lib/peerindex.rb +26 -0
- data/lib/peerindex/api.rb +22 -0
- data/lib/peerindex/client.rb +16 -0
- data/lib/peerindex/client/user.rb +61 -0
- data/lib/peerindex/configuration.rb +80 -0
- data/lib/peerindex/connection.rb +44 -0
- data/lib/peerindex/error.rb +61 -0
- data/lib/peerindex/request.rb +43 -0
- data/lib/peerindex/version.rb +4 -0
- data/peerindex.gemspec +30 -0
- metadata +213 -0
data/.gemtest
ADDED
File without changes
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/.yardopts
ADDED
data/Gemfile
ADDED
data/HISTORY.md
ADDED
data/LICENSE.md
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
Copyright (c) 2011 Reza Jelveh
|
2
|
+
Copyright (c) 2010 John Nunemaker, Wynn Netherland, Erik Michaels-Ober, Steve Richert
|
3
|
+
|
4
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
5
|
+
a copy of this software and associated documentation files (the
|
6
|
+
"Software"), to deal in the Software without restriction, including
|
7
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
8
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
9
|
+
permit persons to whom the Software is furnished to do so, subject to
|
10
|
+
the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be
|
13
|
+
included in all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
16
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
17
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
18
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
19
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
20
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
21
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,40 @@
|
|
1
|
+
# The Peerindex Ruby Gem
|
2
|
+
A Ruby wrapper for the Peerindex API
|
3
|
+
|
4
|
+
## <a name="installation">Installation</a>
|
5
|
+
gem install peerindex
|
6
|
+
|
7
|
+
## Howto
|
8
|
+
This version no longer requires that you explicitly pass the authenticated
|
9
|
+
user's ID or screen name.
|
10
|
+
|
11
|
+
Peerindex.configure do |config|
|
12
|
+
config.api_key = YOUR_API_KEY
|
13
|
+
end
|
14
|
+
|
15
|
+
Peerindex.user("rjzzleep")
|
16
|
+
|
17
|
+
## <a href="performance">Performance</a>
|
18
|
+
You can improve performance by preloading a faster JSON or XML parsing library.
|
19
|
+
By default, the JSON will be parsed with [okjson][okjson] and XML will be
|
20
|
+
parsed with [REXML][rexml]. For faster JSON parsing, we recommend
|
21
|
+
[yajl-ruby][yajl] and for faster XML parsing, we recommend
|
22
|
+
[libxml-ruby][libxml] or [nokogiri][nokogiri].
|
23
|
+
|
24
|
+
[okjson]: https://github.com/ddollar/okjson
|
25
|
+
[rexml]: http://www.germane-software.com/software/rexml
|
26
|
+
[yajl]: https://github.com/brianmario/yajl-ruby
|
27
|
+
[libxml]: https://github.com/dvdplm/libxml-ruby
|
28
|
+
[nokogiri]: http://nokogiri.org
|
29
|
+
|
30
|
+
## <a name="examples">Usage Examples</a>
|
31
|
+
require "rubygems"
|
32
|
+
require "peerindex"
|
33
|
+
|
34
|
+
# Get a user's peerindex
|
35
|
+
puts Peerindex.user("rjzzleep").peerindex
|
36
|
+
|
37
|
+
## <a name="copyright">Copyright</a>
|
38
|
+
Copyright (c) 2011 Reza Jelveh
|
39
|
+
This is heavily based on John Nunemaker, Wynn Netherland, Erik Michaels-Ober, Steve Richert twitter gem so they probably own the copyright as well
|
40
|
+
See [LICENSE](https://github.com/fishman/peerindex/blob/master/LICENSE.md) for details.
|
data/Rakefile
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
#!/usr/bin/env rake
|
2
|
+
|
3
|
+
require 'bundler'
|
4
|
+
Bundler::GemHelper.install_tasks
|
5
|
+
|
6
|
+
require 'rspec/core/rake_task'
|
7
|
+
RSpec::Core::RakeTask.new(:spec)
|
8
|
+
|
9
|
+
task :test => :spec
|
10
|
+
task :default => :spec
|
11
|
+
|
12
|
+
namespace :doc do
|
13
|
+
require 'yard'
|
14
|
+
YARD::Rake::YardocTask.new do |task|
|
15
|
+
task.files = ['HISTORY.md', 'LICENSE.md', 'lib/**/*.rb']
|
16
|
+
task.options = [
|
17
|
+
'--protected',
|
18
|
+
'--output-dir', 'doc/yard',
|
19
|
+
'--tag', 'format:Supported formats',
|
20
|
+
'--tag', 'authenticated:Requires Authentication',
|
21
|
+
'--tag', 'rate_limited:Rate Limited',
|
22
|
+
'--markup', 'markdown',
|
23
|
+
]
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
require 'faraday'
|
2
|
+
|
3
|
+
# @private
|
4
|
+
module Faraday
|
5
|
+
# @private
|
6
|
+
class Request::Gateway < Faraday::Middleware
|
7
|
+
def call(env)
|
8
|
+
url = env[:url].dup
|
9
|
+
url.host = @gateway
|
10
|
+
env[:url] = url
|
11
|
+
@app.call(env)
|
12
|
+
end
|
13
|
+
|
14
|
+
def initialize(app, gateway)
|
15
|
+
@app, @gateway = app, gateway
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
require 'faraday'
|
2
|
+
|
3
|
+
# @private
|
4
|
+
module Faraday
|
5
|
+
# @private
|
6
|
+
class Request::MultipartWithFile < Faraday::Middleware
|
7
|
+
def call(env)
|
8
|
+
if env[:body].is_a?(Hash)
|
9
|
+
env[:body].each do |key, value|
|
10
|
+
if value.is_a?(File)
|
11
|
+
env[:body][key] = Faraday::UploadIO.new(value, mime_type(value.path), value.path)
|
12
|
+
elsif value.is_a?(Hash) && (value['io'].is_a?(IO) || value['io'].is_a?(StringIO))
|
13
|
+
env[:body][key] = Faraday::UploadIO.new(value['io'], mime_type('.'+value['type']), '')
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
@app.call(env)
|
19
|
+
end
|
20
|
+
|
21
|
+
private
|
22
|
+
|
23
|
+
def mime_type(path)
|
24
|
+
case path
|
25
|
+
when /\.jpe?g/i
|
26
|
+
'image/jpeg'
|
27
|
+
when /\.gif$/i
|
28
|
+
'image/gif'
|
29
|
+
when /\.png$/i
|
30
|
+
'image/png'
|
31
|
+
else
|
32
|
+
'application/octet-stream'
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
require 'faraday'
|
2
|
+
|
3
|
+
module Faraday
|
4
|
+
class Request::Phoenix < Faraday::Middleware
|
5
|
+
def call(env)
|
6
|
+
# Not sure what what the X-Phx (Phoenix?) header is for but it's
|
7
|
+
# required to access certain undocumented resources
|
8
|
+
# e.g. GET urls/resolve
|
9
|
+
env[:request_headers]['X-Phx'] = 'true'
|
10
|
+
|
11
|
+
@app.call(env)
|
12
|
+
end
|
13
|
+
|
14
|
+
def initialize(app)
|
15
|
+
@app = app
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,45 @@
|
|
1
|
+
require 'faraday'
|
2
|
+
|
3
|
+
# @private
|
4
|
+
module Faraday
|
5
|
+
# @private
|
6
|
+
class Response::RaiseHttp4xx < Response::Middleware
|
7
|
+
def on_complete(env)
|
8
|
+
case env[:status].to_i
|
9
|
+
when 400
|
10
|
+
raise Peerindex::BadRequest.new(error_message(env), env[:response_headers])
|
11
|
+
when 401
|
12
|
+
raise Peerindex::Unauthorized.new(error_message(env), env[:response_headers])
|
13
|
+
when 403
|
14
|
+
raise Peerindex::Forbidden.new(error_message(env), env[:response_headers])
|
15
|
+
when 404
|
16
|
+
raise Peerindex::NotFound.new(error_message(env), env[:response_headers])
|
17
|
+
when 406
|
18
|
+
raise Peerindex::NotAcceptable.new(error_message(env), env[:response_headers])
|
19
|
+
when 420
|
20
|
+
raise Peerindex::EnhanceYourCalm.new(error_message(env), env[:response_headers])
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
private
|
25
|
+
|
26
|
+
def error_message(env)
|
27
|
+
"#{env[:method].to_s.upcase} #{env[:url].to_s}: #{env[:status]}#{error_body(env[:body])}"
|
28
|
+
end
|
29
|
+
|
30
|
+
def error_body(body)
|
31
|
+
if body.nil?
|
32
|
+
nil
|
33
|
+
elsif body['error']
|
34
|
+
": #{body['error']}"
|
35
|
+
elsif body['errors']
|
36
|
+
first = Array(body['errors']).first
|
37
|
+
if first.kind_of? Hash
|
38
|
+
": #{first['message'].chomp}"
|
39
|
+
else
|
40
|
+
": #{first.chomp}"
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
require 'faraday'
|
2
|
+
|
3
|
+
# @private
|
4
|
+
module Faraday
|
5
|
+
# @private
|
6
|
+
class Response::RaiseHttp5xx < Response::Middleware
|
7
|
+
def on_complete(env)
|
8
|
+
case env[:status].to_i
|
9
|
+
when 500
|
10
|
+
raise Peerindex::InternalServerError.new(error_message(env, "Something is technically wrong."), env[:response_headers])
|
11
|
+
when 502
|
12
|
+
raise Peerindex::BadGateway.new(error_message(env, "Peerindex is down or being upgraded."), env[:response_headers])
|
13
|
+
when 503
|
14
|
+
raise Peerindex::ServiceUnavailable.new(error_message(env, "(__-){ Peerindex is over capacity."), env[:response_headers])
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
private
|
19
|
+
|
20
|
+
def error_message(env, body=nil)
|
21
|
+
"#{env[:method].to_s.upcase} #{env[:url].to_s}: #{[env[:status].to_s + ':', body].compact.join(' ')} Check http://status.twitter.com/ for updates on the status of the Peerindex service."
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
data/lib/peerindex.rb
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
require 'peerindex/api'
|
2
|
+
require 'peerindex/client'
|
3
|
+
require 'peerindex/configuration'
|
4
|
+
require 'peerindex/error'
|
5
|
+
|
6
|
+
module Peerindex
|
7
|
+
extend Configuration
|
8
|
+
class << self
|
9
|
+
# Alias for Peerindex::Client.new
|
10
|
+
#
|
11
|
+
# @return [Peerindex::Client]
|
12
|
+
def new(options={})
|
13
|
+
Peerindex::Client.new(options)
|
14
|
+
end
|
15
|
+
|
16
|
+
# Delegate to Twitter::Client
|
17
|
+
def method_missing(method, *args, &block)
|
18
|
+
return super unless new.respond_to?(method)
|
19
|
+
new.send(method, *args, &block)
|
20
|
+
end
|
21
|
+
|
22
|
+
def respond_to?(method, include_private = false)
|
23
|
+
new.respond_to?(method, include_private) || super(method, include_private)
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
require 'peerindex/configuration'
|
2
|
+
require 'peerindex/connection'
|
3
|
+
require 'peerindex/request'
|
4
|
+
|
5
|
+
module Peerindex
|
6
|
+
# @private
|
7
|
+
class API
|
8
|
+
include Connection
|
9
|
+
include Request
|
10
|
+
|
11
|
+
# @private
|
12
|
+
attr_accessor *Configuration::VALID_OPTIONS_KEYS
|
13
|
+
|
14
|
+
# Creates a new API
|
15
|
+
def initialize(options={})
|
16
|
+
options = Peerindex.options.merge(options)
|
17
|
+
Configuration::VALID_OPTIONS_KEYS.each do |key|
|
18
|
+
send("#{key}=", options[key])
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
module Peerindex
|
2
|
+
# Wrapper for the Peerindex REST API
|
3
|
+
#
|
4
|
+
# @note All methods have been separated into modules and follow the same grouping used in {http://dev.twitter.com/doc the Twitter API Documentation}.
|
5
|
+
# @see http://dev.twitter.com/pages/every_developer
|
6
|
+
class Client < API
|
7
|
+
# Require client method modules after initializing the Client class in
|
8
|
+
# order to avoid a superclass mismatch error, allowing those modules to be
|
9
|
+
# Client-namespaced.
|
10
|
+
require 'peerindex/client/user'
|
11
|
+
|
12
|
+
alias :api_endpoint :endpoint
|
13
|
+
|
14
|
+
include Peerindex::Client::User
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,61 @@
|
|
1
|
+
module Peerindex
|
2
|
+
class Client
|
3
|
+
# Defines methods related to users
|
4
|
+
module User
|
5
|
+
# Returns extended information of a given user
|
6
|
+
#
|
7
|
+
# @see https://dev.twitter.com/docs/api/1/get/users/show
|
8
|
+
# @rate_limited Yes
|
9
|
+
# @requires_authentication No
|
10
|
+
# @response_format `json`
|
11
|
+
# @response_format `xml`
|
12
|
+
# @overload user(user, options={})
|
13
|
+
# @param user [Integer, String] A Twitter user ID or screen name.
|
14
|
+
# @param options [Hash] A customizable set of options.
|
15
|
+
# @option options [Boolean, String, Integer] :include_entities Include {https://dev.twitter.com/docs/tweet-entities Tweet Entities} when set to true, 't' or 1.
|
16
|
+
# @return [Hashie::Mash] The requested user.
|
17
|
+
# @example Return extended information for @sferik
|
18
|
+
# Peerindex.user("sferik")
|
19
|
+
# Peerindex.user(7505382) # Same as above
|
20
|
+
def user(user, options={})
|
21
|
+
merge_user_into_options!(user, options)
|
22
|
+
response = get('version/profile/show', options)
|
23
|
+
format.to_s.downcase == 'xml' ? response['user'] : response
|
24
|
+
end
|
25
|
+
|
26
|
+
# Returns true if the specified user exists
|
27
|
+
#
|
28
|
+
# @param user [Integer, String] A Twitter user ID or screen name.
|
29
|
+
# @return [Boolean] true if the user exists, otherwise false.
|
30
|
+
# @example Return true if @sferik exists
|
31
|
+
# Peerindex.user?("sferik")
|
32
|
+
# Peerindex.user?(7505382) # Same as above
|
33
|
+
# @requires_authentication No
|
34
|
+
# @rate_limited Yes
|
35
|
+
def user?(user, options={})
|
36
|
+
merge_user_into_options!(user, options)
|
37
|
+
get('version/profile/show', options, :format => :json, :raw => true)
|
38
|
+
true
|
39
|
+
rescue Peerindex::NotFound
|
40
|
+
false
|
41
|
+
end
|
42
|
+
|
43
|
+
# Take a single user ID or screen name and merge it into an options hash with the correct key
|
44
|
+
#
|
45
|
+
# @param user_id_or_screen_name [Integer, String] A Twitter user ID or screen_name.
|
46
|
+
# @param options [Hash] A customizable set of options.
|
47
|
+
# @return [Hash]
|
48
|
+
def merge_user_into_options!(user_id_or_screen_name, options={})
|
49
|
+
case user_id_or_screen_name
|
50
|
+
when Fixnum
|
51
|
+
options[:id] = user_id_or_screen_name
|
52
|
+
when String
|
53
|
+
options[:id] = user_id_or_screen_name
|
54
|
+
end
|
55
|
+
options[:api_key] = self.api_key
|
56
|
+
options
|
57
|
+
end
|
58
|
+
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
@@ -0,0 +1,80 @@
|
|
1
|
+
require 'peerindex/version'
|
2
|
+
|
3
|
+
module Peerindex
|
4
|
+
# Defines constants and methods related to configuration
|
5
|
+
module Configuration
|
6
|
+
# An array of valid keys in the options hash when configuring a {Twitter::API}
|
7
|
+
VALID_OPTIONS_KEYS = [
|
8
|
+
:adapter,
|
9
|
+
:api_key,
|
10
|
+
:endpoint,
|
11
|
+
:format,
|
12
|
+
:gateway,
|
13
|
+
:proxy,
|
14
|
+
:user_agent,
|
15
|
+
:faraday_options].freeze
|
16
|
+
|
17
|
+
# The adapter that will be used to connect if none is set
|
18
|
+
DEFAULT_ADAPTER = :net_http
|
19
|
+
|
20
|
+
# This is required
|
21
|
+
DEFAULT_API_KEY = nil
|
22
|
+
|
23
|
+
# By default, don't set an application secret
|
24
|
+
DEFAULT_CONSUMER_SECRET = nil
|
25
|
+
|
26
|
+
# The endpoint that will be used to connect if none is set
|
27
|
+
#
|
28
|
+
# @note Default endpoint
|
29
|
+
# @see http://dev.peerindex.com/docs
|
30
|
+
DEFAULT_ENDPOINT = 'http://api.peerindex.net/'.freeze
|
31
|
+
|
32
|
+
# The response format appended to the path and sent in the 'Accept' header if none is set
|
33
|
+
#
|
34
|
+
# @note JSON is preferred over XML because it is more concise and faster to parse.
|
35
|
+
DEFAULT_FORMAT = :json
|
36
|
+
|
37
|
+
# By default, don't use a proxy server
|
38
|
+
DEFAULT_PROXY = nil
|
39
|
+
|
40
|
+
# The value sent in the 'User-Agent' header if none is set
|
41
|
+
DEFAULT_USER_AGENT = "Peerindex Ruby Gem #{Peerindex::VERSION}".freeze
|
42
|
+
|
43
|
+
DEFAULT_GATEWAY = nil
|
44
|
+
|
45
|
+
DEFAULT_FARADAY_OPTIONS = {}.freeze
|
46
|
+
|
47
|
+
# @private
|
48
|
+
attr_accessor *VALID_OPTIONS_KEYS
|
49
|
+
|
50
|
+
# When this module is extended, set all configuration options to their default values
|
51
|
+
def self.extended(base)
|
52
|
+
base.reset
|
53
|
+
end
|
54
|
+
|
55
|
+
# Convenience method to allow configuration options to be set in a block
|
56
|
+
def configure
|
57
|
+
yield self
|
58
|
+
end
|
59
|
+
|
60
|
+
# Create a hash of options and their values
|
61
|
+
def options
|
62
|
+
options = {}
|
63
|
+
VALID_OPTIONS_KEYS.each{|k| options[k] = send(k)}
|
64
|
+
options
|
65
|
+
end
|
66
|
+
|
67
|
+
# Reset all configuration options to defaults
|
68
|
+
def reset
|
69
|
+
self.adapter = DEFAULT_ADAPTER
|
70
|
+
self.api_key = DEFAULT_API_KEY
|
71
|
+
self.endpoint = DEFAULT_ENDPOINT
|
72
|
+
self.format = DEFAULT_FORMAT
|
73
|
+
self.proxy = DEFAULT_PROXY
|
74
|
+
self.user_agent = DEFAULT_USER_AGENT
|
75
|
+
self.gateway = DEFAULT_GATEWAY
|
76
|
+
self.faraday_options = DEFAULT_FARADAY_OPTIONS
|
77
|
+
self
|
78
|
+
end
|
79
|
+
end
|
80
|
+
end
|
@@ -0,0 +1,44 @@
|
|
1
|
+
require 'faraday_middleware'
|
2
|
+
require 'faraday/request/phoenix'
|
3
|
+
require 'faraday/request/multipart_with_file'
|
4
|
+
require 'faraday/response/raise_http_4xx'
|
5
|
+
require 'faraday/response/raise_http_5xx'
|
6
|
+
|
7
|
+
module Peerindex
|
8
|
+
# @private
|
9
|
+
module Connection
|
10
|
+
private
|
11
|
+
|
12
|
+
def connection(options={})
|
13
|
+
merged_options = faraday_options.merge({
|
14
|
+
:headers => {
|
15
|
+
'Accept' => "application/#{format}",
|
16
|
+
'User-Agent' => user_agent
|
17
|
+
},
|
18
|
+
:proxy => proxy,
|
19
|
+
:ssl => {:verify => false},
|
20
|
+
:url => options.fetch(:endpoint, api_endpoint)
|
21
|
+
})
|
22
|
+
|
23
|
+
Faraday.new(merged_options) do |builder|
|
24
|
+
builder.use Faraday::Request::Phoenix if options[:phoenix]
|
25
|
+
builder.use Faraday::Request::MultipartWithFile
|
26
|
+
builder.use Faraday::Request::Multipart
|
27
|
+
builder.use Faraday::Request::UrlEncoded
|
28
|
+
builder.use Faraday::Response::RaiseHttp4xx
|
29
|
+
unless options[:raw]
|
30
|
+
case options.fetch(:format, format).to_s.downcase
|
31
|
+
when 'json', 'phoenix'
|
32
|
+
builder.use Faraday::Response::Mashify
|
33
|
+
builder.use Faraday::Response::ParseJson
|
34
|
+
when 'xml'
|
35
|
+
builder.use Faraday::Response::Mashify
|
36
|
+
builder.use Faraday::Response::ParseXml
|
37
|
+
end
|
38
|
+
end
|
39
|
+
builder.use Faraday::Response::RaiseHttp5xx
|
40
|
+
builder.adapter(adapter)
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
@@ -0,0 +1,61 @@
|
|
1
|
+
module Peerindex
|
2
|
+
# Custom error class for rescuing from all Twitter errors
|
3
|
+
class Error < StandardError
|
4
|
+
attr_reader :http_headers
|
5
|
+
|
6
|
+
def initialize(message, http_headers)
|
7
|
+
@http_headers = Hash[http_headers]
|
8
|
+
super message
|
9
|
+
end
|
10
|
+
|
11
|
+
def ratelimit_reset
|
12
|
+
Time.at(@http_headers.values_at('x-ratelimit-reset', 'X-RateLimit-Reset').detect{|value| value}.to_i)
|
13
|
+
end
|
14
|
+
|
15
|
+
def ratelimit_limit
|
16
|
+
@http_headers.values_at('x-ratelimit-limit', 'X-RateLimit-Limit').detect{|value| value}.to_i
|
17
|
+
end
|
18
|
+
|
19
|
+
def ratelimit_remaining
|
20
|
+
@http_headers.values_at('x-ratelimit-remaining', 'X-RateLimit-Remaining').detect{|value| value}.to_i
|
21
|
+
end
|
22
|
+
|
23
|
+
def retry_after
|
24
|
+
[(ratelimit_reset - Time.now).ceil, 0].max
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
# Raised when Twitter returns the HTTP status code 400
|
29
|
+
class BadRequest < Error; end
|
30
|
+
|
31
|
+
# Raised when Twitter returns the HTTP status code 401
|
32
|
+
class Unauthorized < Error; end
|
33
|
+
|
34
|
+
# Raised when Twitter returns the HTTP status code 403
|
35
|
+
class Forbidden < Error; end
|
36
|
+
|
37
|
+
# Raised when Twitter returns the HTTP status code 404
|
38
|
+
class NotFound < Error; end
|
39
|
+
|
40
|
+
# Raised when Twitter returns the HTTP status code 406
|
41
|
+
class NotAcceptable < Error; end
|
42
|
+
|
43
|
+
# Raised when Twitter returns the HTTP status code 420
|
44
|
+
class EnhanceYourCalm < Error
|
45
|
+
# The number of seconds your application should wait before requesting date from the Search API again
|
46
|
+
#
|
47
|
+
# @see http://dev.twitter.com/pages/rate-limiting
|
48
|
+
def retry_after
|
49
|
+
@http_headers.values_at('retry-after', 'Retry-After').detect {|value| value }.to_i
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
# Raised when Twitter returns the HTTP status code 500
|
54
|
+
class InternalServerError < Error; end
|
55
|
+
|
56
|
+
# Raised when Twitter returns the HTTP status code 502
|
57
|
+
class BadGateway < Error; end
|
58
|
+
|
59
|
+
# Raised when Twitter returns the HTTP status code 503
|
60
|
+
class ServiceUnavailable < Error; end
|
61
|
+
end
|
@@ -0,0 +1,43 @@
|
|
1
|
+
module Peerindex
|
2
|
+
# Defines HTTP request methods
|
3
|
+
module Request
|
4
|
+
# Perform an HTTP GET request
|
5
|
+
def get(path, params={}, options={})
|
6
|
+
request(:get, path, params, options)
|
7
|
+
end
|
8
|
+
|
9
|
+
def post(path, params={}, options={})
|
10
|
+
request(:post, path, params, options)
|
11
|
+
end
|
12
|
+
|
13
|
+
# Perform an HTTP PUT request
|
14
|
+
def put(path, params={}, options={})
|
15
|
+
request(:put, path, params, options)
|
16
|
+
end
|
17
|
+
|
18
|
+
# Perform an HTTP DELETE request
|
19
|
+
def delete(path, params={}, options={})
|
20
|
+
request(:delete, path, params, options)
|
21
|
+
end
|
22
|
+
|
23
|
+
private
|
24
|
+
|
25
|
+
# Perform an HTTP request
|
26
|
+
def request(method, path, params, options)
|
27
|
+
response = connection(options).send(method) do |request|
|
28
|
+
case method.to_sym
|
29
|
+
when :get, :delete
|
30
|
+
request.url(formatted_path(path, options), params)
|
31
|
+
when :post, :put
|
32
|
+
request.path = formatted_path(path, options)
|
33
|
+
request.body = params unless params.empty?
|
34
|
+
end
|
35
|
+
end
|
36
|
+
options[:raw] ? response : response.body
|
37
|
+
end
|
38
|
+
|
39
|
+
def formatted_path(path, options={})
|
40
|
+
[path, options.fetch(:format, format)].compact.join('.')
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
data/peerindex.gemspec
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
require File.expand_path('../lib/peerindex/version', __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |gem|
|
5
|
+
gem.add_dependency 'hashie', '~> 1.1.0'
|
6
|
+
gem.add_dependency 'faraday', '~> 0.7.4'
|
7
|
+
gem.add_dependency 'faraday_middleware', '~> 0.7.0'
|
8
|
+
gem.add_dependency 'multi_json', '~> 1.0.0'
|
9
|
+
gem.add_dependency 'multi_xml', '~> 0.4.0'
|
10
|
+
gem.add_dependency 'simple_oauth', '~> 0.1.5'
|
11
|
+
gem.add_development_dependency 'nokogiri', '~> 1.4'
|
12
|
+
gem.add_development_dependency 'rake', '~> 0.9'
|
13
|
+
gem.add_development_dependency 'rdiscount', '~> 1.6'
|
14
|
+
gem.add_development_dependency 'rspec', '~> 2.6'
|
15
|
+
gem.add_development_dependency 'simplecov', '~> 0.4'
|
16
|
+
gem.add_development_dependency 'webmock', '~> 1.7'
|
17
|
+
gem.add_development_dependency 'yard', '~> 0.7'
|
18
|
+
gem.authors = ["Reza Jelveh"]
|
19
|
+
gem.description = %q{A Ruby wrapper for the Peerindex API}
|
20
|
+
gem.email = ['reza.jelveh@tuhh.de']
|
21
|
+
gem.executables = `git ls-files -- bin/*`.split("\n").map{|f| File.basename(f)}
|
22
|
+
gem.files = `git ls-files`.split("\n")
|
23
|
+
gem.homepage = 'https://github.com/fishman/peerindex'
|
24
|
+
gem.name = 'peerindex'
|
25
|
+
gem.require_paths = ['lib']
|
26
|
+
gem.required_rubygems_version = Gem::Requirement.new('>= 1.3.6')
|
27
|
+
gem.summary = %q{Ruby wrapper for the Peerindex API}
|
28
|
+
gem.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
29
|
+
gem.version = Peerindex::VERSION.dup
|
30
|
+
end
|
metadata
ADDED
@@ -0,0 +1,213 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: peerindex
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Reza Jelveh
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2011-10-13 00:00:00.000000000Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: hashie
|
16
|
+
requirement: &20554380 !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ~>
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: 1.1.0
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *20554380
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: faraday
|
27
|
+
requirement: &20553880 !ruby/object:Gem::Requirement
|
28
|
+
none: false
|
29
|
+
requirements:
|
30
|
+
- - ~>
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: 0.7.4
|
33
|
+
type: :runtime
|
34
|
+
prerelease: false
|
35
|
+
version_requirements: *20553880
|
36
|
+
- !ruby/object:Gem::Dependency
|
37
|
+
name: faraday_middleware
|
38
|
+
requirement: &20604120 !ruby/object:Gem::Requirement
|
39
|
+
none: false
|
40
|
+
requirements:
|
41
|
+
- - ~>
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: 0.7.0
|
44
|
+
type: :runtime
|
45
|
+
prerelease: false
|
46
|
+
version_requirements: *20604120
|
47
|
+
- !ruby/object:Gem::Dependency
|
48
|
+
name: multi_json
|
49
|
+
requirement: &20603660 !ruby/object:Gem::Requirement
|
50
|
+
none: false
|
51
|
+
requirements:
|
52
|
+
- - ~>
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: 1.0.0
|
55
|
+
type: :runtime
|
56
|
+
prerelease: false
|
57
|
+
version_requirements: *20603660
|
58
|
+
- !ruby/object:Gem::Dependency
|
59
|
+
name: multi_xml
|
60
|
+
requirement: &20603200 !ruby/object:Gem::Requirement
|
61
|
+
none: false
|
62
|
+
requirements:
|
63
|
+
- - ~>
|
64
|
+
- !ruby/object:Gem::Version
|
65
|
+
version: 0.4.0
|
66
|
+
type: :runtime
|
67
|
+
prerelease: false
|
68
|
+
version_requirements: *20603200
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: simple_oauth
|
71
|
+
requirement: &20602740 !ruby/object:Gem::Requirement
|
72
|
+
none: false
|
73
|
+
requirements:
|
74
|
+
- - ~>
|
75
|
+
- !ruby/object:Gem::Version
|
76
|
+
version: 0.1.5
|
77
|
+
type: :runtime
|
78
|
+
prerelease: false
|
79
|
+
version_requirements: *20602740
|
80
|
+
- !ruby/object:Gem::Dependency
|
81
|
+
name: nokogiri
|
82
|
+
requirement: &20602280 !ruby/object:Gem::Requirement
|
83
|
+
none: false
|
84
|
+
requirements:
|
85
|
+
- - ~>
|
86
|
+
- !ruby/object:Gem::Version
|
87
|
+
version: '1.4'
|
88
|
+
type: :development
|
89
|
+
prerelease: false
|
90
|
+
version_requirements: *20602280
|
91
|
+
- !ruby/object:Gem::Dependency
|
92
|
+
name: rake
|
93
|
+
requirement: &20601820 !ruby/object:Gem::Requirement
|
94
|
+
none: false
|
95
|
+
requirements:
|
96
|
+
- - ~>
|
97
|
+
- !ruby/object:Gem::Version
|
98
|
+
version: '0.9'
|
99
|
+
type: :development
|
100
|
+
prerelease: false
|
101
|
+
version_requirements: *20601820
|
102
|
+
- !ruby/object:Gem::Dependency
|
103
|
+
name: rdiscount
|
104
|
+
requirement: &20601360 !ruby/object:Gem::Requirement
|
105
|
+
none: false
|
106
|
+
requirements:
|
107
|
+
- - ~>
|
108
|
+
- !ruby/object:Gem::Version
|
109
|
+
version: '1.6'
|
110
|
+
type: :development
|
111
|
+
prerelease: false
|
112
|
+
version_requirements: *20601360
|
113
|
+
- !ruby/object:Gem::Dependency
|
114
|
+
name: rspec
|
115
|
+
requirement: &20600900 !ruby/object:Gem::Requirement
|
116
|
+
none: false
|
117
|
+
requirements:
|
118
|
+
- - ~>
|
119
|
+
- !ruby/object:Gem::Version
|
120
|
+
version: '2.6'
|
121
|
+
type: :development
|
122
|
+
prerelease: false
|
123
|
+
version_requirements: *20600900
|
124
|
+
- !ruby/object:Gem::Dependency
|
125
|
+
name: simplecov
|
126
|
+
requirement: &20600440 !ruby/object:Gem::Requirement
|
127
|
+
none: false
|
128
|
+
requirements:
|
129
|
+
- - ~>
|
130
|
+
- !ruby/object:Gem::Version
|
131
|
+
version: '0.4'
|
132
|
+
type: :development
|
133
|
+
prerelease: false
|
134
|
+
version_requirements: *20600440
|
135
|
+
- !ruby/object:Gem::Dependency
|
136
|
+
name: webmock
|
137
|
+
requirement: &20599980 !ruby/object:Gem::Requirement
|
138
|
+
none: false
|
139
|
+
requirements:
|
140
|
+
- - ~>
|
141
|
+
- !ruby/object:Gem::Version
|
142
|
+
version: '1.7'
|
143
|
+
type: :development
|
144
|
+
prerelease: false
|
145
|
+
version_requirements: *20599980
|
146
|
+
- !ruby/object:Gem::Dependency
|
147
|
+
name: yard
|
148
|
+
requirement: &20599520 !ruby/object:Gem::Requirement
|
149
|
+
none: false
|
150
|
+
requirements:
|
151
|
+
- - ~>
|
152
|
+
- !ruby/object:Gem::Version
|
153
|
+
version: '0.7'
|
154
|
+
type: :development
|
155
|
+
prerelease: false
|
156
|
+
version_requirements: *20599520
|
157
|
+
description: A Ruby wrapper for the Peerindex API
|
158
|
+
email:
|
159
|
+
- reza.jelveh@tuhh.de
|
160
|
+
executables: []
|
161
|
+
extensions: []
|
162
|
+
extra_rdoc_files: []
|
163
|
+
files:
|
164
|
+
- .gemtest
|
165
|
+
- .gitignore
|
166
|
+
- .rspec
|
167
|
+
- .travis.yml
|
168
|
+
- .yardopts
|
169
|
+
- Gemfile
|
170
|
+
- HISTORY.md
|
171
|
+
- LICENSE.md
|
172
|
+
- README.md
|
173
|
+
- Rakefile
|
174
|
+
- lib/faraday/request/gateway.rb
|
175
|
+
- lib/faraday/request/multipart_with_file.rb
|
176
|
+
- lib/faraday/request/phoenix.rb
|
177
|
+
- lib/faraday/response/raise_http_4xx.rb
|
178
|
+
- lib/faraday/response/raise_http_5xx.rb
|
179
|
+
- lib/peerindex.rb
|
180
|
+
- lib/peerindex/api.rb
|
181
|
+
- lib/peerindex/client.rb
|
182
|
+
- lib/peerindex/client/user.rb
|
183
|
+
- lib/peerindex/configuration.rb
|
184
|
+
- lib/peerindex/connection.rb
|
185
|
+
- lib/peerindex/error.rb
|
186
|
+
- lib/peerindex/request.rb
|
187
|
+
- lib/peerindex/version.rb
|
188
|
+
- peerindex.gemspec
|
189
|
+
homepage: https://github.com/fishman/peerindex
|
190
|
+
licenses: []
|
191
|
+
post_install_message:
|
192
|
+
rdoc_options: []
|
193
|
+
require_paths:
|
194
|
+
- lib
|
195
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
196
|
+
none: false
|
197
|
+
requirements:
|
198
|
+
- - ! '>='
|
199
|
+
- !ruby/object:Gem::Version
|
200
|
+
version: '0'
|
201
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
202
|
+
none: false
|
203
|
+
requirements:
|
204
|
+
- - ! '>='
|
205
|
+
- !ruby/object:Gem::Version
|
206
|
+
version: 1.3.6
|
207
|
+
requirements: []
|
208
|
+
rubyforge_project:
|
209
|
+
rubygems_version: 1.8.10
|
210
|
+
signing_key:
|
211
|
+
specification_version: 3
|
212
|
+
summary: Ruby wrapper for the Peerindex API
|
213
|
+
test_files: []
|