whatcd 0.1.0

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.
Files changed (2) hide show
  1. data/lib/whatcd.rb +84 -0
  2. metadata +58 -0
data/lib/whatcd.rb ADDED
@@ -0,0 +1,84 @@
1
+ require 'httparty'
2
+
3
+ # Public: An API wrapper for What.cd's JSON API.
4
+ class WhatCD
5
+ include HTTParty
6
+
7
+ base_uri 'what.cd'
8
+ maintain_method_across_redirects
9
+ format :json
10
+
11
+ class << self
12
+ # Public: Authenticates with the API. Keep in mind that you only have six
13
+ # tries!
14
+ #
15
+ # username - The username String.
16
+ # password - The password String.
17
+ #
18
+ # Raises an AuthError.
19
+ def authenticate(username, password)
20
+ body = { username: username, password: password }
21
+ response = post '/login.php', body: body,
22
+ follow_redirects: false
23
+
24
+ if response.headers.has_key? 'set-cookie'
25
+ headers 'Cookie' => response.headers['set-cookie']
26
+ else
27
+ raise AuthError
28
+ end
29
+ end
30
+
31
+ # Public: Checks if there's properly authenticated.
32
+ #
33
+ # Returns a Boolean.
34
+ def authenticated?
35
+ headers.has_key? 'Cookie'
36
+ end
37
+
38
+ # Internal: Makes a request.
39
+ #
40
+ # action - An action name String.
41
+ # query - A query Hash to send along (default: {}).
42
+ #
43
+ # Returns a Hash representing JSON. Raises an APIError.
44
+ def make_request(action, query = {})
45
+ raise AuthError unless authenticated?
46
+
47
+ result = get '/ajax.php', query: query.merge(action: action)
48
+
49
+ if result.code != 200 || result['status'] == 'failure'
50
+ raise(APIError)
51
+ else
52
+ result['response']
53
+ end
54
+ end
55
+
56
+ # Public: Make a request. The "method" name (capitalized!) is one of the
57
+ # API actions (ajax.php?action=<this-bit>).
58
+ #
59
+ # query - A query Hash.
60
+ #
61
+ # Examples
62
+ #
63
+ # WhatCD::User id => 666
64
+ # # => <Hash ...>
65
+ #
66
+ # Signature
67
+ #
68
+ # Method(query)
69
+ def method_missing(method, *args, &block)
70
+ if method.to_s == method.to_s.capitalize
71
+ make_request method.to_s.downcase, args.first
72
+ else
73
+ super
74
+ end
75
+ end
76
+ end
77
+
78
+ # Internal: Gets raised whenever failure occured. Error messages will be
79
+ # vague, as What.cd's API doesn't give any reasons for failure.
80
+ class APIError < Exception; end
81
+
82
+ # Internal: Gets raised when a request is made without authenticating first.
83
+ class AuthError < Exception; end
84
+ end
metadata ADDED
@@ -0,0 +1,58 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: whatcd
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Paul Brickfeld
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-08-13 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: httparty
16
+ requirement: &2157115900 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *2157115900
25
+ description: An API wrapper for What.cd's JSON API
26
+ email:
27
+ - paulbrickfeld@gmail.com
28
+ executables: []
29
+ extensions: []
30
+ extra_rdoc_files: []
31
+ files:
32
+ - lib/whatcd.rb
33
+ homepage: http://britishtea.github.com/whatcd
34
+ licenses: []
35
+ post_install_message:
36
+ rdoc_options: []
37
+ require_paths:
38
+ - lib
39
+ required_ruby_version: !ruby/object:Gem::Requirement
40
+ none: false
41
+ requirements:
42
+ - - ! '>='
43
+ - !ruby/object:Gem::Version
44
+ version: '0'
45
+ required_rubygems_version: !ruby/object:Gem::Requirement
46
+ none: false
47
+ requirements:
48
+ - - ! '>='
49
+ - !ruby/object:Gem::Version
50
+ version: '0'
51
+ requirements: []
52
+ rubyforge_project:
53
+ rubygems_version: 1.8.13
54
+ signing_key:
55
+ specification_version: 3
56
+ summary: An API wrapper for What.cd's JSON API
57
+ test_files: []
58
+ has_rdoc: