oa_test 0.0.4 → 0.0.5
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/bin/oa_test +19 -11
- data/lib/oa_test/cattr.rb +264 -0
- data/lib/oa_test/oa_config.rb +44 -0
- data/lib/oa_test/oa_test.rb +66 -64
- data/lib/oa_test/oa_test_version.rb +3 -0
- data/lib/oa_test.rb +1 -0
- metadata +7 -5
- data/lib/oa_test/version.rb +0 -3
data/bin/oa_test
CHANGED
@@ -6,7 +6,8 @@ How to oa_test:
|
|
6
6
|
~~~~~~~~~~~~~~~~~~~~~~~
|
7
7
|
syntax:
|
8
8
|
oa_test [--consumer:token/secret] --yauth:login/passwd [--return:body|body_hash|boolean|object] [--host:host] [--api:path]
|
9
|
-
|
9
|
+
params --host must prefix with "http://"
|
10
|
+
params --api must prefix with "/"
|
10
11
|
Enjoy...
|
11
12
|
|
12
13
|
END
|
@@ -20,18 +21,21 @@ if arg.size>0
|
|
20
21
|
retrn = arg.select{|x|/^--return/=~x}[0]
|
21
22
|
host = arg.select{|x|/^--host/=~x}[0]
|
22
23
|
api = arg.select{|x|/^--api/=~x}[0]
|
23
|
-
|
24
|
-
|
25
|
-
yths = yauth.split(':')[1].split('/')
|
24
|
+
cons = consumer ? consumer.split(':')[1].split('/') : [nil,nil]
|
25
|
+
yths = yauth.split(':')[1].split('/')
|
26
26
|
|
27
27
|
require 'rubygems'
|
28
28
|
require 'oa_test'
|
29
29
|
|
30
|
-
OA = oa = OAuthTest.
|
31
|
-
|
32
|
-
:
|
33
|
-
:
|
34
|
-
|
30
|
+
OA = oa = OAuthTest.
|
31
|
+
new(
|
32
|
+
:key => cons[0],
|
33
|
+
:secret => cons[1],
|
34
|
+
:return => (retrn ? retrn.split(':')[1].to_sym : :body),
|
35
|
+
:host => (host ? host.split(':')[1,99].join(':') : nil),
|
36
|
+
:api => (api ? api.split(':')[1]: nil)
|
37
|
+
).
|
38
|
+
yauth(
|
35
39
|
:login => yths[0].strip,
|
36
40
|
:passwd => yths[1].strip
|
37
41
|
)
|
@@ -41,6 +45,9 @@ if arg.size>0
|
|
41
45
|
def oa
|
42
46
|
OA
|
43
47
|
end
|
48
|
+
|
49
|
+
alias_method :http, :oa
|
50
|
+
|
44
51
|
end
|
45
52
|
|
46
53
|
# remove non irb arguments...
|
@@ -52,8 +59,9 @@ if arg.size>0
|
|
52
59
|
require 'irb'
|
53
60
|
puts ''
|
54
61
|
puts "PATH: #{oa.host}#{oa.api}<path>"
|
55
|
-
puts "Your instance object stored in OA|oa"
|
56
|
-
puts "
|
62
|
+
puts "Your instance object stored in OA|oa|http"
|
63
|
+
puts "The response object: http.resp"
|
64
|
+
puts "Ex: http.get '/me.json'"
|
57
65
|
puts ''
|
58
66
|
|
59
67
|
IRB.start
|
@@ -0,0 +1,264 @@
|
|
1
|
+
class Module
|
2
|
+
|
3
|
+
# Creates a class-variable attribute that can
|
4
|
+
# be accessed both on an instance and class level.
|
5
|
+
#
|
6
|
+
# class CARExample
|
7
|
+
# @@a = 10
|
8
|
+
# cattr :a
|
9
|
+
# end
|
10
|
+
#
|
11
|
+
# CARExample.a #=> 10
|
12
|
+
# CARExample.new.a #=> 10
|
13
|
+
#
|
14
|
+
# NOTE: This is not (presently) a common core extension and is not
|
15
|
+
# loaded automatically when using <code>require 'facets'</code>.
|
16
|
+
#
|
17
|
+
# CREDIT: David Heinemeier Hansson
|
18
|
+
def cattr(*syms)
|
19
|
+
writers, readers = syms.flatten.partition{ |a| a.to_s =~ /=$/ }
|
20
|
+
writers = writers.map{ |e| e.to_s.chomp('=').to_sym }
|
21
|
+
##readers.concat( writers ) # writers also get readers
|
22
|
+
|
23
|
+
cattr_reader(*readers)
|
24
|
+
cattr_writer(*writers)
|
25
|
+
|
26
|
+
return readers + writers
|
27
|
+
end
|
28
|
+
|
29
|
+
# Creates a class-variable attr_reader that can
|
30
|
+
# be accessed both on an instance and class level.
|
31
|
+
#
|
32
|
+
# class CARExample
|
33
|
+
# @@a = 10
|
34
|
+
# cattr_reader :a
|
35
|
+
# end
|
36
|
+
#
|
37
|
+
# CARExample.a #=> 10
|
38
|
+
# CARExample.new.a #=> 10
|
39
|
+
#
|
40
|
+
# NOTE: This is not (presently) a common core extension and is not
|
41
|
+
# loaded automatically when using <code>require 'facets'</code>.
|
42
|
+
#
|
43
|
+
# CREDIT: David Heinemeier Hansson
|
44
|
+
def cattr_reader(*syms)
|
45
|
+
syms.flatten.each do |sym|
|
46
|
+
module_eval(<<-EOS, __FILE__, __LINE__)
|
47
|
+
unless defined? @@#{sym}
|
48
|
+
@@#{sym} = nil
|
49
|
+
end
|
50
|
+
|
51
|
+
def self.#{sym}
|
52
|
+
@@#{sym}
|
53
|
+
end
|
54
|
+
|
55
|
+
def #{sym}
|
56
|
+
@@#{sym}
|
57
|
+
end
|
58
|
+
EOS
|
59
|
+
end
|
60
|
+
return syms
|
61
|
+
end
|
62
|
+
|
63
|
+
# Creates a class-variable attr_writer that can
|
64
|
+
# be accessed both on an instance and class level.
|
65
|
+
#
|
66
|
+
# class CAWExample
|
67
|
+
# cattr_writer :a
|
68
|
+
# def self.a
|
69
|
+
# @@a
|
70
|
+
# end
|
71
|
+
# end
|
72
|
+
#
|
73
|
+
# CAWExample.a = 10
|
74
|
+
# CAWExample.a #=> 10
|
75
|
+
# CAWExample.new.a = 29
|
76
|
+
# CAWExample.a #=> 29
|
77
|
+
#
|
78
|
+
# NOTE: This is not (presently) a common core extension and is not
|
79
|
+
# loaded automatically when using <code>require 'facets'</code>.
|
80
|
+
#
|
81
|
+
# CREDIT: David Heinemeier Hansson
|
82
|
+
def cattr_writer(*syms)
|
83
|
+
syms.flatten.each do |sym|
|
84
|
+
module_eval(<<-EOS, __FILE__, __LINE__)
|
85
|
+
unless defined? @@#{sym}
|
86
|
+
@@#{sym} = nil
|
87
|
+
end
|
88
|
+
|
89
|
+
def self.#{sym}=(obj)
|
90
|
+
@@#{sym} = obj
|
91
|
+
end
|
92
|
+
|
93
|
+
def #{sym}=(obj)
|
94
|
+
@@#{sym}=(obj)
|
95
|
+
end
|
96
|
+
EOS
|
97
|
+
end
|
98
|
+
return syms
|
99
|
+
end
|
100
|
+
|
101
|
+
# Creates a class-variable attr_accessor that can
|
102
|
+
# be accessed both on an instance and class level.
|
103
|
+
#
|
104
|
+
# class CAAExample
|
105
|
+
# cattr_accessor :a
|
106
|
+
# end
|
107
|
+
#
|
108
|
+
# CAAExample.a = 10
|
109
|
+
# CAAExample.a #=> 10
|
110
|
+
# mc = CAAExample.new
|
111
|
+
# mc.a #=> 10
|
112
|
+
#
|
113
|
+
# NOTE: This is not (presently) a common core extension and is not
|
114
|
+
# loaded automatically when using <code>require 'facets'</code>.
|
115
|
+
#
|
116
|
+
# CREDIT: David Heinemeier Hansson
|
117
|
+
def cattr_accessor(*syms)
|
118
|
+
cattr_reader(*syms) + cattr_writer(*syms)
|
119
|
+
end
|
120
|
+
|
121
|
+
# Creates a class-variable attribute that can
|
122
|
+
# be accessed both on an instance and class level.
|
123
|
+
#
|
124
|
+
# c = Class.new do
|
125
|
+
# mattr :a
|
126
|
+
# def initialize
|
127
|
+
# @@a = 10
|
128
|
+
# end
|
129
|
+
# end
|
130
|
+
#
|
131
|
+
# c.new.a #=> 10
|
132
|
+
# c.a #=> 10
|
133
|
+
#
|
134
|
+
# NOTE: The #mattr methods may not be as useful for modules as the #cattr
|
135
|
+
# methods are for classes, becuase class-level methods are not "inherited"
|
136
|
+
# across the metaclass for included modules.
|
137
|
+
#
|
138
|
+
# NOTE: This is not (presently) a common core extension and is not
|
139
|
+
# loaded automatically when using <code>require 'facets'</code>.
|
140
|
+
#
|
141
|
+
# CREDIT: David Heinemeier Hansson
|
142
|
+
def mattr(*syms)
|
143
|
+
writers, readers = syms.flatten.partition{ |a| a.to_s =~ /=$/ }
|
144
|
+
writers = writers.collect{ |e| e.to_s.chomp('=').to_sym }
|
145
|
+
##readers.concat( writers ) # writers also get readers
|
146
|
+
|
147
|
+
mattr_writer( *writers )
|
148
|
+
mattr_reader( *readers )
|
149
|
+
|
150
|
+
return readers + writers
|
151
|
+
end
|
152
|
+
|
153
|
+
# Creates a class-variable attr_reader that can
|
154
|
+
# be accessed both on an instance and class level.
|
155
|
+
#
|
156
|
+
# c = Class.new do
|
157
|
+
# @@a = 10
|
158
|
+
# mattr_reader :a
|
159
|
+
# end
|
160
|
+
#
|
161
|
+
# c.a #=> 10
|
162
|
+
# c.new.a #=> 10
|
163
|
+
#
|
164
|
+
# NOTE: This is not (presently) a common core extension and is not
|
165
|
+
# loaded automatically when using <code>require 'facets'</code>.
|
166
|
+
#
|
167
|
+
# CREDIT: David Heinemeier Hansson
|
168
|
+
def mattr_reader( *syms )
|
169
|
+
syms.flatten.each do |sym|
|
170
|
+
module_eval(<<-EOS, __FILE__, __LINE__)
|
171
|
+
unless defined? @@#{sym}
|
172
|
+
@@#{sym} = nil
|
173
|
+
end
|
174
|
+
|
175
|
+
def self.#{sym}
|
176
|
+
@@#{sym}
|
177
|
+
end
|
178
|
+
|
179
|
+
def #{sym}
|
180
|
+
@@#{sym}
|
181
|
+
end
|
182
|
+
EOS
|
183
|
+
end
|
184
|
+
return syms
|
185
|
+
end
|
186
|
+
|
187
|
+
# Creates a class-variable attr_writer that can
|
188
|
+
# be accessed both on an instance and class level.
|
189
|
+
#
|
190
|
+
# c = Class.new do
|
191
|
+
# mattr_writer :a
|
192
|
+
# def self.a
|
193
|
+
# @@a
|
194
|
+
# end
|
195
|
+
# end
|
196
|
+
#
|
197
|
+
# c.a = 10
|
198
|
+
# c.a #=> 10
|
199
|
+
#
|
200
|
+
# c.new.a = 29
|
201
|
+
# c.a #=> 29
|
202
|
+
#
|
203
|
+
# NOTE: This is not (presently) a common core extension and is not
|
204
|
+
# loaded automatically when using <code>require 'facets'</code>.
|
205
|
+
#
|
206
|
+
# CREDIT: David Heinemeier Hansson
|
207
|
+
def mattr_writer(*syms)
|
208
|
+
syms.flatten.each do |sym|
|
209
|
+
module_eval(<<-EOS, __FILE__, __LINE__)
|
210
|
+
unless defined? @@#{sym}
|
211
|
+
@@#{sym} = nil
|
212
|
+
end
|
213
|
+
|
214
|
+
def self.#{sym}=(obj)
|
215
|
+
@@#{sym} = obj
|
216
|
+
end
|
217
|
+
|
218
|
+
def #{sym}=(obj)
|
219
|
+
@@#{sym}=(obj)
|
220
|
+
end
|
221
|
+
EOS
|
222
|
+
end
|
223
|
+
return syms
|
224
|
+
end
|
225
|
+
|
226
|
+
# Creates a class-variable attr_accessor that can
|
227
|
+
# be accessed both on an instance and class level.
|
228
|
+
#
|
229
|
+
# c = Class.new do
|
230
|
+
# mattr_accessor :a
|
231
|
+
# end
|
232
|
+
#
|
233
|
+
# c.a = 10
|
234
|
+
# c.a #=> 10
|
235
|
+
#
|
236
|
+
# x = c.new
|
237
|
+
# x.a #=> 10
|
238
|
+
#
|
239
|
+
# NOTE: This is not (presently) a common core extension and is not
|
240
|
+
# loaded automatically when using <code>require 'facets'</code>.
|
241
|
+
#
|
242
|
+
# CREDIT: David Heinemeier Hansson
|
243
|
+
def mattr_accessor(*syms)
|
244
|
+
mattr_reader(*syms) + mattr_writer(*syms)
|
245
|
+
end
|
246
|
+
|
247
|
+
end
|
248
|
+
|
249
|
+
# Consider the issue where a module's metaclass is not inherited.
|
250
|
+
#
|
251
|
+
# m = Module.new do
|
252
|
+
# @@a = 10
|
253
|
+
# mattr_reader :a
|
254
|
+
# end
|
255
|
+
#
|
256
|
+
# c = Class.new do
|
257
|
+
# include m
|
258
|
+
# end
|
259
|
+
#
|
260
|
+
# expect NoMethodError do
|
261
|
+
# c.a
|
262
|
+
# end
|
263
|
+
#
|
264
|
+
# Ideally this would work.
|
@@ -0,0 +1,44 @@
|
|
1
|
+
require 'ostruct'
|
2
|
+
require 'yaml'
|
3
|
+
|
4
|
+
class OpenStruct
|
5
|
+
def _to_hash
|
6
|
+
h = @table
|
7
|
+
#handles nested structures
|
8
|
+
h.each do |k,v|
|
9
|
+
if v.class == OpenStruct
|
10
|
+
h[k] = v._to_hash
|
11
|
+
end
|
12
|
+
end
|
13
|
+
return h
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
class OAuthTest
|
18
|
+
attr_reader :config
|
19
|
+
|
20
|
+
protected
|
21
|
+
|
22
|
+
def config_load(ks)
|
23
|
+
fl = File.expand_path("~/.oa_test")
|
24
|
+
@config = File.exist?(fl) ? YAML.load_file(fl) : OpenStruct.new
|
25
|
+
|
26
|
+
#@config = OpenStruct.new(ym)
|
27
|
+
puts 'Read config: ~/.oa_test'
|
28
|
+
|
29
|
+
@config.key = ks[:key].strip if ks[:key]
|
30
|
+
@config.secret = ks[:secret].strip if ks[:secret]
|
31
|
+
|
32
|
+
@config.host = 'http://www.koprol.com' if !@config.host
|
33
|
+
@config.api = '/api/v2' if !@config.api
|
34
|
+
@config.host = ks[:host] if ks[:host]
|
35
|
+
@config.api = ks[:api] if ks[:api]
|
36
|
+
|
37
|
+
@config.return = ks[:return] if ks[:return]
|
38
|
+
|
39
|
+
if ks[:key] || ks[:secret] || ks[:return] || ks[:host] || ks[:api]
|
40
|
+
File.open(File.expand_path("~/.oa_test"),'w') { |f| f.puts @config.to_yaml }
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
data/lib/oa_test/oa_test.rb
CHANGED
@@ -3,78 +3,80 @@ require 'OAuth'
|
|
3
3
|
require 'HTTParty'
|
4
4
|
|
5
5
|
class OAuthTest
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
6
|
+
attr_accessor :host,:api
|
7
|
+
attr_reader :resp
|
8
|
+
|
9
|
+
def initialize(ks)
|
10
|
+
config_load(ks)
|
11
|
+
@key = config.key
|
12
|
+
@secret = config.secret
|
13
|
+
@return = config.return
|
14
|
+
@host = config.host
|
15
|
+
@api = config.api
|
16
|
+
@typ = {
|
15
17
|
:json => {"Accept" => "application/json", "Content-Type" => "application/json"},
|
16
18
|
:xml => {"Accept" => "application/xml", "Content-Type" => "application/xml"},
|
17
|
-
|
18
|
-
|
19
|
+
}
|
20
|
+
end
|
21
|
+
|
22
|
+
def yauth(lp)
|
23
|
+
get_part(lp).get_token()
|
24
|
+
end
|
25
|
+
|
26
|
+
def get(path)
|
27
|
+
rtn @access_token.get(@host+@api+path,typ(path))
|
28
|
+
end
|
19
29
|
|
20
|
-
|
21
|
-
|
22
|
-
|
30
|
+
def post(path,params)
|
31
|
+
rtn @access_token.post(@host+@api+path,params.to_json,typ(path))
|
32
|
+
end
|
23
33
|
|
24
|
-
|
25
|
-
|
26
|
-
|
34
|
+
def put(path,params)
|
35
|
+
rtn @access_token.put(@host+@api+path,params.to_json,typ(path))
|
36
|
+
end
|
27
37
|
|
28
|
-
|
29
|
-
|
30
|
-
|
38
|
+
def delete(path)
|
39
|
+
rtn @access_token.delete(@host+@api+path,typ(path))
|
40
|
+
end
|
31
41
|
|
32
|
-
|
33
|
-
rtn @access_token.put(@host+@api+path,params.to_json,typ(path))
|
34
|
-
end
|
42
|
+
protected
|
35
43
|
|
36
|
-
|
37
|
-
|
38
|
-
|
44
|
+
def get_part(lp)
|
45
|
+
url = "https://login.yahoo.com/WSLogin/V1/get_auth_token"
|
46
|
+
content = {:query=>{:oauth_consumer_key=>@key, :passwd=>lp[:passwd], :login=>lp[:login]}}
|
47
|
+
@part = HTTParty.get(url, content).parsed_response.split('=')[1].gsub("\n","")
|
48
|
+
self
|
49
|
+
end
|
39
50
|
|
40
|
-
|
51
|
+
def get_token
|
52
|
+
consumer = OAuth::Consumer.new(@key,@secret, { :site => "https://api.login.yahoo.com", :access_token_path => "/oauth/v2/get_token" })
|
53
|
+
req_token = OAuth::RequestToken.new(consumer, @part)
|
54
|
+
token_hash = consumer.token_request(:post, "/oauth/v2/get_token", req_token)
|
55
|
+
@access_token= OAuth::AccessToken.new(consumer,token_hash["oauth_token"],token_hash["oauth_token_secret"])
|
56
|
+
self
|
57
|
+
end
|
41
58
|
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
end
|
59
|
+
def typ(path)
|
60
|
+
@ext = path[/.(json|xml)($|\?)/,1]
|
61
|
+
@ext = 'json' if !@ext
|
62
|
+
@typ[@ext.to_sym]
|
63
|
+
end
|
48
64
|
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
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
|
73
|
-
elsif @return==:boolean
|
74
|
-
resp.class==Net::HTTPOK
|
75
|
-
else
|
76
|
-
resp
|
77
|
-
end
|
78
|
-
end
|
79
|
-
|
65
|
+
def rtn(resp)
|
66
|
+
@resp = resp
|
67
|
+
puts resp.class
|
68
|
+
if @return==nil || resp.class!=Net::HTTPOK || @return==:body
|
69
|
+
resp.body
|
70
|
+
elsif @return==:body_hash && resp.class==Net::HTTPOK
|
71
|
+
if @ext=='json'
|
72
|
+
Hash.from_json(resp.body)
|
73
|
+
elsif @ext=='xml'
|
74
|
+
Hash.from_xml(resp.body)
|
75
|
+
end
|
76
|
+
elsif @return==:boolean
|
77
|
+
resp.class==Net::HTTPOK
|
78
|
+
else
|
79
|
+
resp
|
80
|
+
end
|
81
|
+
end
|
80
82
|
end
|
data/lib/oa_test.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: 21
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 0
|
9
|
-
-
|
10
|
-
version: 0.0.
|
9
|
+
- 5
|
10
|
+
version: 0.0.5
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Watchmen
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2011-
|
18
|
+
date: 2011-09-04 00:00:00 +07:00
|
19
19
|
default_executable: oa_test
|
20
20
|
dependencies:
|
21
21
|
- !ruby/object:Gem::Dependency
|
@@ -92,9 +92,11 @@ extensions: []
|
|
92
92
|
extra_rdoc_files: []
|
93
93
|
|
94
94
|
files:
|
95
|
+
- lib/oa_test/cattr.rb
|
95
96
|
- lib/oa_test/from_jsonxml.rb
|
97
|
+
- lib/oa_test/oa_config.rb
|
96
98
|
- lib/oa_test/oa_test.rb
|
97
|
-
- lib/oa_test/
|
99
|
+
- lib/oa_test/oa_test_version.rb
|
98
100
|
- lib/oa_test.rb
|
99
101
|
- bin/oa_test
|
100
102
|
has_rdoc: true
|
data/lib/oa_test/version.rb
DELETED