oa_test 0.0.3 → 0.0.4
Sign up to get free protection for your applications and to get access to all the features.
- data/bin/oa_test +2 -2
- data/lib/oa_test/from_jsonxml.rb +55 -0
- data/lib/oa_test/oa_test.rb +15 -8
- data/lib/oa_test/version.rb +1 -1
- metadata +26 -9
data/bin/oa_test
CHANGED
@@ -5,7 +5,7 @@ puts <<END
|
|
5
5
|
How to oa_test:
|
6
6
|
~~~~~~~~~~~~~~~~~~~~~~~
|
7
7
|
syntax:
|
8
|
-
oa_test [--consumer:token/secret] --yauth:login/passwd [--return:body|boolean|object] [--host:host] [--api:path]
|
8
|
+
oa_test [--consumer:token/secret] --yauth:login/passwd [--return:body|body_hash|boolean|object] [--host:host] [--api:path]
|
9
9
|
|
10
10
|
Enjoy...
|
11
11
|
|
@@ -30,7 +30,7 @@ if arg.size>0
|
|
30
30
|
OA = oa = OAuthTest.new(
|
31
31
|
:key => cons[0].strip,
|
32
32
|
:secret => cons[1].strip,
|
33
|
-
:return => (retrn ? retrn.to_sym : :body)
|
33
|
+
:return => (retrn ? retrn.split(':')[1].to_sym : :body)
|
34
34
|
).yauth(
|
35
35
|
:login => yths[0].strip,
|
36
36
|
:passwd => yths[1].strip
|
@@ -0,0 +1,55 @@
|
|
1
|
+
require 'xml/libxml'
|
2
|
+
# adapted from
|
3
|
+
# http://movesonrails.com/articles/2008/02/25/libxml-for-active-resource-2-0
|
4
|
+
|
5
|
+
class Hash
|
6
|
+
class << self
|
7
|
+
def from_json(str)
|
8
|
+
JSON.parse(str)
|
9
|
+
end
|
10
|
+
|
11
|
+
def from_xml(xml, strict=true)
|
12
|
+
begin
|
13
|
+
XML.default_load_external_dtd = false
|
14
|
+
XML.default_pedantic_parser = strict
|
15
|
+
result = XML::Parser.string(xml).parse
|
16
|
+
return { result.root.name.to_s => xml_node_to_hash(result.root)}
|
17
|
+
rescue Exception => e
|
18
|
+
# raise your custom exception here
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
def xml_node_to_hash(node)
|
23
|
+
# If we are at the root of the document, start the hash
|
24
|
+
if node.element?
|
25
|
+
if node.children?
|
26
|
+
result_hash = {}
|
27
|
+
|
28
|
+
node.each_child do |child|
|
29
|
+
result = xml_node_to_hash(child)
|
30
|
+
|
31
|
+
if child.name == "text"
|
32
|
+
if !child.next? and !child.prev?
|
33
|
+
return result
|
34
|
+
end
|
35
|
+
elsif result_hash[child.name]
|
36
|
+
if result_hash[child.name].is_a?(Object::Array)
|
37
|
+
result_hash[child.name] << result
|
38
|
+
else
|
39
|
+
result_hash[child.name] = [result_hash[child.name]] << result
|
40
|
+
end
|
41
|
+
else
|
42
|
+
result_hash[child.name] = result
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
return result_hash
|
47
|
+
else
|
48
|
+
return nil
|
49
|
+
end
|
50
|
+
else
|
51
|
+
return node.content.to_s
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
data/lib/oa_test/oa_test.rb
CHANGED
@@ -8,9 +8,9 @@ class OAuthTest
|
|
8
8
|
def initialize(ks)
|
9
9
|
@key = ks[:key]
|
10
10
|
@secret = ks[:secret]
|
11
|
-
@return = ks[:return]
|
12
|
-
@host = ks[:host]
|
13
|
-
@api = ks[:api]
|
11
|
+
@return = ks[:return]
|
12
|
+
@host = ks[:host] ? ks[:host] : 'http://www.koprol.com'
|
13
|
+
@api = ks[:api] ? ks[:api] : '/api/v2'
|
14
14
|
@typ = {
|
15
15
|
:json => {"Accept" => "application/json", "Content-Type" => "application/json"},
|
16
16
|
:xml => {"Accept" => "application/xml", "Content-Type" => "application/xml"},
|
@@ -55,17 +55,24 @@ class OAuthTest
|
|
55
55
|
end
|
56
56
|
|
57
57
|
def typ(path)
|
58
|
-
ext = path[/.(json|xml)($|\?)/,1]
|
59
|
-
ext = 'json' if
|
60
|
-
@typ[ext.to_sym]
|
58
|
+
@ext = path[/.(json|xml)($|\?)/,1]
|
59
|
+
@ext = 'json' if !@ext
|
60
|
+
@typ[@ext.to_sym]
|
61
61
|
end
|
62
62
|
|
63
63
|
def rtn(resp)
|
64
|
-
|
64
|
+
puts resp.class
|
65
|
+
if @return==nil || resp.class!=Net::HTTPOK || @return==:body
|
65
66
|
resp.body
|
67
|
+
elsif @return==:body_hash && resp.class==Net::HTTPOK
|
68
|
+
if @ext=='json'
|
69
|
+
Hash.from_json(resp.body)
|
70
|
+
elsif @ext=='xml'
|
71
|
+
Hash.from_xml(resp.body)
|
72
|
+
end
|
66
73
|
elsif @return==:boolean
|
67
74
|
resp.class==Net::HTTPOK
|
68
|
-
|
75
|
+
else
|
69
76
|
resp
|
70
77
|
end
|
71
78
|
end
|
data/lib/oa_test/version.rb
CHANGED
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: oa_test
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 23
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 0
|
9
|
-
-
|
10
|
-
version: 0.0.
|
9
|
+
- 4
|
10
|
+
version: 0.0.4
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Watchmen
|
@@ -19,9 +19,25 @@ date: 2011-07-22 00:00:00 +07:00
|
|
19
19
|
default_executable: oa_test
|
20
20
|
dependencies:
|
21
21
|
- !ruby/object:Gem::Dependency
|
22
|
-
name:
|
22
|
+
name: libxml-ruby
|
23
23
|
prerelease: false
|
24
24
|
requirement: &id001 !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ~>
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
hash: 27
|
30
|
+
segments:
|
31
|
+
- 1
|
32
|
+
- 1
|
33
|
+
- 4
|
34
|
+
version: 1.1.4
|
35
|
+
type: :development
|
36
|
+
version_requirements: *id001
|
37
|
+
- !ruby/object:Gem::Dependency
|
38
|
+
name: oauth
|
39
|
+
prerelease: false
|
40
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
25
41
|
none: false
|
26
42
|
requirements:
|
27
43
|
- - ~>
|
@@ -33,11 +49,11 @@ dependencies:
|
|
33
49
|
- 1
|
34
50
|
version: 0.4.1
|
35
51
|
type: :development
|
36
|
-
version_requirements: *
|
52
|
+
version_requirements: *id002
|
37
53
|
- !ruby/object:Gem::Dependency
|
38
54
|
name: json
|
39
55
|
prerelease: false
|
40
|
-
requirement: &
|
56
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
41
57
|
none: false
|
42
58
|
requirements:
|
43
59
|
- - ~>
|
@@ -49,11 +65,11 @@ dependencies:
|
|
49
65
|
- 1
|
50
66
|
version: 0.5.1
|
51
67
|
type: :development
|
52
|
-
version_requirements: *
|
68
|
+
version_requirements: *id003
|
53
69
|
- !ruby/object:Gem::Dependency
|
54
70
|
name: httparty
|
55
71
|
prerelease: false
|
56
|
-
requirement: &
|
72
|
+
requirement: &id004 !ruby/object:Gem::Requirement
|
57
73
|
none: false
|
58
74
|
requirements:
|
59
75
|
- - ~>
|
@@ -65,7 +81,7 @@ dependencies:
|
|
65
81
|
- 1
|
66
82
|
version: 0.6.1
|
67
83
|
type: :development
|
68
|
-
version_requirements: *
|
84
|
+
version_requirements: *id004
|
69
85
|
description: OAuth mini test for yahoo property
|
70
86
|
email:
|
71
87
|
- watchmencore@yahoo.com
|
@@ -76,6 +92,7 @@ extensions: []
|
|
76
92
|
extra_rdoc_files: []
|
77
93
|
|
78
94
|
files:
|
95
|
+
- lib/oa_test/from_jsonxml.rb
|
79
96
|
- lib/oa_test/oa_test.rb
|
80
97
|
- lib/oa_test/version.rb
|
81
98
|
- lib/oa_test.rb
|