qihu360 0.3.15 → 0.3.16

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: 1dd5017488ec22ca95132048253c31b8c012a742
4
- data.tar.gz: 5b4292e94a7403b5e430c54468129ceff799bbda
3
+ metadata.gz: 608d937acd6b11c607f50bb6bcce140037aa298d
4
+ data.tar.gz: abbf0b233894ab66a411c38e0d2f276e3335b51a
5
5
  SHA512:
6
- metadata.gz: c086d2c1376c8f850cf5986e98146aea176b9b4a5f5ae4cadbf67229d3e590f362833a8c292f77f82bc30ead02ffbe6ade839ad21552681c64e1296999273651
7
- data.tar.gz: d810eb3044043deb3751d84fdb9fa7c930b8be455fcc6445db07e3bdd873ad77b7c1bb5b9d06666b3999709b8acdffc6dc208ac3967bc34f070a84cd8752b91c
6
+ metadata.gz: 9891b3eb7aa54f240deb0e2e3cceaa2f55877d67c395ed10c3d13edfe96abc6a1fb16fb1857543f9149290b406d5b32b0388508b1429dec9f4422f3b706f15f9
7
+ data.tar.gz: dc90d8905e6c8398e7967c7db4e73798dd329dcbd90206cf390aedca1ec21c6c36329213cff80e0a22ca750d3944789aeba86a89b2d5dcbc3b9852fff7a0bcca
@@ -1,3 +1,4 @@
1
1
  require "qihu/version"
2
2
  require "qihu/auth"
3
+ require "qihu/error"
3
4
  require "qihu/dianjing"
@@ -26,13 +26,61 @@ module Qihu
26
26
  end
27
27
 
28
28
  uri_path = uri_path(name)
29
- @client.auth.token.request(method, uri_path, :headers => client_headers, :params => params)
29
+ r = @client.auth.token.request(method, uri_path, :headers => client_headers, :params => params)
30
+
31
+ if self.json?(r.headers)
32
+ data = self.to_json(r.body)
33
+ elsif self.xml?(r.headers)
34
+ root = "#{self.url_section}_#{name}_response"
35
+ data = self.to_xml(r.body)
36
+ data = data[root]
37
+ end
38
+
39
+ if data.has_key?('failures')
40
+ key = data['failures'].kind_of?(Hash) ? 'item' : 0
41
+ code = data['failures'][key]['code'].to_i
42
+ error = data['failures'][key]['message']
43
+
44
+ raise Qihu::FailuresError.new(code, error)
45
+ end
46
+
47
+ return r
30
48
  rescue OAuth2::Error => e
31
- raise "Invaild API: '#{@client.site}/#{uri_path}' with message:\n#{e.message}"
49
+ raise Qihu::InvailAPIError.new("#{@client.site}/#{uri_path}", e.message)
32
50
  end
33
51
  end
34
52
 
35
53
  protected
54
+ # Convert response content to JSON format
55
+ #
56
+ # @return [Hash]
57
+ def to_json(body)
58
+ MultiJson.load(body)
59
+ end
60
+
61
+ # Convert response content to XML format
62
+ #
63
+ # @return [Hash]
64
+ def to_xml(body, parser=nil)
65
+ MultiXml.parser = p if parser and [:ox, :libxml, :nokogiri, :rexml].include?parser.to_sym
66
+ MultiXml.parse(body)
67
+ end
68
+
69
+ # Whether or not the content is json content type
70
+ #
71
+ # @return [Boolean]
72
+ def json?(headers)
73
+ (headers[:content_type].include?'json') ? true : false
74
+ end
75
+
76
+ # Whether or not the content is xml content type
77
+ #
78
+ # @return [Boolean]
79
+ def xml?(headers)
80
+ (['xml', 'html'].any? { |word| headers[:content_type].include?(word) }) ? true : false
81
+ end
82
+
83
+
36
84
  def client_headers
37
85
  {
38
86
  :apiKey => @client.auth.token.client.id,
@@ -41,8 +89,12 @@ module Qihu
41
89
  }
42
90
  end
43
91
 
92
+ def url_section
93
+ self.class.name.split("::")[-1].downcase
94
+ end
95
+
44
96
  def uri_path(uri)
45
- class_name = self.class.name.split("::")[-1].downcase
97
+ class_name = self.url_section
46
98
  "#{@client.version}/#{class_name}/#{uri}"
47
99
  end
48
100
  end
@@ -0,0 +1,25 @@
1
+ module Qihu
2
+ class Error < StandardError
3
+ end
4
+
5
+ class InvailAPIError < Qihu::Error
6
+ attr_reader :url, :error
7
+
8
+ def initialize(url, error)
9
+ @url = url
10
+ @error = error
11
+
12
+ super "Invaild API: [#{@url}]"
13
+ end
14
+ end
15
+
16
+ class FailuresError < Qihu::Error
17
+ attr_reader :code, :error
18
+ def initialize(code, error)
19
+ @code = code
20
+ @error = error
21
+
22
+ super "[#{@code}] #{@error}"
23
+ end
24
+ end
25
+ end
@@ -1,3 +1,3 @@
1
1
  module Qihu
2
- VERSION = "0.3.15"
2
+ VERSION = "0.3.16"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: qihu360
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.15
4
+ version: 0.3.16
5
5
  platform: ruby
6
6
  authors:
7
7
  - icyleaf
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-09-24 00:00:00.000000000 Z
11
+ date: 2013-11-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -105,6 +105,7 @@ files:
105
105
  - lib/qihu/dianjing/api/report.rb
106
106
  - lib/qihu/dianjing/api/tool.rb
107
107
  - lib/qihu/dianjing/client.rb
108
+ - lib/qihu/error.rb
108
109
  - lib/qihu/version.rb
109
110
  - qihu360.gemspec
110
111
  - spec/qihu/auth_spec.rb
@@ -130,7 +131,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
130
131
  version: '0'
131
132
  requirements: []
132
133
  rubyforge_project:
133
- rubygems_version: 2.0.6
134
+ rubygems_version: 2.0.3
134
135
  signing_key:
135
136
  specification_version: 4
136
137
  summary: Qihu 360.cn OAuth2 & Dianjing API wrapper