grizzly-weibo 0.2.0 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/README.markdown CHANGED
@@ -22,6 +22,24 @@ using omniauth. There is already a working strategy for Weibo.
22
22
  client = Grizzly::Client.new(access_token)
23
23
  ```
24
24
 
25
+ ### Updating Status
26
+ First things first. Lets update the users status. This only works after a user has been logged in and has an access token.
27
+
28
+ ```ruby
29
+ client = Grizzly::Client.new(access_token)
30
+ status = client.status_update("Dude. Weibo is awesome!")
31
+ ```
32
+
33
+ Note that all method calls made with Grizzly will always return a domain object. More often than not this domain object
34
+ is a representation of what ever JSON object has been returned by Weibo's API. In this case we get all sorts of handy
35
+ information on the status we just updated including the user that the status belongs to. So you can do...
36
+
37
+ ```ruby
38
+ status.user.id #=> "1233344545356356"
39
+ status.text #=> "Dude. Weibo is awesome!"
40
+ ```
41
+
42
+
25
43
  ### Friends
26
44
  You can access a list of friends by supplying a weibo user id
27
45
 
data/lib/grizzly.rb CHANGED
@@ -1,6 +1,8 @@
1
+ require 'grizzly/base'
1
2
  require 'grizzly/request'
2
3
  require 'grizzly/client'
3
4
  require 'grizzly/cursor'
5
+ require 'grizzly/status'
4
6
  require 'grizzly/user'
5
7
  require 'grizzly/errors/no_access_token'
6
8
  require 'grizzly/errors/weibo_api'
@@ -0,0 +1,21 @@
1
+ module Grizzly
2
+ class Base
3
+
4
+ def initialize(data)
5
+ @data = 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("No data for: #{method_name}")
13
+ end
14
+ end
15
+
16
+ def to_h
17
+ @data
18
+ end
19
+
20
+ end
21
+ end
@@ -12,5 +12,11 @@ module Grizzly
12
12
  def bilateral_friends(user_id)
13
13
  Grizzly::Cursor.new(Grizzly::User, "/friendships/friends/bilateral", {:access_token => @access_token, :uid => user_id})
14
14
  end
15
+
16
+ def status_update(status)
17
+ raise("Must set a status") unless !status.nil?
18
+ request = Grizzly::Request.new(:post, "/statuses/update", { :access_token => @access_token }, { :status => status } )
19
+ Grizzly::Status.new request.response
20
+ end
15
21
  end
16
22
  end
@@ -1,3 +1,5 @@
1
+ # encoding: UTF-8
2
+
1
3
  require 'faraday'
2
4
  require 'faraday_stack'
3
5
  require 'json'
@@ -11,14 +13,16 @@ module Grizzly
11
13
  TIMEOUT = 5
12
14
  OPEN_TIMEOUT = 2
13
15
 
14
- def initialize(method, url, options)
16
+ def initialize(method, url, options, payload = nil)
15
17
  connection = Faraday.new(:url => BASE_URI) do |builder|
16
18
  builder.use Faraday::Adapter::NetHttp
17
19
  builder.use FaradayStack::ResponseJSON, content_type: 'application/json'
18
20
  builder.use Faraday::Request::UrlEncoded
19
21
  end
20
22
 
21
- @response = connection.send(method, "/#{API_VERISON}#{url}.#{FORMAT}") do |request|
23
+ @response = connection.send(method) do |request|
24
+ request.url "/#{API_VERISON}#{url}.#{FORMAT}"
25
+ request.headers['Content-Type'] = "application/x-www-form-urlencoded"
22
26
  request.options = {
23
27
  :timeout => TIMEOUT,
24
28
  :open_timeout => OPEN_TIMEOUT
@@ -27,6 +31,8 @@ module Grizzly
27
31
  options.each do |key, value|
28
32
  request.params[key] = value
29
33
  end
34
+
35
+ request.body = to_uri_encoded(payload) if payload
30
36
  end
31
37
  end
32
38
 
@@ -40,10 +46,14 @@ module Grizzly
40
46
  def response_to_json
41
47
  payload = JSON.parse(@response.body)
42
48
  if payload.has_key?("error") && payload.has_key?("error_code")
43
- raise Grizzly::Errors::WeiboAPI.new(payload[:error], payload[:error_code])
49
+ raise Grizzly::Errors::WeiboAPI.new(payload["error"], payload["error_code"])
44
50
  end
45
51
  payload
46
52
  end
47
53
 
54
+ def to_uri_encoded(params)
55
+ URI.escape(params.collect{|k,v| "#{k}=#{v}"}.join('&'))
56
+ end
57
+
48
58
  end
49
59
  end
@@ -0,0 +1,10 @@
1
+ module Grizzly
2
+ class Status < Grizzly::Base
3
+
4
+ def initialize(data)
5
+ super(data)
6
+ @data["user"] = Grizzly::User.new(data["user"])
7
+ end
8
+
9
+ end
10
+ end
data/lib/grizzly/user.rb CHANGED
@@ -1,23 +1,9 @@
1
1
  module Grizzly
2
- class User
3
-
2
+ class User < Grizzly::Base
4
3
  API_COLLECTION_NAME = "users"
5
4
 
6
- def initialize(user_data)
7
- @data = user_data
5
+ def initialize(data)
6
+ super(data)
8
7
  end
9
-
10
- def method_missing(method_name, *attrs)
11
- if @data.has_key?(method_name.to_s)
12
- return @data[method_name.to_s]
13
- else
14
- raise("Current User does not have #{method_name}")
15
- end
16
- end
17
-
18
- def to_h
19
- @data
20
- end
21
-
22
8
  end
23
9
  end
@@ -1,3 +1,3 @@
1
1
  module Grizzly
2
- VERSION = "0.2.0"
2
+ VERSION = "0.3.0"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: grizzly-weibo
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-03-27 00:00:00.000000000Z
12
+ date: 2012-04-02 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rspec
@@ -99,11 +99,13 @@ executables: []
99
99
  extensions: []
100
100
  extra_rdoc_files: []
101
101
  files:
102
+ - lib/grizzly/base.rb
102
103
  - lib/grizzly/client.rb
103
104
  - lib/grizzly/cursor.rb
104
105
  - lib/grizzly/errors/no_access_token.rb
105
106
  - lib/grizzly/errors/weibo_api.rb
106
107
  - lib/grizzly/request.rb
108
+ - lib/grizzly/status.rb
107
109
  - lib/grizzly/user.rb
108
110
  - lib/grizzly/version.rb
109
111
  - lib/grizzly.rb
@@ -123,7 +125,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
123
125
  version: '0'
124
126
  segments:
125
127
  - 0
126
- hash: 2776003271979443730
128
+ hash: 891757663377480348
127
129
  required_rubygems_version: !ruby/object:Gem::Requirement
128
130
  none: false
129
131
  requirements: