reddit-base 0.3.1 → 0.4.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +28 -5
- data/lib/reddit/base.rb +1 -0
- data/lib/reddit/base/client.rb +4 -6
- data/lib/reddit/base/helpers.rb +0 -16
- data/lib/reddit/base/mash.rb +17 -0
- data/lib/reddit/base/version.rb +1 -1
- data/reddit-base.gemspec +1 -0
- metadata +17 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4232cd6654bdea59d59a30f9ab7ec08edd20c0b8
|
4
|
+
data.tar.gz: 07d3bfef1b8886603e6b1fabf3ffbd7db59ff311
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 2c8ec53b4ece9178ade713a2ec316af9e23da7474517c14904f34cd8cccb273b0f9e1f405023576f6fc56e524910d5510eead64fff3c0febe2e87fe3b5ddfea8
|
7
|
+
data.tar.gz: 508f49c87fefbdb5804aab9f01eca73d8029f1ae36163abd29133ee75bc340b6b53687a5e17dee7440bbf4b7c4d139f16011021772a6d8e9a3dffb42a69589af
|
data/README.md
CHANGED
@@ -3,6 +3,8 @@ Reddit Base
|
|
3
3
|
|
4
4
|
A minimal reddit API client for Ruby.
|
5
5
|
|
6
|
+
[![Gem Version](https://badge.fury.io/rb/reddit-base.png)](http://badge.fury.io/rb/reddit-base)
|
7
|
+
|
6
8
|
Motivation
|
7
9
|
----------
|
8
10
|
|
@@ -90,18 +92,39 @@ For example, uploading an image to a subreddit you moderate:
|
|
90
92
|
|
91
93
|
```ruby
|
92
94
|
image_upload = Reddit::Base::UploadIO.new('/path/to/your/image.png', 'image/png')
|
93
|
-
|
95
|
+
client.post('/api/upload_sr_img.json', r: SUBREDDIT, file: image_upload, header: 0, name: 'example'
|
96
|
+
```
|
97
|
+
|
98
|
+
### Traversal
|
99
|
+
|
100
|
+
`Client` returns a type of `Hashie::Mash` so instead of:
|
101
|
+
|
102
|
+
```ruby
|
103
|
+
client.get('/r/AskReddit')['data']['children']
|
94
104
|
```
|
95
105
|
|
96
|
-
|
106
|
+
You can do:
|
97
107
|
|
98
|
-
|
99
|
-
|
108
|
+
```ruby
|
109
|
+
client.get('/r/AskReddit').data.children
|
110
|
+
```
|
111
|
+
|
112
|
+
As a bonus it also forwards any missed methods along to its `data` attribute, so you can take it a step
|
113
|
+
further and just do:
|
100
114
|
|
101
115
|
```ruby
|
102
|
-
client.get('/r/AskReddit'
|
116
|
+
client.get('/r/AskReddit').children
|
103
117
|
```
|
104
118
|
|
119
|
+
Frequently Asked Questions
|
120
|
+
--------------------------
|
121
|
+
|
122
|
+
### Why do some requests 403 the first time but succeed when repeated?
|
123
|
+
|
124
|
+
The most common cause is that you're attempting to access something private and your modhash hasn't
|
125
|
+
been set yet. Try making a request against a public endpoint (e.g. `client.get('/r/AskReddit')`) at
|
126
|
+
the start of each session.
|
127
|
+
|
105
128
|
Recommended Reading
|
106
129
|
-------------------
|
107
130
|
|
data/lib/reddit/base.rb
CHANGED
data/lib/reddit/base/client.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
require 'hashie'
|
2
|
+
|
1
3
|
module Reddit
|
2
4
|
module Base
|
3
5
|
# Client that does everything BasicClient does but also attempts to
|
@@ -11,17 +13,13 @@ module Reddit
|
|
11
13
|
end
|
12
14
|
|
13
15
|
def get(*args, **options)
|
14
|
-
simplify = options.delete(:simplify)
|
15
16
|
body = connection.get(*args, **options).body
|
16
|
-
|
17
|
-
body
|
17
|
+
Mash.new body
|
18
18
|
end
|
19
19
|
|
20
20
|
def post(*args, **options)
|
21
|
-
simplify = options.delete(:simplify)
|
22
21
|
body = connection.post(*args, **options).body
|
23
|
-
|
24
|
-
body
|
22
|
+
Mash.new body
|
25
23
|
end
|
26
24
|
end
|
27
25
|
end
|
data/lib/reddit/base/helpers.rb
CHANGED
@@ -1,22 +1,6 @@
|
|
1
1
|
module Reddit
|
2
2
|
module Base
|
3
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
4
|
end
|
21
5
|
end
|
22
6
|
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
module Reddit
|
2
|
+
module Base
|
3
|
+
class Mash < Hashie::Mash
|
4
|
+
def method_missing(method, *args, &block)
|
5
|
+
if self['data'] && self['data'].has_key?(method)
|
6
|
+
self['data'].send(method, *args, &block)
|
7
|
+
else
|
8
|
+
super(method, *args, &block)
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
def respond_to?(method)
|
13
|
+
(self.respond_to?(:data) && self.data.respond_to?(method)) || super
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
data/lib/reddit/base/version.rb
CHANGED
data/reddit-base.gemspec
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: reddit-base
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.4.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Daniel O'Brien
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-04-
|
11
|
+
date: 2014-04-28 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -52,6 +52,20 @@ dependencies:
|
|
52
52
|
- - '='
|
53
53
|
- !ruby/object:Gem::Version
|
54
54
|
version: 0.3.1
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: hashie
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ~>
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: 2.1.1
|
62
|
+
type: :runtime
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ~>
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: 2.1.1
|
55
69
|
description: |-
|
56
70
|
A minimal reddit API client for Ruby that
|
57
71
|
simplifies concerns such as authentication, rate limiting and extracting
|
@@ -71,6 +85,7 @@ files:
|
|
71
85
|
- lib/reddit/base/basic_client.rb
|
72
86
|
- lib/reddit/base/client.rb
|
73
87
|
- lib/reddit/base/helpers.rb
|
88
|
+
- lib/reddit/base/mash.rb
|
74
89
|
- lib/reddit/base/upload_io.rb
|
75
90
|
- lib/reddit/base/version.rb
|
76
91
|
- reddit-base.gemspec
|