-
Notifications
You must be signed in to change notification settings - Fork 98
/
flickraw_rdoc.rb
127 lines (106 loc) · 3.41 KB
/
flickraw_rdoc.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
require "rdoc"
require "rdoc/parser/ruby"
require "nokogiri"
FLICKR_API_URL='https://www.flickr.com/services/api'
FakedToken = Struct.new :text, :kind
module RDoc
class FlickrawParser < Parser::Ruby
parse_files_matching(/flickraw\.rb$/)
def scan
super
fr = @top_level.find_module_named 'FlickRaw'
k = fr.add_class NormalClass, 'Flickr', 'FlickRaw::Request'
k.record_location @top_level
@stats.add_class 'Flickr'
add_flickr_methods(FlickRaw::Flickr, k)
@top_level
end
private
def add_flickr_methods(obj, doc)
flickr = FlickRaw::Flickr.new
obj.constants.each { |const_name|
const = obj.const_get const_name
if const.is_a?(Class) && const < FlickRaw::Request
name = const.name.sub(/.*::/, '')
k = doc.add_class NormalClass, name, 'FlickRaw::Request'
k.record_location @top_level
@stats.add_class name
m = AnyMethod.new nil, name.downcase
m.comment = "Returns a #{name} object."
m.params = ''
m.singleton = false
doc.add_method m
@stats.add_method m
add_flickr_methods(const, k)
end
}
obj.flickr_methods.each { |name|
flickr_method = obj.request_name + '.' + name
info = flickr.reflection.getMethodInfo :method_name => flickr_method
m = AnyMethod.new nil, name
m.comment = flickr_method_comment(info)
m.params = flickr_method_args(info)
m.singleton = false
m.start_collecting_tokens
m.add_token FakedToken.new( %{
# Generated automatically from flickr api
def #{name}(*args)
@flickr.call '#{flickr_method}', *args
end
} )
doc.add_method m
@stats.add_method m
}
end
def flickr_method_comment(info)
description = Nokogiri::HTML(info.method.description.to_s).text
if info.respond_to? :arguments
args = info.arguments.select { |arg| arg.name != 'api_key' }
arguments = "<b>Arguments</b>\n"
if args.size > 0
args.each { |arg|
arguments << "[#{arg.name} "
arguments << "<em>(required)</em> " if arg.optional == '0'
arguments << "] "
arguments << "#{Nokogiri::HTML(arg.to_s).text}\n"
}
end
end
if info.respond_to? :errors
errors = "<b>Error codes</b>\n"
info.errors.each { |e|
errors << "* #{e.code}: <em>#{e.message}</em>\n\n"
errors << " #{Nokogiri::HTML(e.to_s).text}\n"
}
end
if info.method.respond_to? :response
response = "<b>Returns</b>\n"
raw = Nokogiri::HTML(info.method.response.to_s).text
response << raw.lines.collect { |line| line.insert(0, ' ') }.join
else
response = ''
end
str = "{#{info.method.name}}[#{FLICKR_API_URL}/#{info.method.name}.html] request.\n\n"
str << description << "\n\n"
str << arguments << "\n\n"
str << errors << "\n\n"
str << response << "\n\n"
end
def flickr_method_args(info)
str = ''
if info.respond_to? :arguments
args = info.arguments.select { |arg| arg.name != 'api_key' }
if args.size > 0
str << '('
args.each { |arg|
str << ":#{arg.name} => '#{arg.name}'"
str << ','
}
str.chomp! ','
str << ')'
end
end
str
end
end
end