whatcd 0.1.5 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (6) hide show
  1. checksums.yaml +7 -0
  2. checksums.yaml.gz.sig +0 -0
  3. data.tar.gz.sig +6 -0
  4. data/lib/whatcd.rb +54 -93
  5. metadata +62 -16
  6. metadata.gz.sig +1 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: cb8d083335ae85367be54b14155d42b7c29b9070
4
+ data.tar.gz: c8fe42b6b6423b25ed7b77a4159a5c65265c198a
5
+ SHA512:
6
+ metadata.gz: ecc1bb291168579a900a2b1c8babae840ea16b906094b125a149d714aa50b36745bf7dbd7c6db87ed925f650d37cce379cc1cf29e2599aac68928ac05ca70469
7
+ data.tar.gz: 0d69c9c25b509d67ab326d29e298b6c4b0a13ee91530efd4acf5ebc669fbb4464af2f81da0ab107fa3cf52753f6aa84bb9bd889b75c1fe5292ae07bfc04c4251
Binary file
@@ -0,0 +1,6 @@
1
+ nvs�@�>f�U۠C�=�W�I��m�
2
+ ��g�`X�W,^Z"@�R+Yi�o��mw1}�|uw�A��l�̓n%��95>{�mC� ��6���+�����,�-g9�I��w�5�єL]ݴ��#����
3
+ DJ�m �W��5���n&ô6Fҷp�����Ǟ� 
4
+ �;u�(��{_��`��K���8�++� Ċ����_��+�Mt��&�`� 2ن[σ<ajމO>�Ǜ�
5
+ 0z]v ���f&��]
6
+
@@ -1,127 +1,88 @@
1
- require 'httparty'
1
+ require "faraday"
2
+ require "faraday-cookie_jar"
3
+ require "json"
2
4
 
3
5
  # Public: An API wrapper for What.cd's JSON API.
4
6
  #
5
7
  # Examples
6
8
  #
7
- # WhatCD::authenticate 'username', 'password'
9
+ # client = WhatCD::Client.new 'username', 'password'
8
10
  #
9
- # WhatCD::User :id => 666
11
+ # client.fetch :user, :id => 666
10
12
  # => { ... }
11
13
  #
12
- # WhatCD::Browse :searchstr => 'The Flaming Lips'
14
+ # client.fetch :browse, :searchstr => 'The Flaming Lips'
13
15
  # => { ... }
14
16
  class WhatCD
15
- include HTTParty
17
+ AuthError = Class.new StandardError
18
+ APIError = Class.new StandardError
16
19
 
17
- base_uri 'https://what.cd'
18
- maintain_method_across_redirects
19
- format :json
20
+ class Client
21
+ attr_reader :connection
20
22
 
21
- class << self
22
- # Public: Authenticates with the API. Keep in mind that you only have six
23
- # tries!
24
- #
25
- # username - The username String.
26
- # password - The password String.
27
- #
28
- # Returns a HTTParty::CookieHash. Raises an AuthError.
29
- def authenticate(username, password)
30
- body = { username: username, password: password, keeplogged: 1 }
31
- response = post '/login.php', body: body,
32
- follow_redirects: false
33
-
34
- if response.headers.has_key? 'set-cookie'
35
- cookie_jar = HTTParty::CookieHash.new
36
- cookie_jar.add_cookies response.headers['set-cookie']
23
+ def initialize(username = nil, password = nil)
24
+ @connection = Faraday.new(:url => "https://what.cd") do |builder|
25
+ builder.request :url_encoded
26
+ builder.use :cookie_jar
27
+ builder.adapter Faraday.default_adapter
28
+ end
37
29
 
38
- cookies cookie_jar
39
- else
40
- raise AuthError
30
+ unless username.nil? || password.nil?
31
+ authenticate username, password
41
32
  end
42
33
  end
43
34
 
44
- # Public: Checks if there's properly authenticated.
45
- #
46
- # Returns a Boolean.
47
- def authenticated?
48
- cookies.has_key? :session
49
- end
35
+ def authenticate(username, password)
36
+ body = { :username => username, :password => password, :keeplogged => 1 }
37
+ res = connection.post "/login.php", body
50
38
 
51
- # Public: Makes a request. The "method" name (capitalized!) is one of the
52
- # API actions (ajax.php?action=<this-bit>).
53
- #
54
- # query - A query Hash.
55
- #
56
- # Returns a Hash. Raises an AuthError or APIError.
57
- #
58
- # Examples
59
- #
60
- # WhatCD::User :id => 666
61
- # # => <Hash ...>
62
- #
63
- # Signature
64
- #
65
- # Action(query)
66
- def method_missing(method, *args, &block)
67
- if method.to_s == method.to_s.capitalize
68
- make_request method.to_s.downcase, args.first
69
- else
70
- super
39
+ unless res["set-cookie"] && res["location"] == "index.php"
40
+ raise AuthError
71
41
  end
42
+
43
+ @authenticated = true
72
44
  end
73
45
 
74
- # Public: Makes a request without a query. See method_missing for usage.
75
- #
76
- # Returns a String or Hash. Raises an AuthError or APIError.
77
- def const_missing(constant)
78
- # Rippy's response differs from the other respones.
79
- constant == :Rippy ? rippy : make_request(constant.to_s.downcase)
46
+ def set_cookie(cookie)
47
+ connection.headers["Cookie"] = cookie
48
+ @authenticated = true
80
49
  end
81
50
 
82
- private
51
+ def authenticated?
52
+ @authenticated
53
+ end
83
54
 
84
- # Internal: Makes a request.
55
+ # Public: Fetches a resource. For all possible resources and parameters see
56
+ # https://github.com/WhatCD/Gazelle/wiki/JSON-API-Documentation.
85
57
  #
86
- # action - An action name String.
87
- # query - A query Hash to send along (default: {}).
58
+ # resource - A resource name Symbol (ajax.php?action=<this part>).
59
+ # parameters - A Hash.
88
60
  #
89
- # Returns a Hash representing JSON. Raises an AuthError or APIError.
90
- def make_request(action, query = {})
91
- raise AuthError unless authenticated?
61
+ # Returns a Hash.
62
+ # Raises AuthError when not authenticated yet.
63
+ # Raises AuthError when redirected to /login.php.
64
+ # Raises APIError when a HTTP error occurs.
65
+ # Raises APIError when the response body is `{"status": "failure"}`.
66
+ def fetch(resource, parameters = {})
67
+ unless authenticated?
68
+ raise AuthError
69
+ end
92
70
 
93
- result = get '/ajax.php', query: query.merge(action: action)
71
+ res = connection.get "ajax.php", parameters.merge(:action => resource)
94
72
 
95
- if result.code != 200 || result['status'] == 'failure'
96
- raise APIError
97
- else
98
- result['response']
73
+ if res.status == "302" && res["location"] == "login.php"
74
+ raise AuthError
75
+ elsif !res.success?
76
+ raise APIError, res.status
99
77
  end
100
- end
101
78
 
102
- # Internal: Gets a Rippy quote.
103
- #
104
- # json - A Boolean indicating wether or not the return value should be
105
- # JSON (default: false).
106
- #
107
- # Returns a String or Hash.
108
- def rippy(json = false)
109
- raise AuthError unless authenticated?
79
+ parsed_res = JSON.parse res.body
110
80
 
111
- result = get '/ajax.php', query: { action: 'rippy', format: 'json' }
112
-
113
- if result.code != 200 || result['status'] == 'failure'
114
- raise(APIError)
115
- else
116
- json ? result : result['rippy']
81
+ if parsed_res["status"] == "failure"
82
+ raise APIError
117
83
  end
84
+
85
+ parsed_res["response"]
118
86
  end
119
87
  end
120
-
121
- # Internal: Gets raised whenever failure occured. Error messages will be
122
- # vague, as What.cd's API doesn't give any reasons for failure.
123
- class APIError < Exception; end
124
-
125
- # Internal: Gets raised when a request is made without authenticating first.
126
- class AuthError < Exception; end
127
88
  end
metadata CHANGED
@@ -1,30 +1,77 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: whatcd
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.5
5
- prerelease:
4
+ version: 0.2.0
6
5
  platform: ruby
7
6
  authors:
8
7
  - Paul Brickfeld
9
8
  autorequire:
10
9
  bindir: bin
11
- cert_chain: []
12
- date: 2012-08-23 00:00:00.000000000 Z
10
+ cert_chain:
11
+ - |
12
+ -----BEGIN CERTIFICATE-----
13
+ MIIDhTCCAm2gAwIBAgIBATANBgkqhkiG9w0BAQUFADBEMRYwFAYDVQQDDA1qaXB2
14
+ YW5yZWlqc2VuMRUwEwYKCZImiZPyLGQBGRYFZ21haWwxEzARBgoJkiaJk/IsZAEZ
15
+ FgNjb20wHhcNMTMwODA4MjEzMDE4WhcNMTQwODA4MjEzMDE4WjBEMRYwFAYDVQQD
16
+ DA1qaXB2YW5yZWlqc2VuMRUwEwYKCZImiZPyLGQBGRYFZ21haWwxEzARBgoJkiaJ
17
+ k/IsZAEZFgNjb20wggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQClzSDD
18
+ gHU5xJorLZBEClUFIQsAVUToqrIY25G50wd3tRRitsD4+8TKNcH/afmTZ7itFd47
19
+ Y38wLZHJ3QGNWWUzKSp75hwY1YjFT/WTt2CBU/Yisawy2Ah04fEDDNtXnUm/a4Nf
20
+ cm1iJCSO0gYqihYx/cjt8ZzMUZGQc0HjYg7jzOGOdjsozJDUtrPK+7njgJeRznBu
21
+ Zglx4JcSvSnUe3KnZYiHUAfNMbHZC6aDoWJNEJWIugDOC4ERFDQTl3u6X4WtMgK0
22
+ veZT7x84/+o2OAkXrj6Vbz4idyFlaOi+4yl5b0YJLZdzHSZEIoROzELt3kfRYIUe
23
+ +KW3UGk4RtI/zzO1AgMBAAGjgYEwfzAJBgNVHRMEAjAAMAsGA1UdDwQEAwIEsDAd
24
+ BgNVHQ4EFgQUVokOPAdYy4XiyylEpkAbiU8TclUwIgYDVR0RBBswGYEXamlwdmFu
25
+ cmVpanNlbkBnbWFpbC5jb20wIgYDVR0SBBswGYEXamlwdmFucmVpanNlbkBnbWFp
26
+ bC5jb20wDQYJKoZIhvcNAQEFBQADggEBADg9YN9UgiC7XpRuWGs/CP1sRXuiKFHL
27
+ sZCWGXivJK5VVIjUAABVDU0bG8z7kf8k2LMoXY2reVjhdKjrYYkFVMqY0U207xMA
28
+ seECzepUAXMMbLtppejnTNoIgz+w6nNfIwTutKdLx8xXGpa3z5AqYdmCWfuIj77G
29
+ 3T7twg/E2UReEgkHWULU05ISp0SODfCJzq21nKReppwVumyGBmyCOBz+uM27OXxc
30
+ 336lVuJ9KEoETd0t0biCkWVE2illvjAj0HuCi4mSA5QRkr2FbWkgFQXiMAcaakcW
31
+ xr2cJv1xwkNLUhdO3OoK0ix8tfsubY0PhA8xi8a016pr7xYJD+xUPiA=
32
+ -----END CERTIFICATE-----
33
+ date: 2014-01-27 00:00:00.000000000 Z
13
34
  dependencies:
14
35
  - !ruby/object:Gem::Dependency
15
- name: httparty
36
+ name: faraday
16
37
  requirement: !ruby/object:Gem::Requirement
17
- none: false
18
38
  requirements:
19
- - - ! '>='
39
+ - - ">="
20
40
  - !ruby/object:Gem::Version
21
41
  version: '0'
22
42
  type: :runtime
23
43
  prerelease: false
24
44
  version_requirements: !ruby/object:Gem::Requirement
25
- none: false
26
45
  requirements:
27
- - - ! '>='
46
+ - - ">="
47
+ - !ruby/object:Gem::Version
48
+ version: '0'
49
+ - !ruby/object:Gem::Dependency
50
+ name: faraday-cookie_jar
51
+ requirement: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ version: '0'
56
+ type: :runtime
57
+ prerelease: false
58
+ version_requirements: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ version: '0'
63
+ - !ruby/object:Gem::Dependency
64
+ name: cutest
65
+ requirement: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - ">="
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - ">="
28
75
  - !ruby/object:Gem::Version
29
76
  version: '0'
30
77
  description: An API wrapper for What.cd's JSON API
@@ -35,28 +82,27 @@ extensions: []
35
82
  extra_rdoc_files: []
36
83
  files:
37
84
  - lib/whatcd.rb
38
- homepage: http://britishtea.github.com/whatcd
85
+ homepage: http://github.com/britishtea/whatcd
39
86
  licenses: []
87
+ metadata: {}
40
88
  post_install_message:
41
89
  rdoc_options: []
42
90
  require_paths:
43
91
  - lib
44
92
  required_ruby_version: !ruby/object:Gem::Requirement
45
- none: false
46
93
  requirements:
47
- - - ! '>='
94
+ - - ">="
48
95
  - !ruby/object:Gem::Version
49
96
  version: '0'
50
97
  required_rubygems_version: !ruby/object:Gem::Requirement
51
- none: false
52
98
  requirements:
53
- - - ! '>='
99
+ - - ">="
54
100
  - !ruby/object:Gem::Version
55
101
  version: '0'
56
102
  requirements: []
57
103
  rubyforge_project:
58
- rubygems_version: 1.8.23
104
+ rubygems_version: 2.2.0
59
105
  signing_key:
60
- specification_version: 3
106
+ specification_version: 4
61
107
  summary: An API wrapper for What.cd's JSON API
62
108
  test_files: []
@@ -0,0 +1 @@
1
+ oA^�T������|��ߊ���J��f�㿗U�|�� ��KEbKMֶ`�0��tD[�i4����Y/����*�N��M9�O����=��ʁ� �U��`؂�v*Z}�sK�'�d��^A��%�{