Class: WorldBank::Query

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

Direct Known Subclasses

DataQuery, ParamQuery

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, id, model) ⇒ Query

Returns a new instance of Query.



7
8
9
10
11
12
13
14
15
16
# File 'lib/world_bank/query.rb', line 7

def initialize(name, id, model)
  @model = model
  @name = name
  @id = id
  @lang = false
  @raw = false
  @new = true
  @query = {:params => {:format => :json}, :dirs => []}
  @param_dir = []
end

Instance Attribute Details

#pagesObject (readonly)

Returns the value of attribute pages.



5
6
7
# File 'lib/world_bank/query.rb', line 5

def pages
  @pages
end

#totalObject (readonly)

Returns the value of attribute total.



5
6
7
# File 'lib/world_bank/query.rb', line 5

def total
  @total
end

Instance Method Details

#cycleObject



106
107
108
109
110
111
112
# File 'lib/world_bank/query.rb', line 106

def cycle
  @cycle_results = @pages.nil? ? fetch : []
  (@pages - self.page).times do
    @cycle_results += self.next.fetch
  end
  @cycle_results
end

#dates(date_range) ⇒ Object

the date param is expressed as a range 'StartDate:EndDate' Date may be year & month, year & quarter, or just year Date will convert any Date/Time object to an apporpriate year & month



23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/world_bank/query.rb', line 23

def dates(date_range)
  if date_range.is_a? String

    # assume the user knows what she is doing if passed a string
    @query[:params][:date] = date_range
  elsif date_range.is_a? Range
    start = date_range.first
    last = date_range.last
    @query[:params][:date] = "#{start.strftime("%YM%m")}:#{last.strftime("%YM%m")}"
  end
  self
end

#fetchObject



114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/world_bank/query.rb', line 114

def fetch
  if @new
    @query[:dirs].push @name
    @query[:dirs].push @id
    @query[:dirs].unshift @lang if @lang
    @query[:params][:format] ||= :json
    @new = false
  end
  @query[:dirs] = @param_dir + @query[:dirs] unless @param_dir.empty?
  client = WorldBank::Client.new(@query, @raw)
  results = client.get_query
  results = parse results unless @raw
  results
end

#format(format) ⇒ Object



36
37
38
39
# File 'lib/world_bank/query.rb', line 36

def format(format)
  @query[:params][:format] = format
  self
end

#id(id) ⇒ Object



46
47
48
49
# File 'lib/world_bank/query.rb', line 46

def id(id)
  @query[:params][:id] = id
  self
end

#language(lang) ⇒ Object



94
95
96
97
98
99
100
101
102
103
104
# File 'lib/world_bank/query.rb', line 94

def language(lang)
  if lang.to_s.length == 2
    @lang = lang.to_s.downcase
  else
    @lang = [ ['french', 'fr'],
      ['spanish', 'es'],
      ['english', 'en'],
      ['arabic', 'ar']  ].assoc(lang.to_s.downcase)[1]
  end
  self
end

#most_recent_values(num, options = {}) ⇒ Object

Most Recent Values param has two optional params Gap fill will specify how many items to add if there isn't enough data for you query Frequency sets how frequent the data sets are... An example is best to explain this: I want the 5 most recent yearly values between 2000 and 2010, but I need at least three data sets to continue. My query would look like this: .dates('2000:2010').most_recent_values(:gap_fill => 3, :frequency => 'Y')



60
61
62
63
64
65
# File 'lib/world_bank/query.rb', line 60

def most_recent_values(num, options={})
  @query[:params][:gapFill] = options[:gap_fill] if options[:gap_fill]
  @query[:params][:frequency] = options[:frequency] if options[:frequency]
  @query[:params][:MRV] = num
  self
end

#nextObject



85
86
87
88
89
90
91
92
# File 'lib/world_bank/query.rb', line 85

def next
  if @query[:params][:page].nil?
    @query[:params][:page] = 2
  else
    @query[:params][:page] += 1
  end
  self
end

#page(num = false) ⇒ Object



76
77
78
79
80
81
82
83
# File 'lib/world_bank/query.rb', line 76

def page(num=false)
  if num
    @query[:params][:page] = num
    self
  else
    @page || @query[:params][:page] || 1
  end
end

#per_page(num = false) ⇒ Object



67
68
69
70
71
72
73
74
# File 'lib/world_bank/query.rb', line 67

def per_page(num=false)
  if num
    @query[:params][:perPage] = num
    self
  else
    @per_page || @query[:params][:per_page] || 50
  end
end

#rawObject



41
42
43
44
# File 'lib/world_bank/query.rb', line 41

def raw
  @raw = true
  self
end