Class: UN::Data::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/un/data/client.rb

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Client

Returns a new instance of Client.



9
10
11
12
13
14
# File 'lib/un/data/client.rb', line 9

def initialize(options = {})
  @host_adpator    = options[:host_adpator] || UN::Data::HostAdaptor.new
  @response_parser = options[:response_parser] || UN::Data::ResponseParser.new

  configure_user_key(options)
end

Instance Method Details

#configure_user_key(options) ⇒ Object



16
17
18
19
20
# File 'lib/un/data/client.rb', line 16

def configure_user_key(options)
  @user_key = ENV[UNDATA_USER_KEY_NAME]

  @user_key = options[:user_key] || @user_key || raise("User key (#{@user_key}) not specified")
end

#data_setsObject



26
27
28
29
30
31
32
# File 'lib/un/data/client.rb', line 26

def data_sets
  uri = user_uri :path => '/data/index'

  response = @host_adpator.get(uri)

  @response_parser.parse_data_sets response
end

#filtered_query(parameters = {}) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/un/data/client.rb', line 42

def filtered_query(parameters = {})
  path = '/data/query/'
  path << URI.escape(parameters[:name], Regexp.new('[^#{URI::PATTERN::UNRESERVED}]')) || raise('Dataset parameter required for general queries')
  path << '/' << URI.escape(parameters[:year]) unless parameters[:year].nil?
  path << '/' << URI.escape(parameters[:country]) unless parameters[:country].nil?

  uri = user_uri :path => path

  response = @host_adpator.get(uri)

  @response_parser.parse_filter_query response
end

#general_query(name) ⇒ Object



34
35
36
37
38
39
40
# File 'lib/un/data/client.rb', line 34

def general_query(name)
  uri = user_uri :path => "/data/query/#{URI.escape(name, Regexp.new('[^#{URI::PATTERN::UNRESERVED}]'))}"

  response = @host_adpator.get(uri)

  @response_parser.parse_query_results response
end

#user_keyObject



22
23
24
# File 'lib/un/data/client.rb', line 22

def user_key
  @user_key
end

#user_uri(options = {}) ⇒ Object



55
56
57
# File 'lib/un/data/client.rb', line 55

def user_uri(options = {})
  URI::HTTP.build :host => HOST_NAME, :path => options[:path], :query => "user_key=#{@user_key}"
end