class StackExchange::StackOverflow::Client
Constants
- API_VERSION
- URL
Attributes
api_key[R]
api_version[R]
url[R]
Public Class Methods
config() { |options| ... }
click to toggle source
# File lib/pilha.rb, line 34 def config(&block) options = OpenStruct.new yield options if block_given? @client = Client.new(options) end
instance()
click to toggle source
# File lib/pilha.rb, line 40 def instance @client end
new(options = OpenStruct.new)
click to toggle source
# File lib/pilha.rb, line 46 def initialize(options = OpenStruct.new) @url = normalize(options.url || URL) @api_version = options.api_version || API_VERSION @api_key = options.api_key end
Public Instance Methods
api_method_path(pattern, options = {})
click to toggle source
# File lib/pilha.rb, line 52 def api_method_path(pattern, options = {}) pattern = normalize(pattern) pattern.scan(/:(\w+)/).each do |part| val = part.first pattern.sub!(":" + val, options[val.to_sym].to_s) end pattern end
api_method_url(method, options = {})
click to toggle source
# File lib/pilha.rb, line 63 def api_method_url(method, options = {}) options.merge! :api_key => api_key if api_key root_path + api_method_path(method, options) + query_string(options) end
get(url)
click to toggle source
# File lib/pilha.rb, line 68 def get(url) JSON.parse(Zlib::GzipReader.new(open(url)).read) end
request(path, options)
click to toggle source
# File lib/pilha.rb, line 76 def request(path, options) get api_method_url(path, options) end
root_path()
click to toggle source
# File lib/pilha.rb, line 72 def root_path url + api_version end
Private Instance Methods
normalize(url)
click to toggle source
# File lib/pilha.rb, line 91 def normalize(url) url.end_with?('/') ? url : url + '/' end
query_string(options)
click to toggle source
# File lib/pilha.rb, line 81 def query_string(options) params = options[:query] || options[:conditions] return '' unless params params = params.sort_by { |k, v| k.to_s } pairs = params.map { |k, v| "#{k}=#{v}" } '?' + pairs.join('&') end