rippy_rules 0.0.1 → 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README.md +9 -0
- data/lib/rippy_rules/rippy_rules.rb +16 -5
- data/lib/rippy_rules/version.rb +1 -1
- data/spec/lib/rippy_rules/rippy_rules_spec.rb +21 -16
- metadata +2 -2
data/README.md
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
<a href="https://codeclimate.com/github/MrCheese/rippy_rules"><img src="https://codeclimate.com/badge.png" /></a>
|
2
|
+
|
1
3
|
# RippyRules
|
2
4
|
|
3
5
|
A Ruby wrapper for the What.cd JSON API
|
@@ -36,6 +38,13 @@ client = RippyRules::Client.new(your_whatcd_username, your_whatcd_password)
|
|
36
38
|
results = client.browse({searchstr: 'Bruce Springsteen'})
|
37
39
|
```
|
38
40
|
|
41
|
+
There is also a search method that can be used for simpler searches where you only care about getting results.
|
42
|
+
It is equivalent to calling browse with a searchstr
|
43
|
+
```ruby
|
44
|
+
client = RippyRules::Client.new(your_whatcd_username, your_whatcd_password)
|
45
|
+
results = client.search 'Bruce Springsteen'
|
46
|
+
```
|
47
|
+
|
39
48
|
## Contributing
|
40
49
|
|
41
50
|
To contribute to RippyRules, you must have a What.cd account. I do not have one and I do not know how to acquire one.
|
@@ -11,22 +11,32 @@ module RippyRules
|
|
11
11
|
authenticate
|
12
12
|
end
|
13
13
|
|
14
|
+
def search(str)
|
15
|
+
browse({searchstr: str})
|
16
|
+
end
|
17
|
+
|
14
18
|
def method_missing(method, *params)
|
15
|
-
|
16
|
-
|
19
|
+
first_param = params.first
|
20
|
+
raise APIError unless first_param.is_a?(Hash)
|
21
|
+
perform_request(method.to_s,first_param)
|
17
22
|
end
|
18
23
|
|
19
24
|
def perform_request(method, query={})
|
20
|
-
self.class.get("/ajax.php
|
25
|
+
self.class.get("/ajax.php", :query => query.merge({action: method}))
|
21
26
|
end
|
22
27
|
|
23
28
|
private
|
24
29
|
|
25
30
|
def authenticate
|
26
|
-
body = {:
|
31
|
+
body = {username: @username, password: @password, keeplogged: 1}
|
32
|
+
response = login(body)
|
33
|
+
cookies(response.headers['set-cookie'])
|
34
|
+
end
|
35
|
+
|
36
|
+
def login(body)
|
27
37
|
response = self.class.post('/login.php', :body => body, :follow_redirects => false)
|
28
38
|
raise AuthenticationError if response.headers['set-cookie'].nil?
|
29
|
-
|
39
|
+
response
|
30
40
|
end
|
31
41
|
|
32
42
|
def cookies(cookie)
|
@@ -34,6 +44,7 @@ module RippyRules
|
|
34
44
|
cookie_jar.add_cookies cookie
|
35
45
|
self.class.cookies cookie_jar
|
36
46
|
end
|
47
|
+
|
37
48
|
end
|
38
49
|
class AuthenticationError < StandardError; end
|
39
50
|
class APIError < StandardError; end
|
data/lib/rippy_rules/version.rb
CHANGED
@@ -3,34 +3,39 @@ require 'spec_helper'
|
|
3
3
|
describe RippyRules::Client do
|
4
4
|
use_vcr_cassette :record => :new_episodes, :match_requests_on => [:body, :uri]
|
5
5
|
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
end
|
6
|
+
subject{RippyRules::Client.new(WHATCD_USERNAME,WHATCD_PASSWORD)}
|
7
|
+
|
8
|
+
it {subject.kind_of?(HTTParty).should be_true}
|
10
9
|
|
11
10
|
describe "#initialize" do
|
12
11
|
context "when given valid authentication information" do
|
13
12
|
it "authenticates the user" do
|
14
|
-
|
15
|
-
rippy_rules.class.cookies[:session].nil?.should == false
|
13
|
+
subject.class.cookies[:session].nil?.should be_false
|
16
14
|
end
|
17
15
|
end
|
18
16
|
|
19
17
|
context "when the authentication fails" do
|
20
18
|
it "throws an AuthenticationError" do
|
21
|
-
expect{
|
19
|
+
expect{RippyRules::Client.new('foo', 'bar')}.to raise_error(RippyRules::AuthenticationError)
|
22
20
|
end
|
23
21
|
end
|
24
22
|
end
|
25
23
|
|
24
|
+
describe "#search" do
|
25
|
+
it "calls browse with the given string as 'searchstr'" do
|
26
|
+
str = 'bruce springsteen'
|
27
|
+
subject.expects(:browse).with({searchstr: str})
|
28
|
+
subject.search str
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
26
32
|
describe "#method_missing?" do
|
27
|
-
let(:rippy_rules){RippyRules::Client.new(WHATCD_USERNAME, WHATCD_PASSWORD)}
|
28
33
|
context "when given an action and a hash" do
|
29
34
|
it "performs a request with the method name as the request type" do
|
30
35
|
method = 'top10'
|
31
|
-
params = {:
|
32
|
-
results =
|
33
|
-
results['response'].first['results'].size
|
36
|
+
params = {type: :users}
|
37
|
+
results = subject.send(method.to_sym, params)
|
38
|
+
results['response'].first['results'].size.should eq(10)
|
34
39
|
end
|
35
40
|
end
|
36
41
|
|
@@ -38,18 +43,18 @@ describe RippyRules::Client do
|
|
38
43
|
it "throws an APIError" do
|
39
44
|
method = 'eatmyshorts'
|
40
45
|
param = 'foo'
|
41
|
-
expect{
|
46
|
+
expect{subject.send(method.to_sym,param)}.to raise_error(RippyRules::APIError)
|
42
47
|
end
|
43
48
|
end
|
44
49
|
end
|
45
50
|
|
46
51
|
describe "#perform_request" do
|
47
|
-
let(:rippy_rules){RippyRules::Client.new(WHATCD_USERNAME, WHATCD_PASSWORD)}
|
48
52
|
it "performs a GET request with the correct parameters" do
|
49
53
|
method = 'top10'
|
50
|
-
query = {:
|
51
|
-
|
52
|
-
|
54
|
+
query = {limit: 10, type: :torrents}
|
55
|
+
query_with_method = query.merge({action: method})
|
56
|
+
subject.class.expects(:get).with("/ajax.php", query: query_with_method)
|
57
|
+
subject.send(method.to_sym, query)
|
53
58
|
end
|
54
59
|
end
|
55
60
|
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rippy_rules
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-11-
|
12
|
+
date: 2012-11-10 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rspec
|