grizzly-weibo 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/LICENSE ADDED
@@ -0,0 +1,7 @@
1
+ Copyright (c) 2012 Stewart Matheson
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4
+
5
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6
+
7
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,39 @@
1
+ Grizzly
2
+ ===================
3
+
4
+ ### About
5
+ Grizzly is a simple API wrapper around the Weibo API V2. The name comes form the awesome Koala gem for facebook.
6
+
7
+ ### Usage
8
+ To start using grizzly you want to create a client object. This client object allows you to interact with weibo api.
9
+ This can be done creating a new object of type ```Grizzly::Client```. You will need an access token
10
+ to do this. Currently grizzly does not support authentication with oauth. If you need to do this then I recommend
11
+ using omniauth. There is already a working strategy for Weibo.
12
+
13
+ ```ruby
14
+ client = Grizzly::Client.new(access_token)
15
+ ```
16
+
17
+ ### Friends
18
+ You can access a list of friends by supplying a weibo user id
19
+
20
+ ```ruby
21
+ friends = client.friends(user_id)
22
+ ```
23
+
24
+ This method will return an array of user friend objects. Data of these objects can be accessed via methods
25
+
26
+ ```ruby
27
+ friends.first.id #=> "1233344545356356"
28
+ friends.first.screen_name #=> "Fred Chang"
29
+ friends.first.gender #=> "m"
30
+ ```
31
+
32
+ ### Friends (Bilateral)
33
+ Returns a list of your bilateral friends on Weibo. This list has users who are a fan of you and your are a fan of.
34
+
35
+ ```ruby
36
+ friends = client.bilateral_friends(user_id)
37
+ ```
38
+
39
+ This method returns the same friend object that ```client.friends``` returns.
@@ -0,0 +1,6 @@
1
+ require 'grizzly/request'
2
+ require 'grizzly/client'
3
+ require 'grizzly/user'
4
+ require 'grizzly/errors/no_access_token'
5
+ require 'grizzly/errors/weibo_api'
6
+
@@ -0,0 +1,33 @@
1
+ module Grizzly
2
+ class Client
3
+ def initialize(access_token = nil)
4
+ raise Grizzly::Errors::NoAccessToken.new unless access_token
5
+ @access_token = access_token
6
+ end
7
+
8
+ def friends(user_id)
9
+ response = perfrom_request(:get, '/friendships/friends', {:access_token => @access_token, :uid => user_id})
10
+ users = []
11
+ response["users"].each do |user|
12
+ users << Grizzly::User.new(user)
13
+ end
14
+ users
15
+ end
16
+
17
+ def bilateral_friends(user_id)
18
+ response = perfrom_request(:get, '/friendships/friends/bilateral', {:access_token => @access_token, :uid => user_id})
19
+ users = []
20
+ response["users"].each do |user|
21
+ users << Grizzly::User.new(user)
22
+ end
23
+ users
24
+ end
25
+
26
+ private
27
+
28
+ def perfrom_request(method, url, options)
29
+ request = Grizzly::Request.new(method, url, options)
30
+ request.response
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,9 @@
1
+ module Grizzly
2
+ module Errors
3
+ class NoAccessToken < StandardError
4
+ def to_s
5
+ "You have not defined an access token. Grizzly needs an oauth access token to access the weibo"
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,16 @@
1
+ module Grizzly
2
+ module Errors
3
+ class WeiboAPI < StandardError
4
+
5
+ attr_accessor :error, :error_code
6
+
7
+ def initialize(error, error_code)
8
+ @error, @error_code = error, error_code
9
+ end
10
+
11
+ def to_s
12
+ "The Weibo API has returned an error: (#{@error_code}) - #{@error}"
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,49 @@
1
+ require 'faraday'
2
+ require 'faraday_stack'
3
+ require 'json'
4
+
5
+ module Grizzly
6
+ class Request
7
+
8
+ API_VERISON = 2
9
+ BASE_URI = "https://api.weibo.com"
10
+ FORMAT = "json"
11
+ TIMEOUT = 5
12
+ OPEN_TIMEOUT = 2
13
+
14
+ def initialize(method, url, options)
15
+ connection = Faraday.new(:url => BASE_URI) do |builder|
16
+ builder.use Faraday::Adapter::NetHttp
17
+ builder.use FaradayStack::ResponseJSON, content_type: 'application/json'
18
+ builder.use Faraday::Request::UrlEncoded
19
+ end
20
+
21
+ @response = connection.send(method, "/#{API_VERISON}#{url}.#{FORMAT}") do |request|
22
+ request.options = {
23
+ :timeout => TIMEOUT,
24
+ :open_timeout => OPEN_TIMEOUT
25
+ }
26
+
27
+ options.each do |key, value|
28
+ request.params[key] = value
29
+ end
30
+ end
31
+ end
32
+
33
+ def response
34
+ #One day we might want to use XML here. God forbid.
35
+ send "response_to_#{FORMAT}"
36
+ end
37
+
38
+ private
39
+
40
+ def response_to_json
41
+ payload = JSON.parse(@response.body)
42
+ if payload.has_key?("error") && payload.has_key?("error_code")
43
+ raise Grizzly::Errors::WeiboAPI.new(payload[:error], payload[:error_code])
44
+ end
45
+ payload
46
+ end
47
+
48
+ end
49
+ end
@@ -0,0 +1,17 @@
1
+ module Grizzly
2
+ class User
3
+
4
+ def initialize(user_data)
5
+ @data = user_data
6
+ end
7
+
8
+ def method_missing(method_name, *attrs)
9
+ if @data.has_key?(method_name.to_s)
10
+ return @data[method_name.to_s]
11
+ else
12
+ raise("Current User does not have #{method_name}")
13
+ end
14
+ end
15
+
16
+ end
17
+ end
@@ -0,0 +1,3 @@
1
+ module Grizzly
2
+ VERSION = "0.1.0"
3
+ end
metadata ADDED
@@ -0,0 +1,74 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: grizzly-weibo
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Stewart Matheson
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-03-26 00:00:00.000000000Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rspec
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ description: Grizzly makes communicating with Weibo API V2 a breeze by wrapping calls
31
+ to its api in a neat ruby gem.
32
+ email:
33
+ - stew@rtmatheosn.com
34
+ executables: []
35
+ extensions: []
36
+ extra_rdoc_files: []
37
+ files:
38
+ - lib/grizzly/client.rb
39
+ - lib/grizzly/errors/no_access_token.rb
40
+ - lib/grizzly/errors/weibo_api.rb
41
+ - lib/grizzly/request.rb
42
+ - lib/grizzly/user.rb
43
+ - lib/grizzly/version.rb
44
+ - lib/grizzly.rb
45
+ - LICENSE
46
+ - README.markdown
47
+ homepage: https://github.com/stewartmatheson/grizzly
48
+ licenses: []
49
+ post_install_message:
50
+ rdoc_options: []
51
+ require_paths:
52
+ - lib
53
+ required_ruby_version: !ruby/object:Gem::Requirement
54
+ none: false
55
+ requirements:
56
+ - - ! '>='
57
+ - !ruby/object:Gem::Version
58
+ version: '0'
59
+ segments:
60
+ - 0
61
+ hash: -2593568912853458534
62
+ required_rubygems_version: !ruby/object:Gem::Requirement
63
+ none: false
64
+ requirements:
65
+ - - ! '>='
66
+ - !ruby/object:Gem::Version
67
+ version: 1.8.19
68
+ requirements: []
69
+ rubyforge_project:
70
+ rubygems_version: 1.8.19
71
+ signing_key:
72
+ specification_version: 3
73
+ summary: An api wrapper for Weibo V2
74
+ test_files: []