Class: StackExchange::StackOverflow::Client
- Inherits:
-
Object
- Object
- StackExchange::StackOverflow::Client
- Defined in:
- lib/pilha.rb
Constant Summary collapse
- URL =
'http://api.stackoverflow.com/'- API_VERSION =
'1.1'
Instance Attribute Summary collapse
-
#api_key ⇒ Object
readonly
Returns the value of attribute api_key.
-
#api_version ⇒ Object
readonly
Returns the value of attribute api_version.
-
#url ⇒ Object
readonly
Returns the value of attribute url.
Class Method Summary collapse
Instance Method Summary collapse
- #api_method_path(pattern, options = {}) ⇒ Object
- #api_method_url(method, options = {}) ⇒ Object
- #get(url) ⇒ Object
-
#initialize(options = OpenStruct.new) ⇒ Client
constructor
A new instance of Client.
- #request(path, options) ⇒ Object
- #root_path ⇒ Object
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( = OpenStruct.new) @url = normalize(.url || URL) @api_version = .api_version || API_VERSION @api_key = .api_key end |
Instance Attribute Details
#api_key ⇒ Object (readonly)
Returns the value of attribute api_key.
30 31 32 |
# File 'lib/pilha.rb', line 30 def api_key @api_key end |
#api_version ⇒ Object (readonly)
Returns the value of attribute api_version.
29 30 31 |
# File 'lib/pilha.rb', line 29 def api_version @api_version end |
#url ⇒ Object (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
34 35 36 37 38 |
# File 'lib/pilha.rb', line 34 def config(&block) = OpenStruct.new yield if block_given? @client = Client.new() end |
.instance ⇒ Object
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, = {}) pattern = normalize(pattern) pattern.scan(/:(\w+)/).each do |part| val = part.first pattern.sub!(":" + val, [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, = {}) .merge! :api_key => api_key if api_key root_path + api_method_path(method, ) + query_string() 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, ) get api_method_url(path, ) end |
#root_path ⇒ Object
72 73 74 |
# File 'lib/pilha.rb', line 72 def root_path url + api_version end |