Class: Jpnstacapi::Api

Inherits:
Object
  • Object
show all
Defined in:
lib/jpnstacapi.rb

Direct Known Subclasses

List, Meta, RailsCache, Stats

Instance Method Summary collapse

Constructor Details

#initializeApi

Returns a new instance of Api.



5
6
7
8
9
10
# File 'lib/jpnstacapi.rb', line 5

def initialize
  @url = 'http://statdb.nstac.go.jp/api/1.0b/app/'
  @params = {
    :appId => 'afe504de49de927279178f0934af9facf1425bab'
  }
end

Instance Method Details

#getXmlObject



18
19
20
21
22
# File 'lib/jpnstacapi.rb', line 18

def getXml
  require 'rexml/document'
  source = httpRequest
  return REXML::Document.new source
end

#httpRequestObject



24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/jpnstacapi.rb', line 24

def httpRequest
  require 'net/http'
  cache = Jpnstacapi::RailsCache.new
  uri = @url + '?' + URI.encode_www_form(@params)
  if source = cache.is_enable?(uri)
    return source
  elsif source = Net::HTTP.get(URI(uri))
    cache.save uri, source
    return source
  else
    return source
  end
end

#setParameters(params) ⇒ Object



12
13
14
15
16
# File 'lib/jpnstacapi.rb', line 12

def setParameters(params)
  params.each {|k, v|
    @params.store("cd" + k.capitalize, v.join(','))
  }
end