Class: StackExchange::StackOverflow::Client

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

Constant Summary collapse

URL =
'http://api.stackoverflow.com/'
API_VERSION =
'1.1'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = OpenStruct.new) ⇒ Client

Returns a new instance of Client.



46
47
48
49
50
# 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

Instance Attribute Details

#api_keyObject (readonly)

Returns the value of attribute api_key.



30
31
32
# File 'lib/pilha.rb', line 30

def api_key
  @api_key
end

#api_versionObject (readonly)

Returns the value of attribute api_version.



29
30
31
# File 'lib/pilha.rb', line 29

def api_version
  @api_version
end

#urlObject (readonly)

Returns the value of attribute url.



28
29
30
# File 'lib/pilha.rb', line 28

def url
  @url
end

Class Method Details

.config {|options| ... } ⇒ Object

Yields:

  • (options)


34
35
36
37
38
# File 'lib/pilha.rb', line 34

def config(&block)
  options = OpenStruct.new
  yield options if block_given?
  @client = Client.new(options)
end

.instanceObject



40
41
42
# File 'lib/pilha.rb', line 40

def instance
  @client 
end

Instance Method Details

#api_method_path(pattern, options = {}) ⇒ Object



52
53
54
55
56
57
58
59
60
61
# 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 = {}) ⇒ Object



63
64
65
66
# 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) ⇒ Object



68
69
70
# File 'lib/pilha.rb', line 68

def get(url)
  JSON.parse(Zlib::GzipReader.new(open(url)).read)
end

#request(path, options) ⇒ Object



76
77
78
# File 'lib/pilha.rb', line 76

def request(path, options)
  get api_method_url(path, options)
end

#root_pathObject



72
73
74
# File 'lib/pilha.rb', line 72

def root_path
  url + api_version
end