Class: Rixi
- Inherits:
-
Object
- Object
- Rixi
- Defined in:
- lib/rixi.rb
Defined Under Namespace
Classes: APIError
Constant Summary collapse
- SITE =
'http://api.mixi-platform.com'
- AUTH_URL =
'https://mixi.jp/connect_authorize.pl'
- TOKEN_URL =
'https://secure.mixi-platform.com/2/token'
Instance Attribute Summary collapse
-
#client ⇒ Object
readonly
Returns the value of attribute client.
-
#consumer_key ⇒ Object
readonly
Returns the value of attribute consumer_key.
-
#consumer_secret ⇒ Object
readonly
Returns the value of attribute consumer_secret.
-
#redirect_uri ⇒ Object
readonly
Returns the value of attribute redirect_uri.
-
#token ⇒ Object
readonly
Returns the value of attribute token.
Class Method Summary collapse
-
.api_settings ⇒ Object
手抜き実装のため、長いメソッド名の乱立で非常に汚いです メソッド名は適当なので好きなように変えて使ってください %s の部分が各種メソッドの引数になります.
Instance Method Summary collapse
-
#application_json(time, json) ⇒ Object
build request body.
- #attach_photos(time, imgs) ⇒ Object
- #authorized_uri ⇒ Object
- #delete(path, params = { }) ⇒ Object
- #end_boundary(time) ⇒ Object
-
#extend_expire ⇒ Object
OAuth2::AccessTokenの仕様上破壊的代入が出来ないため…
-
#get(path, params = { }) ⇒ Object
define_methodで定義されたメソッドは最終的に これらのメソッドを呼ぶ.
-
#get_token(code) ⇒ Object
自分自身を返す.
-
#initialize(params = { }) ⇒ Rixi
constructor
A new instance of Rixi.
- #parse_response(response) ⇒ Object
- #post(path, params = { }) ⇒ Object
- #post_image(path, params = { }) ⇒ Object
-
#post_json(path, params = { }) ⇒ Object
paramsはハッシュで渡して関数内でJSON化する.
-
#post_multipart(path, params = { }) ⇒ Object
JSON形式+写真を投稿することが可能なAPIについて.
- #put(path, params = { }) ⇒ Object
-
#scope_to_query(scope) ⇒ Object
スコープ未設定の時はとりあえずプロフィールだけで.
-
#set_token(access_token, refresh_token, expires_in) ⇒ Object
自分自身を返す.
-
#voice(status) ⇒ Object
mixiボイスの投稿を楽にするため.
Constructor Details
#initialize(params = { }) ⇒ Rixi
Returns a new instance of Rixi.
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/rixi.rb', line 21 def initialize(params = { }) if params[:consumer_key] == nil && params[:consumer_secret] == nil raise "Rixi needs a consumer_key or consumer_secret." end @consumer_key = params.delete :consumer_key @consumer_secret = params.delete :consumer_secret @redirect_uri = params.delete :redirect_uri @scope = scope_to_query(params.delete(:scope)) params.merge!({ :site => SITE, :authorize_url => AUTH_URL, :token_url => TOKEN_URL }) @client = OAuth2::Client.new( @consumer_key, @consumer_secret, params ) end |
Instance Attribute Details
#client ⇒ Object (readonly)
Returns the value of attribute client.
15 16 17 |
# File 'lib/rixi.rb', line 15 def client @client end |
#consumer_key ⇒ Object (readonly)
Returns the value of attribute consumer_key.
15 16 17 |
# File 'lib/rixi.rb', line 15 def consumer_key @consumer_key end |
#consumer_secret ⇒ Object (readonly)
Returns the value of attribute consumer_secret.
15 16 17 |
# File 'lib/rixi.rb', line 15 def consumer_secret @consumer_secret end |
#redirect_uri ⇒ Object (readonly)
Returns the value of attribute redirect_uri.
15 16 17 |
# File 'lib/rixi.rb', line 15 def redirect_uri @redirect_uri end |
#token ⇒ Object (readonly)
Returns the value of attribute token.
15 16 17 |
# File 'lib/rixi.rb', line 15 def token @token end |
Class Method Details
.api_settings ⇒ Object
手抜き実装のため、長いメソッド名の乱立で非常に汚いです メソッド名は適当なので好きなように変えて使ってください %s の部分が各種メソッドの引数になります
次期バージョンが出来るとするならAPIの種類毎に モジュールに切り分けて実装したいです。
注:%o は省略可能なpathを表現するために使ってます 例えば、友人のつぶやき一覧の取得をするAPIは以下で、 /2/voice/statuses/friends_timeline/?since_id= Group-IDはpathにも含まれますが省略可能です そのような場合は、最後の引数のハッシュで:optional_pathのキーで 指定するとして、:optional_pathが存在しなければAPIのパスから 省略することとします。
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 |
# File 'lib/rixi.rb', line 91 def self.api_settings # method name, path for API endpoints, http method "people /2/people/%s/%s get search_people /2/search/people/%s get updates /2/updates/%s/%s get user_timeline /2/voice/statuses/%s/user_timeline get friends_timeline /2/voice/statuses/friends_timeline/%o get show_status /2/voice/statuses/show/%s get show_favorites /2/voice/favorites/show/%s get update_status /2/voice/statuses/update post delete_status /2/voice/statuses/destroy/%s post create_replies /2/voice/statuses/replies/create/%s post delete_replies /2/voice/replies/destroy/%s/%s post create_favorite /2/voice/favorites/create/%s post delete_favorite /2/voice/favorites/destory/%s/%s post share /2/share post_json albums /2/photo/albums/%s/@self/%o get recent_album /2/photo/albums/%s/%s get photos_in_album /2/photo/mediaItems/%s/@self/%s/%o get recent_photos /2/photo/mediaItems/%s/%o get comments_album /2/photo/comments/albums/%s/@self/%s get comments_photo /2/photo/comments/mediaItems/%s/@self/%s/%s get favorites_photo /2/phoho/favorites/mediaItems/%s/@self/%s/%s get create_album /2/photo/albums/%s/@self post_json delete_album /2/photo/albums/%s/@self/%s delete create_comment_album /2/photo/comments/albums/%s/@self/%s post_json delete_comment_album /2/photo/comments/albums/%s/@self/%s/%s delete upload_photo /2/photo/mediaItems/%s/@self/%s post_image delete_photo /2/photo/mediaItems/%s/@self/%s/%s delete create_comment_photo /2/photo/comments/mediaItems/%s/@self/%s/%s/ post_json delete_comment_photo /2/photo/comments/mediaItems/%s/@self/%s/%s/ delete create_favorite_photo /2/photo/favorites/mediaItems/%s/@self/%s/%s/ post delete_favorite_photo /2/photo/favorites/mediaItems/%s/@self/%s/%s/ delete spot /2/spots/%s get search_spot /2/search/spots get spots_list /2/spots/%s/@self get create_myspot /2/spots/%s/@self post delete_myspot /2/spots/%s/@self delete get_checkins /2/checkins/%s/%s get get_checkin /2/checkins/%s/@self/%s get checkin /2/checkins/%s post_multipart checkin_with_photo /2/checkins/%s post_multipart diary /2/diary/articles/@me/@self post_multipart messages_inbox /2/messages/%s/@inbox/%o get messages_outbox /2/messages/%s/@outbox/%o get create_message /2/messages/%s/@self/@outbox post read_message /2/messages/%s/@self/@inbox/%s put delete_inbox /2/messages/%s/@self/@inbox/%s put delete_outbox /2/messages/%s/@self/@outbox/%s delete people_images /2/people/images/%s/@self/%o get create_people_image /2/people/images/%s/@self post set_people_image /2/people/images/%s/@self/%s put delete_people_image /2/people/images/%s/@self/%s delete ".strip.split("\n").map {|l| l.strip.split(/\s+/)} end |
Instance Method Details
#application_json(time, json) ⇒ Object
build request body
263 264 265 266 267 268 269 270 271 |
# File 'lib/rixi.rb', line 263 def application_json(time,json) return <<-"EOF".force_encoding("UTF-8") --boundary#{time}\r Content-Disposition: form-data; name="request"\r Content-Type: application/json\r \r #{json.to_json}\r EOF end |
#attach_photos(time, imgs) ⇒ Object
273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 |
# File 'lib/rixi.rb', line 273 def attach_photos(time, imgs) if imgs.instance_of?(Array) count = 1 else count = "" imgs = [imgs] end attach = "" imgs.each do |img| tmp = <<"IMAGE".force_encoding("UTF-8") --boundary#{time}\r Content-Disposition: form-data; name="photo#{count}"; filename="#{time+count.to_s}.jpg"\r Content-Type: image/jpeg\r \r #{img}\r IMAGE count+=1 if count != "" attach << tmp end attach end |
#authorized_uri ⇒ Object
54 55 56 |
# File 'lib/rixi.rb', line 54 def @client.auth_code.(:scope => @scope) end |
#delete(path, params = { }) ⇒ Object
230 231 232 |
# File 'lib/rixi.rb', line 230 def delete(path, params = { }) @token.delete(path, :params => params).response.env[:status].to_s end |
#end_boundary(time) ⇒ Object
296 297 298 |
# File 'lib/rixi.rb', line 296 def end_boundary(time) "--boundary#{time}--" end |
#extend_expire ⇒ Object
OAuth2::AccessTokenの仕様上破壊的代入が出来ないため…
239 240 241 242 243 |
# File 'lib/rixi.rb', line 239 def extend_expire if @token.expired? @token = @token.refresh! end end |
#get(path, params = { }) ⇒ Object
define_methodで定義されたメソッドは最終的に これらのメソッドを呼ぶ
181 182 183 |
# File 'lib/rixi.rb', line 181 def get(path, params = { }) parse_response(@token.get(path, :params => params)) end |
#get_token(code) ⇒ Object
自分自身を返す
59 60 61 62 |
# File 'lib/rixi.rb', line 59 def get_token(code) @token = @client.auth_code.get_token(code, {:redirect_uri => @redirect_uri}, {:mode => :header, :header_format => "OAuth %s"}) return self end |
#parse_response(response) ⇒ Object
251 252 253 254 255 256 257 258 259 260 |
# File 'lib/rixi.rb', line 251 def parse_response(response) res = response.response.env case res[:status].to_i when 400...600 puts "API ERROR: status_code=" + res[:status].to_s JSON.parse(res[:body]) else JSON.parse(res[:body]) end end |
#post(path, params = { }) ⇒ Object
185 186 187 |
# File 'lib/rixi.rb', line 185 def post(path, params = { }) parse_response(@token.post(path,:params => params)) end |
#post_image(path, params = { }) ⇒ Object
191 192 193 194 195 196 197 198 |
# File 'lib/rixi.rb', line 191 def post_image(path, params = { }) path += "?title="+ CGI.escape(params[:title]) if params[:title] parse_response(@token.post(path,{ :headers => { :content_type => "image/jpeg", :content_length => params[:image].size.to_s, },:body => params[:image]})) end |
#post_json(path, params = { }) ⇒ Object
paramsはハッシュで渡して関数内でJSON化する
201 202 203 204 205 206 207 |
# File 'lib/rixi.rb', line 201 def post_json(path, params = { }) parse_response(@token.post(path,{ :headers => { :content_type => "application/json; charset=utf-8", :content_length => params[:json].size.to_s },:body => params[:json]})) end |
#post_multipart(path, params = { }) ⇒ Object
JSON形式+写真を投稿することが可能なAPIについて
210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 |
# File 'lib/rixi.rb', line 210 def post_multipart(path, params ={ }) if params[:image] now = Time.now.strftime("%Y%m%d%H%M%S") content_type = "multipart/form-data; boundary=boundary#{now}" body = application_json(now,params[:json]) body << attach_photos(now,params[:image]) body << end_boundary(now) else content_type = "application/json" body = params[:json].to_json end parse_response(@token.post(path,{ :headers => { :content_type => content_type, :content_length => body.size.to_s }, :body => body})) end |
#put(path, params = { }) ⇒ Object
234 235 236 |
# File 'lib/rixi.rb', line 234 def put(path, params = { }) parse_response(@token.put(path, :params => params)) end |
#scope_to_query(scope) ⇒ Object
スコープ未設定の時はとりあえずプロフィールだけで
44 45 46 47 48 49 50 51 52 |
# File 'lib/rixi.rb', line 44 def scope_to_query(scope) if scope.kind_of?(Hash) return scope.map {|key, value| key.to_s if value }.join(" ") else return scope || "r_profile" end end |
#set_token(access_token, refresh_token, expires_in) ⇒ Object
自分自身を返す
65 66 67 68 69 70 71 72 73 |
# File 'lib/rixi.rb', line 65 def set_token(access_token, refresh_token, expires_in) @token = OAuth2::AccessToken.new(@client,access_token, {:refresh_token => refresh_token, :expires_in => expires_in, :expires_at => Time.now.to_i+expires_in, :mode => :header, :header_format => "OAuth %s"}) return self end |
#voice(status) ⇒ Object
mixiボイスの投稿を楽にするため
246 247 248 249 |
# File 'lib/rixi.rb', line 246 def voice(status) parse_response(@token.post("/2/voice/statuses/update", :params => {:status => status.force_encoding("UTF-8")})) end |