Skip to content

Commit 079287b

Browse files
committed
Initial commit
0 parents  commit 079287b

File tree

89 files changed

+4723
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

89 files changed

+4723
-0
lines changed

.gitignore

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
*.gem
2+
*.rbc
3+
.bundle
4+
.config
5+
.yardoc
6+
Gemfile.lock
7+
InstalledFiles
8+
_yardoc
9+
coverage
10+
doc/
11+
lib/bundler/man
12+
pkg
13+
rdoc
14+
spec/reports
15+
test/tmp
16+
test/version_tmp
17+
tmp

.rake_tasks

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
build
2+
install
3+
release
4+
spec

Gemfile

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
source 'https://rubygems.org'
2+
3+
# Specify your gem's dependencies in ruby-stackoverflow.gemspec
4+
gemspec
5+
6+
group :test do
7+
gem 'rspec'
8+
end
9+

LICENSE.txt

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
Copyright (c) 2013 Rashmi Yadav
2+
3+
MIT License
4+
5+
Permission is hereby granted, free of charge, to any person obtaining
6+
a copy of this software and associated documentation files (the
7+
"Software"), to deal in the Software without restriction, including
8+
without limitation the rights to use, copy, modify, merge, publish,
9+
distribute, sublicense, and/or sell copies of the Software, and to
10+
permit persons to whom the Software is furnished to do so, subject to
11+
the following conditions:
12+
13+
The above copyright notice and this permission notice shall be
14+
included in all copies or substantial portions of the Software.
15+
16+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

README.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# Ruby::Stackoverflow
2+
3+
Ruby toolkit to Stackoverflow api
4+
5+
## Install
6+
Add this line to your application's Gemfile:
7+
8+
gem 'ruby-stackoverflow'
9+
10+
And then execute:
11+
12+
$ bundle
13+
14+
Or install it yourself as:
15+
16+
$ gem install ruby-stackoverflow
17+
##Configuration
18+
You can add your key and access-token for higher rate limit.
19+
To get Key and Access Token you have to register your app to http://stackapps.com/
20+
Run command in your application dir.
21+
22+
$ rails generate ruby-stackoverflow --client_key=<key> --access_token=<access token>
23+
This command will create a ruby_stackoverflow.rb file in config/initializers.
24+
25+
## Contributing
26+
27+
1. Fork it
28+
2. Create your feature branch (`git checkout -b my-new-feature`)
29+
3. Commit your changes (`git commit -am 'Add some feature'`)
30+
4. Push to the branch (`git push origin my-new-feature`)
31+
5. Create new Pull Request

Rakefile

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
require "bundler/gem_tasks"
2+
require 'rspec/core/rake_task'
3+
4+
RSpec::Core::RakeTask.new(:spec)
5+
6+
task :default => :spec
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
require 'rails/generators'
2+
3+
class RubyStackoverflowGenerator < Rails::Generators::Base
4+
desc "Creates the ruby-stackoverflow initializer file at config/initialize/ruby-stackoverflow.rb"
5+
class_option :client_key, :aliases => "-k", :type => :string, :desc => "Your Stackoverflow Client key"
6+
class_option :access_token, :aliases => "-k", :type => :string, :desc => "Your STackoverflow access token"
7+
8+
def self.source_root
9+
@_ruby_stackoverflow_root ||= File.expand_path("../templates", __FILE__)
10+
end
11+
12+
def install
13+
template 'ruby-stackoverflow.rb', 'config/initializers/ruby-stackoverflow.rb'
14+
end
15+
16+
private
17+
18+
def configuration_output
19+
output = <<-eos
20+
RubyStackoverflow.configure do|config|
21+
config.client_key = '#{options[:client_key]}'
22+
config.access_token = '#{options[:access_token]}'
23+
end
24+
eos
25+
output
26+
end
27+
28+
29+
end
30+
31+
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<%= configuration_output %>

lib/ruby-stackoverflow.rb

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
require 'httparty'
2+
require 'ruby-stackoverflow/version'
3+
require 'ruby-stackoverflow/client'
4+
require 'ruby-stackoverflow/configuration'
5+
6+
module RubyStackoverflow
7+
include HTTParty
8+
9+
class << self
10+
def client
11+
@client = RubyStackoverflow::Client.new unless defined?(@client)
12+
@client
13+
end
14+
15+
# @private
16+
def respond_to_missing?(method_name, include_private=false); client.respond_to?(method_name, include_private); end if RUBY_VERSION >= "1.9"
17+
# @private
18+
def respond_to?(method_name, include_private=false); client.respond_to?(method_name, include_private) || super; end if RUBY_VERSION < "1.9"
19+
20+
private
21+
22+
def method_missing(method_name, *args, &block)
23+
return super unless client.respond_to?(method_name)
24+
client.send(method_name, *args, &block)
25+
end
26+
27+
end
28+
end
29+

lib/ruby-stackoverflow/client.rb

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
require 'json'
2+
require 'ruby-stackoverflow/client/response_data'
3+
require 'ruby-stackoverflow/client/resource/resource'
4+
require 'ruby-stackoverflow/client/resource/user'
5+
require 'ruby-stackoverflow/client/resource/question'
6+
require 'ruby-stackoverflow/client/resource/answer'
7+
require 'ruby-stackoverflow/client/resource/notification'
8+
require 'ruby-stackoverflow/client/resource/badge'
9+
require 'ruby-stackoverflow/client/resource/reputation'
10+
require 'ruby-stackoverflow/client/resource/suggested_edit'
11+
require 'ruby-stackoverflow/client/resource/comment'
12+
require 'ruby-stackoverflow/client/resource/tag'
13+
require 'ruby-stackoverflow/client/resource/post'
14+
require 'ruby-stackoverflow/client/resource/permission'
15+
require 'ruby-stackoverflow/client/resource/stackoverflow_error'
16+
require 'ruby-stackoverflow/client/user_helper'
17+
require 'ruby-stackoverflow/client/question_helper'
18+
require 'ruby-stackoverflow/client/badges_helper'
19+
require 'ruby-stackoverflow/client/comments_helper'
20+
require 'ruby-stackoverflow/client/parse_options'
21+
22+
module RubyStackoverflow
23+
class Client
24+
include RubyStackoverflow::Client::ParseOptions
25+
include RubyStackoverflow::Client::UserHelper
26+
include RubyStackoverflow::Client::QuestionHelper
27+
include RubyStackoverflow::Client::BadgesHelper
28+
include RubyStackoverflow::Client::CommentsHelper
29+
30+
attr_accessor :configuration
31+
32+
def getr(url,klass, options={})
33+
request :get, url,klass ,options
34+
end
35+
36+
def configure
37+
yield(configuration)
38+
end
39+
40+
private
41+
42+
def request(method, url, klass, options={})
43+
url = append_params_to_url(url, parse_options(options))
44+
response = HTTParty.send(method,url)
45+
parse_response(response, klass)
46+
end
47+
48+
def parse_response(data, klass)
49+
data = JSON.parse(data.body, symbolize_names: true)
50+
ResponseData.new(data, klass)
51+
end
52+
53+
def append_params_to_url(url, options)
54+
url = Configuration.api_url + url
55+
options.merge!(key_params)
56+
options = options.to_a.map{|k,v|"#{k}=#{v}"}
57+
url+'?'+options.join('&')
58+
end
59+
60+
def key_params
61+
{key: configuration.client_key, site: 'stackoverflow', access_token: configuration.access_token}
62+
end
63+
64+
def configuration
65+
@configuration||= Configuration.new
66+
end
67+
end
68+
end

0 commit comments

Comments
 (0)