Module: ChiebukuroAPI

Included in:
Chiebukuro::Chiebukuro
Defined in:
lib/chiebukuro/api.rb

Defined Under Namespace

Classes: ChiebukuroAPIException

Constant Summary collapse

API_BASE_URL =
"http://chiebukuro.yahooapis.jp/Chiebukuro/V1"
API_LIST =
[
  :questionSearch,
  :categoryTree,
  :getNewQuestionList,
  :detailSearch,
  :postQuestion,
  :postQuestionPreview,
  :postQueSupplement,
  :postQueSupplementPreview,
  :cancelQuestion,
  :cancelQuestionPreview,
  :selectBestAnswer,
  :postAnswer,
  :postAnswerPreview,
  :cancelAnswer,
  :cancelAnswerPreview,
]

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, params = {}) ⇒ Object



25
26
27
28
29
30
# File 'lib/chiebukuro/api.rb', line 25

def method_missing(name, params = {})
  raise(ChiebukuroAPIException, "API #{name} does not exists.") unless API_LIST.include? name
  params['appid'] = @appid
  xml = open(URI.encode("#{API_BASE_URL}/#{name}#{to_query(params)}")).read
  ActiveSupport::XmlMini.parse(xml)['ResultSet']
end

Instance Method Details

#to_query(hash) ⇒ Object



32
33
34
# File 'lib/chiebukuro/api.rb', line 32

def to_query(hash)
  '?' + hash.inject(''){|q, h| "#{q}#{h[0]}=#{h[1]}&"}
end