Created
July 21, 2014 13:49
-
-
Save ka2n/e44cc1684c0f6468bf3d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# coding: utf-8 | |
# Fetch remember the milk API info | |
require 'pp' | |
require 'nokogiri' | |
require 'open-uri' | |
require 'erb' | |
require 'active_support/dependencies' | |
class String | |
def sorepponize | |
s = gsub(/[a-zA-Z\d]*/) { | |
ss = $& | |
ss[0] = ss[0].to_s.capitalize | |
ss | |
} | |
s.gsub!(/\./, '') | |
s | |
end | |
end | |
def fetch(url) | |
charset = nil | |
html = open(url) do |f| | |
charset = f.charset | |
f.read | |
end | |
Nokogiri::HTML.parse(html, nil, charset) | |
end | |
# Fetch methods page | |
doc = fetch("https://www.rememberthemilk.com/services/api/methods/") | |
mapping = Hash.new | |
omapping = Hash.new | |
cur_service = nil | |
# Iterate methods | |
doc.css('#api h2').reverse.each do |h2| | |
service = h2.text | |
mapping[service] = {} | |
ul = h2.next_element | |
links = ul.css('a') | |
links.each do | link | | |
href = link['href'] | |
meth = link.text | |
# Fetch detail | |
detail = fetch("https://www.rememberthemilk.com" + href) | |
# Retrieve arguments | |
auth_required = detail.text.include?("This method requires authentication") | |
arg_h2 = detail.at('h2:contains("Arguments")') | |
arg_dl = arg_h2.next_element | |
args = Hash.new | |
arg_dl.css('dt code').each do |code| | |
required = code.parent.text.match('Required') | |
args[code.text] = { | |
:required => !!required | |
} | |
end | |
(mapping[service][:methods] ||= []) << { | |
:args => args, | |
:meth => meth, | |
:href => href, | |
:auth_required => auth_required | |
} | |
end | |
end | |
mapping.each { |key, info| | |
# それっぽnize | |
info[:name] = key.sorepponize | |
info[:methods].each { |info| | |
method_name = info[:meth].sub('rtm.'+key, '').sorepponize | |
info[:name] = method_name | |
} | |
} | |
pp mapping |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment