school_friend 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,19 @@
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
18
+ .rvmrc
19
+ .idea
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in school_friend.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Kostyantyn
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.
data/README.md ADDED
@@ -0,0 +1,29 @@
1
+ # SchoolFriend
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'school_friend'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install school_friend
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,39 @@
1
+ module SchoolFriend
2
+ module APIMethods
3
+ class << self
4
+ def included(base)
5
+ base.extend(ClassMethods)
6
+ base.send(:attr_reader, :session)
7
+ end
8
+ end
9
+
10
+ module ClassMethods
11
+ def api_method(*methods)
12
+ options = methods.last.is_a?(Hash) ? methods.pop : {}
13
+
14
+ namespace = name.sub(/^.+::/, '')
15
+ namespace[0] = namespace[0].downcase
16
+
17
+ methods.each do |method|
18
+ call = "session.api_call('#{namespace}.#{method.to_s.gsub(/_([a-z])/) { $1.upcase }}', params"
19
+ call += options[:session_only] ? ', true)' : ')'
20
+
21
+ class_eval <<-OES, __FILE__, __LINE__ + 1
22
+ def #{method}(params = {}) # def get_widgets(params = {})
23
+ response = #{call} # response = session.api_call('widget.getWidgets', params, true)
24
+ if response.is_a?(Net::HTTPSuccess) # if response.is_a?(Net::HTTPSuccess)
25
+ JSON(response.body) # JSON(response.body)
26
+ else # else
27
+ response.error! # response.error!
28
+ end # end
29
+ end # end
30
+ OES
31
+ end
32
+ end
33
+ end
34
+
35
+ def initialize(session)
36
+ @session = session
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,12 @@
1
+ module SchoolFriend
2
+ module REST
3
+ class Auth
4
+ include APIMethods
5
+
6
+ api_method :login
7
+ api_method :login_by_token
8
+ api_method :expire_session, session_only: true
9
+ api_method :touch_session, session_only: true
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,15 @@
1
+ module SchoolFriend
2
+ module REST
3
+ class Callbacks
4
+
5
+ attr_reader :session
6
+
7
+ def initialize(session)
8
+ @session = session
9
+ end
10
+
11
+ def payment(params = {})
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,14 @@
1
+ module SchoolFriend
2
+ module REST
3
+ class Discussions
4
+ include APIMethods
5
+
6
+ api_method :get_discussions, session_only: true
7
+ api_method :get_discussion_comments_count, session_only: true
8
+ api_method :add_discussion_comment, session_only: true
9
+ api_method :delete_discussion_comment, session_only: true
10
+ api_method :get_discussion_comments, session_only: true
11
+ api_method :mark_discussion_as_read, session_only: true
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,10 @@
1
+ module SchoolFriend
2
+ module REST
3
+ class Events
4
+ include APIMethods
5
+
6
+ api_method :get, session_only: true
7
+ api_method :get_type_info
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,14 @@
1
+ module SchoolFriend
2
+ module REST
3
+ class Friends
4
+ include APIMethods
5
+
6
+ api_method :get
7
+ api_method :get_mutual_friends
8
+ api_method :get_online
9
+ api_method :get_app_users, session_only: true
10
+ api_method :get_birthdays
11
+ api_method :are_friends, session_only: true
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,15 @@
1
+ module SchoolFriend
2
+ module REST
3
+ class Messages
4
+ include APIMethods
5
+
6
+ api_method :get_conversations, session_only: true
7
+ api_method :get_list, session_only: true
8
+ api_method :get, session_only: true
9
+ api_method :send
10
+ api_method :mark_as_read, session_only: true
11
+ api_method :mark_as_spam, session_only: true
12
+ api_method :delete, session_only: true
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,10 @@
1
+ module SchoolFriend
2
+ module REST
3
+ class Notifications
4
+ include APIMethods
5
+
6
+ api_method :send_simple
7
+ api_method :send_mass
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,9 @@
1
+ module SchoolFriend
2
+ module REST
3
+ class Payment
4
+ include APIMethods
5
+
6
+ api_method :get_user_account_balance, session_only: true
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,18 @@
1
+ module SchoolFriend
2
+ module REST
3
+ class Photos
4
+ include APIMethods
5
+
6
+ api_method :create_album
7
+ api_method :delete_album
8
+ api_method :edit_album
9
+ api_method :get_albums
10
+ api_method :get_photo_info
11
+ api_method :get_photo_marks, session_only: true
12
+ api_method :get_user_album_photos
13
+ api_method :get_user_photos
14
+ api_method :mark_user_photo, session_only: true
15
+ api_method :set_album_main_photo
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,10 @@
1
+ module SchoolFriend
2
+ module REST
3
+ class PhotosV2
4
+ include APIMethods
5
+
6
+ api_method :get_upload_url
7
+ api_method :commit, method: 'POST'
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,9 @@
1
+ module SchoolFriend
2
+ module REST
3
+ class Share
4
+ include APIMethods
5
+
6
+ api_method :add_link, session_only: true
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,9 @@
1
+ module SchoolFriend
2
+ module REST
3
+ class Stream
4
+ include APIMethods
5
+
6
+ api_method :publish
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,20 @@
1
+ module SchoolFriend
2
+ module REST
3
+ class Users
4
+ include APIMethods
5
+
6
+ api_method :set_status
7
+ api_method :get_logged_in_user, session_only: true
8
+ api_method :get_info, session_only: true
9
+ api_method :is_app_user
10
+ api_method :has_app_permission
11
+ api_method :remove_app_permission
12
+ api_method :get_guests, session_only: true
13
+ api_method :get_calls_left
14
+ api_method :get_current_user, session_only: true
15
+ api_method :get_settings
16
+ api_method :set_settings
17
+ api_method :get_mobile_operator
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,10 @@
1
+ module SchoolFriend
2
+ module REST
3
+ class Widget
4
+ include APIMethods
5
+
6
+ api_method :get_widgets
7
+ api_method :get_widget_content
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,114 @@
1
+ require 'net/http'
2
+ require 'digest'
3
+
4
+ module SchoolFriend
5
+ class Session
6
+ class RequireSessionScopeError < ArgumentError
7
+ end
8
+
9
+ attr_reader :options, :session_scope
10
+
11
+ def initialize(options = {})
12
+ @options = options
13
+ @session_scope = options[:session_key] && options[:session_secret_key]
14
+ end
15
+
16
+ # Returns true if API call is performed in session scope
17
+ #
18
+ # @return [TrueClass, FalseClass]
19
+ alias_method :session_scope?, :session_scope
20
+
21
+ # Returns true if API call is performed in application scope
22
+ #
23
+ # @return [TrueClass, FalseClass]
24
+ def application_scope?
25
+ not session_scope?
26
+ end
27
+
28
+ # Returns application key
29
+ #
30
+ # @return [String]
31
+ def application_key
32
+ SchoolFriend.application_key
33
+ end
34
+
35
+ # Returns signature for signing request params
36
+ #
37
+ # @return [String]
38
+ def signature
39
+ @signature ||= session_scope? ? options[:session_secret_key] : SchoolFriend.secret_key
40
+ end
41
+
42
+ # Returns API server
43
+ #
44
+ # @@return [String]
45
+ def api_server
46
+ SchoolFriend.api_server
47
+ end
48
+
49
+ # Signs params
50
+ #
51
+ # @param [Hash] params
52
+ # @return [Hash] returns modified params
53
+ def sign(params = {})
54
+ params = additional_params.merge(params)
55
+ digest = params.sort_by(&:first).map{ |key, value| "#{key}=#{value}" }.join
56
+ params[:sig] = Digest::MD5.hexdigest("#{digest}#{signature}")
57
+ params
58
+ end
59
+
60
+ # Returns additional params which are required for all requests.
61
+ # Depends on request scope.
62
+ #
63
+ # @return [Hash]
64
+ def additional_params
65
+ @additional_params ||= if session_scope?
66
+ {application_key: application_key, session_key: options[:session_key]}
67
+ else
68
+ {application_key: application_key}
69
+ end
70
+ end
71
+
72
+ # Performs API call to Odnoklassniki
73
+ #
74
+ # @example Performs API call in current scope
75
+ # school_friend = SchoolFriend::Session.new
76
+ # school_friend.api_call('widget.getWidgets', wids: 'mobile-header,mobile-footer') # Net::HTTPResponse
77
+ #
78
+ # @example Force performs API call in session scope
79
+ # school_friend = SchoolFriend::Session.new
80
+ # school_friend.api_call('widget.getWidgets', {wids: 'mobile-header,mobile-footer'}, true) # SchoolFriend::Session::RequireSessionScopeError
81
+ #
82
+ #
83
+ # @param [String] method API method
84
+ # @param [Hash] params params which should be sent to portal
85
+ # @param [FalseClass, TrueClass] force_session_call says if this call should be performed in session scope
86
+ # @return [Net::HTTPResponse]
87
+ def api_call(method, params = {}, force_session_call = false)
88
+ raise RequireSessionScopeError.new('This API call requires session scope') if force_session_call and application_scope?
89
+
90
+ uri = build_uri(method, params)
91
+ Net::HTTP.get_response(uri)
92
+ end
93
+
94
+ # Builds URI object
95
+ #
96
+ # @param [String] method request method
97
+ # @param [Hash] params request params
98
+ # @return [URI::HTTP]
99
+ def build_uri(method, params = {})
100
+ uri = URI(api_server)
101
+ uri.path = '/api/' + method.sub('.', '/')
102
+ uri.query = URI.encode_www_form(sign(params))
103
+ uri
104
+ end
105
+
106
+ SchoolFriend::REST_NAMESPACES.each do |namespace|
107
+ class_eval <<-EOS, __FILE__, __LINE__ + 1
108
+ def #{namespace}
109
+ SchoolFriend::REST::#{namespace.capitalize.gsub(/_([a-z])/) { $1.upcase }}.new(self)
110
+ end
111
+ EOS
112
+ end
113
+ end
114
+ end
@@ -0,0 +1,3 @@
1
+ module SchoolFriend
2
+ VERSION = '0.0.1'
3
+ end
@@ -0,0 +1,35 @@
1
+ require 'forwardable'
2
+
3
+ module SchoolFriend
4
+ REST_NAMESPACES = %w[auth callbacks discussions events friends messages notifications payment photos photos_v2 share stream stream users widget].freeze
5
+
6
+ class << self
7
+ extend Forwardable
8
+
9
+ attr_accessor :application_key, :secret_key, :api_server
10
+
11
+ def_delegators :session, *REST_NAMESPACES
12
+
13
+ def session(options = {})
14
+ Session.new(options)
15
+ end
16
+ end
17
+ end
18
+
19
+ require 'school_friend/version'
20
+ require 'school_friend/session'
21
+ require 'school_friend/api_methods'
22
+ require 'school_friend/rest/auth'
23
+ require 'school_friend/rest/callbacks'
24
+ require 'school_friend/rest/discussions'
25
+ require 'school_friend/rest/events'
26
+ require 'school_friend/rest/friends'
27
+ require 'school_friend/rest/messages'
28
+ require 'school_friend/rest/notifications'
29
+ require 'school_friend/rest/payment'
30
+ require 'school_friend/rest/photos'
31
+ require 'school_friend/rest/photos_v2'
32
+ require 'school_friend/rest/share'
33
+ require 'school_friend/rest/stream'
34
+ require 'school_friend/rest/users'
35
+ require 'school_friend/rest/widget'
@@ -0,0 +1,20 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'school_friend/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = 'school_friend'
8
+ gem.version = SchoolFriend::VERSION
9
+ gem.authors = 'Kostyantyn Stepanyuk'
10
+ gem.email = 'kostya.stepanyuk@gmail.com'
11
+ gem.description = 'A Ruby interface to the Odnoklassniki API'
12
+ gem.summary = 'A Ruby interface to the Odnoklassniki API'
13
+ gem.homepage = 'https://github.com/kostyantyn/school_friend'
14
+ gem.required_ruby_version = Gem::Requirement.new('>= 1.9.2')
15
+
16
+ gem.files = `git ls-files`.split($/)
17
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
18
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
19
+ gem.require_paths = %w[lib]
20
+ end
metadata ADDED
@@ -0,0 +1,68 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: school_friend
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Kostyantyn Stepanyuk
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-12-03 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: A Ruby interface to the Odnoklassniki API
15
+ email: kostya.stepanyuk@gmail.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - .gitignore
21
+ - Gemfile
22
+ - LICENSE.txt
23
+ - README.md
24
+ - Rakefile
25
+ - lib/school_friend.rb
26
+ - lib/school_friend/api_methods.rb
27
+ - lib/school_friend/rest/auth.rb
28
+ - lib/school_friend/rest/callbacks.rb
29
+ - lib/school_friend/rest/discussions.rb
30
+ - lib/school_friend/rest/events.rb
31
+ - lib/school_friend/rest/friends.rb
32
+ - lib/school_friend/rest/messages.rb
33
+ - lib/school_friend/rest/notifications.rb
34
+ - lib/school_friend/rest/payment.rb
35
+ - lib/school_friend/rest/photos.rb
36
+ - lib/school_friend/rest/photos_v2.rb
37
+ - lib/school_friend/rest/share.rb
38
+ - lib/school_friend/rest/stream.rb
39
+ - lib/school_friend/rest/users.rb
40
+ - lib/school_friend/rest/widget.rb
41
+ - lib/school_friend/session.rb
42
+ - lib/school_friend/version.rb
43
+ - school_friend.gemspec
44
+ homepage: https://github.com/kostyantyn/school_friend
45
+ licenses: []
46
+ post_install_message:
47
+ rdoc_options: []
48
+ require_paths:
49
+ - lib
50
+ required_ruby_version: !ruby/object:Gem::Requirement
51
+ none: false
52
+ requirements:
53
+ - - ! '>='
54
+ - !ruby/object:Gem::Version
55
+ version: 1.9.2
56
+ required_rubygems_version: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ requirements: []
63
+ rubyforge_project:
64
+ rubygems_version: 1.8.24
65
+ signing_key:
66
+ specification_version: 3
67
+ summary: A Ruby interface to the Odnoklassniki API
68
+ test_files: []