greentext 0.3.0 → 0.3.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: c7ba122616e32e4f72fcf28eece5496993aca498
4
- data.tar.gz: 301ad86a4d16613ded9a9a9e43a4cdd0b2018289
3
+ metadata.gz: aea6f17edaac64598a494d1a439d5db64b98230f
4
+ data.tar.gz: 06f28533e8e7cc84713f1d581778ce27989e9569
5
5
  SHA512:
6
- metadata.gz: 8af2f6b6277a4acc059445cf6735347e7946bfbed47d5c7dc1e39b9c622306fbf3bd5cf5055f56f7665f59ac7c6f11b61634963afff55401fe6f4229426d8568
7
- data.tar.gz: f7fe8d1809779ba7b6b437e7a453479802402486a01d630c1e9b7b0bed96976c67718a10ef4e0f75cd4fd90d43f685702b50785054f42dc736b3437c96cde8c9
6
+ metadata.gz: 5a4c9edbd694256e8990774ca31b93bf6ac62d65058fcfbd004c3542a2cfaed812278de462b111ec26b02888a665ee4cfaee2cc728562587a5a0bacd52c16ea2
7
+ data.tar.gz: 9ef8f01dccf3d3d01f4734c56ec42290f16c4dd85bf18b24b7a096dcde9ec0954e937f8a122e149a8c4fc2d641655fee80de9b4a5f8556eca5090880b2aff2b9
data/README.md CHANGED
@@ -1,4 +1,5 @@
1
- **Greentext** *Unofficial 4chan API Client.*
1
+ **Greentext**
2
+ *Unofficial [4chan's JSON API](https://github.com/4chan/4chan-API) Wrapper for Ruby.*
2
3
 
3
4
  [![Gem Version](https://badge.fury.io/rb/greentext.svg)](http://badge.fury.io/rb/greentext)
4
5
  [![Dependency Status](https://gemnasium.com/avidw/greentext.svg)](https://gemnasium.com/avidw/greentext)
@@ -6,71 +7,46 @@
6
7
 
7
8
  ---
8
9
 
9
- ## Usage
10
- ### Greentext::Dirty
11
-
12
- I'm still working on the main implementation. Meanwhile, one
13
- way of getting data from the API is using `Greentext::Dirty`.
14
-
15
- ```ruby
16
- require "greentext/dirty"
17
- dirty = Greentext::Dirty.new
10
+ ## Installation
18
11
 
19
- boards = dirty.boards
20
- # => [{"board"=>"3", "title"=>"3DCG" ... }, {"board"=>"a" ...
12
+ Add this line to your application's Gemfile:
21
13
 
22
- board = dirty.board(:v)
23
- # => [{:no=>543128225, :last_modified=>1398021394}, {:no=> ...
14
+ gem "greentext"
24
15
 
25
- thread = dirty.thread(:v, 543125139)
26
- # => [{:no=>543125139, :now=>"04/20/14(Sun)14:28:49", ...
16
+ And then execute:
27
17
 
28
- thread.first[:time]
29
- # => #<DateTime: 2014-04-20T18:28:49+00:00 ((2456768j,66529s,0n),+0s,2299161j)>
18
+ $ bundle
30
19
 
31
- thread.first[:url]
32
- # => "http://boards.4chan.org/b/res/543125139#p543128395"
33
- ```
20
+ Or install it yourself as:
34
21
 
35
- ### Greentext::Client
22
+ $ gem install greentext
36
23
 
37
- Haven't implemented it yet :neutral_face:.
38
- Meanwhile, use `Greentext::Dirty`.
24
+ ## Usage
39
25
 
40
26
  ```ruby
41
27
  require "greentext"
42
28
 
43
- client = Greentext::Client.new
44
- # => #<Greentext::Client:0x007f9f9a624260 @base_uri="http://a.4cdn.org/">
29
+ client = Greentext.client
30
+ # => #<Greentext::Client:0x007f9f9a624260 @api_endpoint="http://a.4cdn.org/">
45
31
 
46
- cg = client.boards.first
47
- # => #<Greentext::Client::Board:0x007f9f9aa10928 @attrs={"board"=>"3", "title"=>"3DCG", "ws_board"=>1, ... }>
48
- cg.title
32
+ board = client.boards.first
33
+ # => #<Greentext::Board:0x007f9f9aa10928 @attrs={"board"=>"3", "title"=>"3DCG", "ws_board"=>1, ... }>
34
+ board.title
49
35
  # => "3DCG"
50
- cg.ws_board?
36
+ board.worksafe?
51
37
  # => true
52
38
 
53
- post = cg.threads.first
54
- # => #<Greentext::Client::Post:0x005f4f9aa12393 @attrs={"no"=>240555661, "now"=>"04\/21\/14(Mon)13:26:27", ... }>
55
- post.time
39
+ thread = board.threads.first
40
+ # => #<Greentext::Post:0x005f4f9aa12393 @attrs={"no"=>240555661, "now"=>"04\/21\/14(Mon)13:26:27", ... }>
41
+ thread.time
56
42
  # => #<DateTime: 2014-04-21T18:24:10+00:00 ((2456769j,66250s,699039399n),+0s,2299161j)>
57
- post.comment
43
+ thread.comment
58
44
  # => "Love it. Looks like there&#039;s going to be ton of secret ... "
59
- ```
60
45
 
61
- ## Installation
62
-
63
- Add this line to your application's Gemfile:
64
-
65
- gem "greentext"
66
-
67
- And then execute:
68
-
69
- $ bundle
70
-
71
- Or install it yourself as:
72
-
73
- $ gem install greentext
46
+ # Get the rest of the thread from OP's post
47
+ all_posts = thread.expand
48
+ # => [#<Greentext::Post:0x005f4f9aa26345>, #<Greentext::Post:0x005f4f9aa27456>
49
+ ```
74
50
 
75
51
  ## Contributing
76
52
 
@@ -2,12 +2,12 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = "greentext"
5
- s.version = "0.3.0"
5
+ s.version = "0.3.1"
6
6
  s.authors = ["Avinash Dwarapu"]
7
7
  s.email = ["d.nash.avi@gmail.com"]
8
8
  s.summary = %q{Unofficial API for 4chan.}
9
9
  s.description = %q{Unofficial API for 4chan.}
10
- s.homepage = "https://github.com/avidw/greentext"
10
+ s.homepage = "https://avidw.github.io/greentext"
11
11
  s.license = "MIT"
12
12
 
13
13
  s.files = `git ls-files -z`.split("\x0")
@@ -18,8 +18,11 @@ module Greentext
18
18
  private
19
19
 
20
20
  def method_missing(method_name, *args, &block)
21
- return super unless client.respond_to?(method_name)
22
- client.send(method_name, *args, &block)
21
+ if client.respond_to?(method_name)
22
+ client.send(method_name, *args, &block)
23
+ else
24
+ super
25
+ end
23
26
  end
24
27
  end
25
28
  end
@@ -8,7 +8,7 @@ module Greentext
8
8
  attr_reader :posts_per_page, :per_page
9
9
  attr_reader :pages
10
10
  attr_predicate :worksafe, :ws_board
11
-
11
+
12
12
  def threads
13
13
  Greentext.client.threads(self)
14
14
  end
@@ -14,11 +14,12 @@ module Greentext
14
14
 
15
15
  # Public: List all available threads in a board.
16
16
  #
17
- # board - A Greentext::Board object.
17
+ # board - A Greentext::Board object or a board name String/Symbol.
18
18
  #
19
19
  # Returns an Array of Greentext::Post instances.
20
20
  def threads(board)
21
- get("/#{board.name}/catalog.json")
21
+ name = board.is_a?(Greentext::Board) ? board.name : board
22
+ get("/#{name}/catalog.json")
22
23
  .map { |page| page["threads"] }.flatten
23
24
  .map { |thread| Greentext::Post.new(thread.merge("board" => board)) }
24
25
  end
@@ -18,7 +18,7 @@ module Greentext
18
18
  Greentext::Post.new(params)
19
19
  end
20
20
  else
21
- raise ArgumentError, "This is not the first post in the thread."
21
+ fail ArgumentError, "This is not the first post in the thread."
22
22
  end
23
23
  end
24
24
  end
@@ -39,21 +39,32 @@ module Greentext
39
39
  #
40
40
  # Returns a Hash.
41
41
  def image
42
- {
43
- width: @attrs['w'],
44
- height: @attrs['h'],
45
- filename: @attrs['tim'],
46
- md5: @attrs['md5'],
47
- url: "https://i.4cdn.org/#{@attrs['board']}/#{@attrs['tim']}.#{@attrs['ext']}"
48
- }
42
+ if @attrs["tim"]
43
+ {
44
+ width: @attrs["w"],
45
+ height: @attrs["h"],
46
+ filename: @attrs["tim"],
47
+ md5: @attrs["md5"],
48
+ url: "https://i.4cdn.org/#{@attrs['board'].name}/" \
49
+ "#{@attrs['tim']}#{@attrs['ext']}"
50
+ }
51
+ else
52
+ {}
53
+ end
49
54
  end
50
55
 
51
56
  # Public: Get information about the thumbnail of the image.
52
57
  #
53
58
  # Returns a Hash.
54
59
  def thumbnail
55
- { width: @attrs["tn_w"], height: @attrs["tn_h"],
56
- url: "https://t.4cdn.org/#{@attrs['board']}/#{@attrs['tim']}s.jpg" }
60
+ if @attrs["tim"]
61
+ { width: @attrs["tn_w"], height: @attrs["tn_h"],
62
+ url: "https://t.4cdn.org/#{@attrs['board'].name}/" \
63
+ "#{@attrs['tim']}s.jpg"
64
+ }
65
+ else
66
+ {}
67
+ end
57
68
  end
58
69
 
59
70
  # Public: Determines if the post is OP.
@@ -10,14 +10,15 @@ describe Greentext::Board, :vcr do
10
10
  expect(@client.boards.sample).to be_an_instance_of(Greentext::Board)
11
11
  end
12
12
  end
13
-
13
+
14
14
  describe "#threads" do
15
15
  it "gets an Array of posts" do
16
16
  expect(@client.threads(@client.boards.sample)).to be_an(Array)
17
17
  end
18
18
 
19
19
  it "converts objects to Greentext::Post instances" do
20
- expect(@client.threads(@client.boards.sample).sample).to be_an_instance_of(Greentext::Post)
20
+ expect(@client.threads(@client.boards.sample).sample)
21
+ .to be_an_instance_of(Greentext::Post)
21
22
  end
22
23
  end
23
24
  end
@@ -1 +1 @@
1
- require "spec_helper"
1
+ require "spec_helper"
@@ -3,7 +3,7 @@ require "webmock/rspec"
3
3
  require "vcr"
4
4
 
5
5
  VCR.configure do |c|
6
- c.default_cassette_options = {record: :new_episodes}
6
+ c.default_cassette_options = { record: :new_episodes }
7
7
  c.cassette_library_dir = "spec/cassettes"
8
8
  c.hook_into :webmock
9
9
  end
@@ -17,13 +17,13 @@ RSpec.configure do |c|
17
17
  c.before :all do
18
18
  @client = Greentext.client
19
19
  end
20
-
20
+
21
21
  c.around(:each, :vcr) do |example|
22
22
  name = example.metadata[:full_description]
23
23
  .split(/\s+/, 2)
24
24
  .join("/")
25
25
  .downcase
26
- .tr(' ', '_')
26
+ .tr(" ", "_")
27
27
  .gsub(/[^\w\/]+/, "_")
28
28
  VCR.use_cassette(name) { example.call }
29
29
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: greentext
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.3.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Avinash Dwarapu
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-04-24 00:00:00.000000000 Z
11
+ date: 2014-04-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -158,13 +158,12 @@ files:
158
158
  - lib/greentext/client.rb
159
159
  - lib/greentext/client/board.rb
160
160
  - lib/greentext/client/post.rb
161
- - lib/greentext/dirty.rb
162
161
  - lib/greentext/post.rb
163
162
  - spec/board_spec.rb
164
163
  - spec/client_spec.rb
165
164
  - spec/post_spec.rb
166
165
  - spec/spec_helper.rb
167
- homepage: https://github.com/avidw/greentext
166
+ homepage: https://avidw.github.io/greentext
168
167
  licenses:
169
168
  - MIT
170
169
  metadata: {}
@@ -1,36 +0,0 @@
1
- require "faraday"
2
- require "json"
3
-
4
- module Greentext
5
- # Public: Crudely written but quick way to access the 4chan API.
6
- # Surprisingly, Rubocop's pretty okay with this mess.
7
- class Dirty
8
- def boards
9
- get("/boards.json")["boards"]
10
- end
11
-
12
- def threads(board)
13
- get("/#{board}/threads.json")
14
- .map { |page| page["threads"] }.flatten
15
- .map { |thread| symbolize(thread) }
16
- end
17
- alias_method :board, :threads
18
-
19
- def posts(board, thread)
20
- get("/#{board}/res/#{thread}.json")["posts"]
21
- .map { |post| symbolize(post) }
22
- end
23
- alias_method :thread, :posts
24
-
25
- private
26
-
27
- def get(path)
28
- res = Faraday.new(url: "http://a.4cdn.org/").get(path).body
29
- JSON.parse(res) if res.length > 2
30
- end
31
-
32
- def symbolize(hash)
33
- Hash[hash.map { |k, v| [k.to_sym, v] }]
34
- end
35
- end
36
- end