reddit-base 0.2.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +18 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +71 -0
- data/Rakefile +1 -0
- data/lib/reddit/base.rb +14 -0
- data/lib/reddit/base/basic_client.rb +34 -0
- data/lib/reddit/base/client.rb +26 -0
- data/lib/reddit/base/helpers.rb +22 -0
- data/lib/reddit/base/version.rb +5 -0
- data/reddit-base.gemspec +28 -0
- metadata +100 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 5cb71e8fcd16075836f4a9582b134c91825bdf96
|
4
|
+
data.tar.gz: 387602c1ada73880d1d8515045c75b55edc366e3
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 1a252a3aa26a572e8a841a432d64f6b236f4c70de7b2707202b54ceb3d5133014388de6218d58e30b5462308ddfdb123bf16b5c35c8f90578f64342b4984a33a
|
7
|
+
data.tar.gz: 32b2579fcff87770557e8cf29ef21420db290a63f0601eaf91d5f15dba45ab2403e5458df4dfba54e006ed52d8a91d464638d55deea7985ff4795b2ac0b0799e
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 Daniel O'Brien
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,71 @@
|
|
1
|
+
Reddit Base
|
2
|
+
===========
|
3
|
+
|
4
|
+
A minimal reddit API client for Ruby.
|
5
|
+
|
6
|
+
Motivation
|
7
|
+
----------
|
8
|
+
|
9
|
+
Managing major versions of an API client can be tricky, especially when the
|
10
|
+
API us unversioned, inconsistent and outside of your control.
|
11
|
+
|
12
|
+
This client library aims to provide minimal support for the reddit API,
|
13
|
+
reducing the need for frequent breaking changes and to act as the backbone
|
14
|
+
for more other higher-level API clients.
|
15
|
+
|
16
|
+
Installation
|
17
|
+
------------
|
18
|
+
|
19
|
+
Via Rubygems:
|
20
|
+
|
21
|
+
gem install reddit-base
|
22
|
+
|
23
|
+
Or in your Gemfile with Bundler:
|
24
|
+
|
25
|
+
gem reddit-base
|
26
|
+
|
27
|
+
Basic Usage
|
28
|
+
-------------
|
29
|
+
|
30
|
+
Retrieving a particular endpoint:
|
31
|
+
|
32
|
+
require 'reddit/base'
|
33
|
+
|
34
|
+
client = Reddit::Base::Client.new(user: USERNAME, password: PASSWORD)
|
35
|
+
client.get('/r/AskReddit')
|
36
|
+
|
37
|
+
The above will return a `Faraday::Response` instance with JSON data in the
|
38
|
+
`body` attribute.
|
39
|
+
|
40
|
+
What it Does
|
41
|
+
------------
|
42
|
+
|
43
|
+
* Authentication.
|
44
|
+
* Rate limiting.
|
45
|
+
* Modhash handling (reddit's CSRF protection).
|
46
|
+
* JSON coersion.
|
47
|
+
* Forwarding..
|
48
|
+
* Multipart POST.
|
49
|
+
* Reddit error wrapping.
|
50
|
+
|
51
|
+
What it Doesn't
|
52
|
+
---------------
|
53
|
+
|
54
|
+
* Parsing of Reddit "Things" and "Kinds."
|
55
|
+
* Parsing of common attributes like dates and times.
|
56
|
+
* HTML entity decoding (beware of "body" and "selftext").
|
57
|
+
|
58
|
+
Recommended Reading
|
59
|
+
-------------------
|
60
|
+
|
61
|
+
* reddit API documentation: http://www.reddit.com/dev/api
|
62
|
+
* reddit API wiki: https://github.com/reddit/reddit/wiki/API
|
63
|
+
* reddit project on Github: https://github.com/reddit/reddit
|
64
|
+
|
65
|
+
Contributors
|
66
|
+
------------
|
67
|
+
|
68
|
+
* Maintainer: [Daniel O'Brien](http://github.com/dobs)
|
69
|
+
|
70
|
+
This project is copyright 2014 by its contributors, refer to LICENSE file for
|
71
|
+
licensing information.
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
data/lib/reddit/base.rb
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
require 'faraday'
|
2
|
+
require 'faraday_middleware'
|
3
|
+
require 'faraday_middleware/reddit'
|
4
|
+
require 'json'
|
5
|
+
|
6
|
+
require 'reddit/base/version'
|
7
|
+
require 'reddit/base/basic_client'
|
8
|
+
require 'reddit/base/client'
|
9
|
+
require 'reddit/base/helpers'
|
10
|
+
|
11
|
+
module Reddit
|
12
|
+
module Base
|
13
|
+
end
|
14
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
module Reddit
|
2
|
+
module Base
|
3
|
+
# Basic client that doesn't make assumptions regarding response type.
|
4
|
+
#
|
5
|
+
# The reddit API isn't always consistent with its response types so may
|
6
|
+
# return HTML even when JSON is requested. Error pages are a common and
|
7
|
+
# unavoidable example.
|
8
|
+
class BasicClient
|
9
|
+
extend Forwardable
|
10
|
+
|
11
|
+
DEFAULT_OPTIONS = {
|
12
|
+
url: 'http://www.reddit.com',
|
13
|
+
headers: {'User-Agent' => "reddit-base, a reddit client for ruby by /u/dobs (v#{VERSION})"}
|
14
|
+
}.freeze
|
15
|
+
|
16
|
+
attr_reader :connection, :options
|
17
|
+
|
18
|
+
def_delegators :connection, :get, :post, :params, :headers
|
19
|
+
|
20
|
+
def initialize(options)
|
21
|
+
@options = DEFAULT_OPTIONS.merge(options)
|
22
|
+
@connection = Faraday.new(url: @options[:url], headers: @options[:headers]) do |builder|
|
23
|
+
builder.request :multipart
|
24
|
+
builder.request :url_encoded
|
25
|
+
builder.request :reddit_authentication, @options
|
26
|
+
builder.response :follow_redirects
|
27
|
+
builder.use :reddit_rate_limit
|
28
|
+
builder.use :reddit_modhash
|
29
|
+
builder.adapter Faraday.default_adapter
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
module Reddit
|
2
|
+
module Base
|
3
|
+
# Client that does everything BasicClient does but also attempts to
|
4
|
+
# coerce and parse JSON.
|
5
|
+
class Client < BasicClient
|
6
|
+
def initialize(options)
|
7
|
+
super(options)
|
8
|
+
|
9
|
+
connection.builder.insert_before FaradayMiddleware::FollowRedirects, FaradayMiddleware::ParseJson
|
10
|
+
connection.builder.insert_before FaradayMiddleware::Reddit::RateLimit, FaradayMiddleware::Reddit::ForceJson
|
11
|
+
end
|
12
|
+
|
13
|
+
def get(*args, **options)
|
14
|
+
body = connection.get(*args, *options).body
|
15
|
+
body = Reddit::Base::Helpers.simplify body if options[:simplify]
|
16
|
+
body
|
17
|
+
end
|
18
|
+
|
19
|
+
def post(*args, **options)
|
20
|
+
body = connection.post(*args, *options).body
|
21
|
+
body = Reddit::Base::Helpers.simplify body if options[:simplify]
|
22
|
+
body
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
module Reddit
|
2
|
+
module Base
|
3
|
+
module Helpers
|
4
|
+
def self.simplify(json)
|
5
|
+
return json unless json.is_a? Hash
|
6
|
+
|
7
|
+
if json['data']
|
8
|
+
json['data']['kind'] = json['kind'] if json['data'].is_a? Hash
|
9
|
+
json = simplify json['data']
|
10
|
+
elsif json['children']
|
11
|
+
json['children'] = json['children'].map { |child| simplify child }
|
12
|
+
else
|
13
|
+
json.keys.each do |key|
|
14
|
+
json[key] = simplify json[key]
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
json
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
data/reddit-base.gemspec
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'reddit/base/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "reddit-base"
|
8
|
+
spec.version = Reddit::Base::VERSION
|
9
|
+
spec.authors = ["Daniel O'Brien"]
|
10
|
+
spec.email = ["dan@dobs.org"]
|
11
|
+
spec.summary = %q{A minimal reddit API client for Ruby.}
|
12
|
+
spec.description = %q{A minimal reddit API client for Ruby that
|
13
|
+
simplifies concerns such authentication, rate limiting and extracting
|
14
|
+
JSON.}
|
15
|
+
spec.homepage = ""
|
16
|
+
spec.license = "MIT"
|
17
|
+
spec.required_ruby_version = '>= 2.0.0'
|
18
|
+
|
19
|
+
spec.files = `git ls-files -z`.split("\x0")
|
20
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
21
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
22
|
+
spec.require_paths = ["lib"]
|
23
|
+
|
24
|
+
spec.add_development_dependency "bundler", "~> 1.5"
|
25
|
+
spec.add_development_dependency "rake"
|
26
|
+
|
27
|
+
spec.add_dependency 'faraday_middleware-reddit', '0.2.1'
|
28
|
+
end
|
metadata
ADDED
@@ -0,0 +1,100 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: reddit-base
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.2.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Daniel O'Brien
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-04-25 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ~>
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.5'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ~>
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.5'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - '>='
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - '>='
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: faraday_middleware-reddit
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - '='
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: 0.2.1
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - '='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: 0.2.1
|
55
|
+
description: |-
|
56
|
+
A minimal reddit API client for Ruby that
|
57
|
+
simplifies concerns such authentication, rate limiting and extracting
|
58
|
+
JSON.
|
59
|
+
email:
|
60
|
+
- dan@dobs.org
|
61
|
+
executables: []
|
62
|
+
extensions: []
|
63
|
+
extra_rdoc_files: []
|
64
|
+
files:
|
65
|
+
- .gitignore
|
66
|
+
- Gemfile
|
67
|
+
- LICENSE.txt
|
68
|
+
- README.md
|
69
|
+
- Rakefile
|
70
|
+
- lib/reddit/base.rb
|
71
|
+
- lib/reddit/base/basic_client.rb
|
72
|
+
- lib/reddit/base/client.rb
|
73
|
+
- lib/reddit/base/helpers.rb
|
74
|
+
- lib/reddit/base/version.rb
|
75
|
+
- reddit-base.gemspec
|
76
|
+
homepage: ''
|
77
|
+
licenses:
|
78
|
+
- MIT
|
79
|
+
metadata: {}
|
80
|
+
post_install_message:
|
81
|
+
rdoc_options: []
|
82
|
+
require_paths:
|
83
|
+
- lib
|
84
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
85
|
+
requirements:
|
86
|
+
- - '>='
|
87
|
+
- !ruby/object:Gem::Version
|
88
|
+
version: 2.0.0
|
89
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
90
|
+
requirements:
|
91
|
+
- - '>='
|
92
|
+
- !ruby/object:Gem::Version
|
93
|
+
version: '0'
|
94
|
+
requirements: []
|
95
|
+
rubyforge_project:
|
96
|
+
rubygems_version: 2.0.3
|
97
|
+
signing_key:
|
98
|
+
specification_version: 4
|
99
|
+
summary: A minimal reddit API client for Ruby.
|
100
|
+
test_files: []
|