whatcd 0.1.1 → 0.1.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/lib/whatcd.rb +37 -13
- metadata +4 -4
data/lib/whatcd.rb
CHANGED
@@ -1,6 +1,16 @@
|
|
1
1
|
require 'httparty'
|
2
2
|
|
3
3
|
# Public: An API wrapper for What.cd's JSON API.
|
4
|
+
#
|
5
|
+
# Examples
|
6
|
+
#
|
7
|
+
# WhatCD::authenticate 'username', 'password'
|
8
|
+
#
|
9
|
+
# WhatCD::User :id => 666
|
10
|
+
# => { ... }
|
11
|
+
#
|
12
|
+
# WhatCD::Browse :searchstr => 'The Flaming Lips'
|
13
|
+
# => { ... }
|
4
14
|
class WhatCD
|
5
15
|
include HTTParty
|
6
16
|
|
@@ -15,14 +25,17 @@ class WhatCD
|
|
15
25
|
# username - The username String.
|
16
26
|
# password - The password String.
|
17
27
|
#
|
18
|
-
# Raises an AuthError.
|
28
|
+
# Returns a HTTParty::CookieHash. Raises an AuthError.
|
19
29
|
def authenticate(username, password)
|
20
30
|
body = { username: username, password: password }
|
21
31
|
response = post '/login.php', body: body,
|
22
32
|
follow_redirects: false
|
23
33
|
|
24
34
|
if response.headers.has_key? 'set-cookie'
|
25
|
-
|
35
|
+
cookie_jar = HTTParty::CookieHash.new
|
36
|
+
cookie_jar.add_cookies response.headers['set-cookie']
|
37
|
+
|
38
|
+
cookies cookie_jar
|
26
39
|
else
|
27
40
|
raise AuthError
|
28
41
|
end
|
@@ -32,25 +45,33 @@ class WhatCD
|
|
32
45
|
#
|
33
46
|
# Returns a Boolean.
|
34
47
|
def authenticated?
|
35
|
-
|
48
|
+
cookies.has_key? :session
|
36
49
|
end
|
37
50
|
|
38
51
|
# Public: Gets a Rippy quote.
|
39
52
|
#
|
40
|
-
#
|
41
|
-
|
53
|
+
# json - A Boolean indicating wether or not the return value should be
|
54
|
+
# JSON (default: false).
|
55
|
+
#
|
56
|
+
# Returns a String or Hash.
|
57
|
+
def rippy(json = false)
|
58
|
+
raise AuthError unless authenticated?
|
59
|
+
|
42
60
|
result = get '/ajax.php', query: { action: 'rippy', format: 'json' }
|
43
|
-
|
61
|
+
|
62
|
+
if result.code != 200 || result['status'] == 'failure'
|
63
|
+
raise(APIError)
|
64
|
+
else
|
65
|
+
json ? result : result['rippy']
|
66
|
+
end
|
44
67
|
end
|
45
68
|
|
46
|
-
alias_method :Rippy, :rippy
|
47
|
-
|
48
69
|
# Internal: Makes a request.
|
49
70
|
#
|
50
71
|
# action - An action name String.
|
51
72
|
# query - A query Hash to send along (default: {}).
|
52
73
|
#
|
53
|
-
# Returns a Hash representing JSON. Raises an APIError.
|
74
|
+
# Returns a Hash representing JSON. Raises an AuthError or APIError.
|
54
75
|
def make_request(action, query = {})
|
55
76
|
raise AuthError unless authenticated?
|
56
77
|
|
@@ -68,14 +89,16 @@ class WhatCD
|
|
68
89
|
#
|
69
90
|
# query - A query Hash.
|
70
91
|
#
|
92
|
+
# Returns a Hash. Raises an AuthError or APIError.
|
93
|
+
#
|
71
94
|
# Examples
|
72
95
|
#
|
73
|
-
# WhatCD::User id => 666
|
96
|
+
# WhatCD::User :id => 666
|
74
97
|
# # => <Hash ...>
|
75
98
|
#
|
76
99
|
# Signature
|
77
100
|
#
|
78
|
-
#
|
101
|
+
# Action(query)
|
79
102
|
def method_missing(method, *args, &block)
|
80
103
|
if method.to_s == method.to_s.capitalize
|
81
104
|
make_request method.to_s.downcase, args.first
|
@@ -84,8 +107,9 @@ class WhatCD
|
|
84
107
|
end
|
85
108
|
end
|
86
109
|
|
87
|
-
# Public: Makes a request. See method_missing for usage.
|
88
|
-
#
|
110
|
+
# Public: Makes a request without a query. See method_missing for usage.
|
111
|
+
#
|
112
|
+
# Returns a String or Hash. Raises an AuthError or APIError.
|
89
113
|
def const_missing(constant)
|
90
114
|
constant == :Rippy ? rippy : super
|
91
115
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: whatcd
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.2
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,11 +9,11 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-08-
|
12
|
+
date: 2012-08-15 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: httparty
|
16
|
-
requirement: &
|
16
|
+
requirement: &2154156560 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ! '>='
|
@@ -21,7 +21,7 @@ dependencies:
|
|
21
21
|
version: '0'
|
22
22
|
type: :runtime
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *2154156560
|
25
25
|
description: An API wrapper for What.cd's JSON API
|
26
26
|
email:
|
27
27
|
- paulbrickfeld@gmail.com
|