smart_vk_api 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: e42c24b1492eecfa6a9c1f2fb33054b0ae6bbe8f
4
+ data.tar.gz: 821f6c14fcee0bea3f505376563e0b31057817dd
5
+ SHA512:
6
+ metadata.gz: c7e49fe118e49fb980b823f460e7593b1e625aa53a845bdef82afe65b30a2bf3ce3ff3df0d16c7d29e0cd4abdbb570d196c9c49f8795d10992909954d89f55e4
7
+ data.tar.gz: b47a63e748a1083a4bd54b8428e20e3663f11a9020a558b23e3eaf8dac813304ae6d744ea05f12133086f0893073ba5dd2bd68249a13a77a61af1ae92d8446b4
data/.gitignore ADDED
@@ -0,0 +1,15 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
15
+ *.gem
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format documentation
data/.travis.yml ADDED
@@ -0,0 +1,11 @@
1
+ language: ruby
2
+
3
+ rvm:
4
+ - 2.0
5
+ - 2.1
6
+ - 2.2
7
+
8
+ gemfile:
9
+ - Gemfile
10
+
11
+ script: "bundle exec rspec"
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in smart_vk_api.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 Sergey Tsvetkov
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,49 @@
1
+ # SmartVkApi
2
+
3
+ [![Build Status](https://travis-ci.org/kimrgrey/smart_vk_api.svg)](https://travis-ci.org/kimrgrey/smart_vk_api)
4
+ [![Code Climate](https://codeclimate.com/github/kimrgrey/smart_vk_api/badges/gpa.svg)](https://codeclimate.com/github/kimrgrey/smart_vk_api)
5
+
6
+ Библиотека, обеспечивающая по-настоящему удобный и простой интерфейс для работы с API социальной сети "Вконтакте".
7
+
8
+ ## Что еще один?
9
+
10
+ Да, в некотором роде. Но не совсем.
11
+
12
+ 1. Почти все гемы для работы с VK API, которые можно [найти на rubygems.org](https://rubygems.org/search?utf8=%E2%9C%93&query=vkontakte), давно не обновлялись.
13
+ 2. Один из самых [приятно реализованных](https://github.com/7even/vkontakte_api) и более или менее актуальных тянет за собой [количество зависимостей](https://github.com/7even/vkontakte_api/blob/master/vkontakte_api.gemspec#L21-L39), которое ставит в тупик.
14
+
15
+ Поэтому при разработке этого гема используются три простых принципа:
16
+
17
+ 1. Он должен быть максимально удобным в использовании;
18
+ 2. Он должен быть настолько "тонким" по отношению к API, насколько это вообще возможно сделать, не жертвуя первым пунктом;
19
+ 3. Он должен тянуть за собой только **необходимый минимум** зависимостей, в идеале - **ни одной**;
20
+
21
+ ## Как установить?
22
+
23
+ Добавьте в Gemfile вашего приложения:
24
+
25
+ ```ruby
26
+ gem 'smart_vk_api'
27
+ ```
28
+
29
+ После чего запустите:
30
+
31
+ ```
32
+ $ bundle install
33
+ ```
34
+
35
+ Или просто установите гем вручную:
36
+
37
+ ```
38
+ $ gem install smart_vk_api
39
+ ```
40
+
41
+ ## Как помочь в разработке?
42
+
43
+ Все как обычно:
44
+
45
+ 1. Сделайте форк (https://github.com/kimrgrey/smart_vk_api/fork)
46
+ 2. Добавьте ветку (`git checkout -b my-new-feature`)
47
+ 3. Сделайте коммит (`git commit -am 'Add some feature'`)
48
+ 4. Сделайте пуш (`git push origin my-new-feature`)
49
+ 5. Пришлите pull request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
@@ -0,0 +1,13 @@
1
+ require 'net/http'
2
+ require 'json'
3
+
4
+ module SmartVkApi
5
+ autoload :Constants, "smart_vk_api/constants"
6
+ autoload :Call, "smart_vk_api/call"
7
+
8
+ class MethodCallError < StandardError; end;
9
+
10
+ class VK
11
+ include SmartVkApi::Call
12
+ end
13
+ end
@@ -0,0 +1,45 @@
1
+ module SmartVkApi
2
+ module Call
3
+ def call(method_name, *options)
4
+ params = options.last if options && options.last.is_a?(Hash)
5
+ http(method_name, params) do |response|
6
+ raise SmartVkApi::MethodCallError, response.body unless response.is_a?(Net::HTTPSuccess)
7
+ json = response.body
8
+ raise SmartVkApi::MethodCallError, 'Response could not be empty' if json.nil? || json.empty?
9
+ parsed_json = JSON.parse(json, :symbolize_names => true)
10
+ raise SmartVkApi::MethodCallError, json if is_error?(parsed_json)
11
+ raise SmartVkApi::MethodCallError, 'Response should include key named :response' unless parsed_json.has_key?(:response)
12
+ parsed_json[:response]
13
+ end
14
+ end
15
+
16
+ def http(method_name, params = {})
17
+ uri = URI(method_url(method_name, params))
18
+ http = Net::HTTP.new(uri.host,uri.port)
19
+ http.use_ssl = true
20
+ block_given? ? yield(http.get(uri.request_uri, headers)) : http
21
+ end
22
+
23
+ def method_url(method_name, params = {})
24
+ if method_name.nil? || method_name.empty?
25
+ raise ArgumentError, 'Method name could not be empty'
26
+ end
27
+ url = SmartVkApi::Constants::METHOD_CALL_URL + "/#{method_name}"
28
+ url += "?#{URI.encode_www_form(params)}" if params && params.any?
29
+ url
30
+ end
31
+
32
+ protected
33
+
34
+ def is_error?(parsed_json)
35
+ parsed_json.has_key?(:error)
36
+ end
37
+
38
+ def headers
39
+ {
40
+ 'Accept' => 'application/json',
41
+ 'Content-Type' =>'application/json'
42
+ }
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,6 @@
1
+ module SmartVkApi
2
+ module Constants
3
+ API_URL = "https://api.vk.com".freeze
4
+ METHOD_CALL_URL = "#{SmartVkApi::Constants::API_URL}/method".freeze
5
+ end
6
+ end
@@ -0,0 +1,3 @@
1
+ module SmartVkApi
2
+ VERSION = "1.0.0"
3
+ end
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'smart_vk_api/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "smart_vk_api"
8
+ spec.version = SmartVkApi::VERSION
9
+ spec.authors = ["Sergey Tsvetkov"]
10
+ spec.email = ["sergey.a.tsvetkov@gmail.com"]
11
+ spec.summary = %q{Small, thin and fast client for VK API}
12
+ spec.homepage = "https://github.com/kimrgrey/smart_vk_api"
13
+ spec.license = "MIT"
14
+
15
+ spec.files = `git ls-files -z`.split("\x0")
16
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
17
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
18
+ spec.require_paths = ["lib"]
19
+
20
+ spec.add_development_dependency "bundler", "~> 1.7"
21
+ spec.add_development_dependency "rake", "~> 10.0"
22
+ spec.add_development_dependency 'rspec', "~> 3.2"
23
+ spec.add_development_dependency 'webmock', "~> 1.20"
24
+ end
data/spec/call_spec.rb ADDED
@@ -0,0 +1,99 @@
1
+ require 'spec_helper'
2
+
3
+ describe SmartVkApi::VK do
4
+ it { should respond_to(:call) }
5
+ it { should respond_to(:method_url) }
6
+ it { should respond_to(:http) }
7
+
8
+ let (:vk) { SmartVkApi::VK.new }
9
+ let (:default_response_body) { {:response => {:test => 'yes'} } }
10
+
11
+ describe '#method_url' do
12
+ it 'should raise ArgumentError when method name is nil' do
13
+ expect{ vk.method_url(nil) }.to raise_error(ArgumentError, 'Method name could not be empty')
14
+ end
15
+
16
+ it 'should raise ArgumentError when method name is empty string' do
17
+ expect{ vk.method_url('') }.to raise_error(ArgumentError, 'Method name could not be empty')
18
+ end
19
+
20
+ it 'should take care of method name' do
21
+ expect(vk.method_url('scope.method')).to eq('https://api.vk.com/method/scope.method')
22
+ end
23
+
24
+ it 'should take care of parameters' do
25
+ expect(vk.method_url('scope.method', {:aaa => :bbb, :xxx => :yyy})).to eq('https://api.vk.com/method/scope.method?aaa=bbb&xxx=yyy')
26
+ end
27
+ end
28
+
29
+ describe '#http' do
30
+ it 'should return http connection if block was not given' do
31
+ expect( vk.http('scope.method')).to be_a(Net::HTTP)
32
+ end
33
+
34
+ it 'should return result of block call if block was given' do
35
+ stub_request(:get, "https://api.vk.com/method/scope.method").to_return(:status => 200, :body => default_response_body.to_json)
36
+ expect( vk.http('scope.method') { |_| 'result of block' }).to eq('result of block')
37
+ end
38
+
39
+ it 'should pass http connection as parameter into block' do
40
+ stub_request(:get, "https://api.vk.com/method/scope.method").to_return(:status => 200, :body => default_response_body.to_json)
41
+ expect(vk.http('scope.method') { |http| http }).to be_a(Net::HTTPResponse)
42
+ end
43
+
44
+ it 'should setup appropriate headers' do
45
+ headers = {
46
+ 'Content-Type' =>'application/json',
47
+ 'Content-Type' =>'application/json'
48
+ }
49
+ stub_request(:get, "https://api.vk.com/method/scope.method").to_return(:status => 200, :body => default_response_body.to_json)
50
+ vk.call('scope.method')
51
+ expect(WebMock).to have_requested(:get, "https://api.vk.com/method/scope.method").with(:headers => headers)
52
+ end
53
+ end
54
+
55
+ describe '#call' do
56
+ it 'should send request to appropriate url according to method name' do
57
+ stub_request(:get, "https://api.vk.com/method/scope.method").to_return(:status => 200, :body => default_response_body.to_json)
58
+ vk.call('scope.method')
59
+ expect(WebMock).to have_requested(:get, "https://api.vk.com/method/scope.method")
60
+ end
61
+
62
+ it 'should send request to appropriate url according params' do
63
+ stub_request(:get, "https://api.vk.com/method/scope.method?xxx=yyy").to_return(:status => 200, :body => default_response_body.to_json)
64
+ vk.call('scope.method', {:xxx => 'yyy'})
65
+ expect(WebMock).to have_requested(:get, "https://api.vk.com/method/scope.method?xxx=yyy")
66
+ end
67
+
68
+ it 'should return raise MethodCallError if body of response is nil' do
69
+ stub_request(:get, "https://api.vk.com/method/scope.method").to_return(:status => 200, :body => nil)
70
+ expect { vk.call('scope.method') }.to raise_error(SmartVkApi::MethodCallError, 'Response could not be empty')
71
+ end
72
+
73
+ it 'should return raise MethodCallError if body of response is empty string' do
74
+ stub_request(:get, "https://api.vk.com/method/scope.method").to_return(:status => 200, :body => "")
75
+ expect { vk.call('scope.method') }.to raise_error(SmartVkApi::MethodCallError, 'Response could not be empty')
76
+ end
77
+
78
+ it 'should return raise MethodCallError if body of response has no key :response' do
79
+ stub_request(:get, "https://api.vk.com/method/scope.method").to_return(:status => 200, :body => { :bad => :response }.to_json)
80
+ expect { vk.call('scope.method') }.to raise_error(SmartVkApi::MethodCallError, 'Response should include key named :response')
81
+ end
82
+
83
+ it 'should raise MethodCallError error in case of internal server error' do
84
+ stub_request(:get, "https://api.vk.com/method/scope.method").to_return(:status => 500, :body => 'Internal Server Error')
85
+ expect { vk.call('scope.method') }.to raise_error(SmartVkApi::MethodCallError, 'Internal Server Error')
86
+ end
87
+
88
+ it 'should raise MethodCallError in case of method call error' do
89
+ response = { :error => { :error_code => 113, :error_msg => 'Invalid user id' } }
90
+ stub_request(:get, "https://api.vk.com/method/scope.method").to_return(:status => 200, :body => response.to_json)
91
+ expect { vk.call('scope.method') }.to raise_error(SmartVkApi::MethodCallError, response.to_json)
92
+ end
93
+
94
+ it 'should return value of :response key of returned json' do
95
+ stub_request(:get, "https://api.vk.com/method/scope.method").to_return(:status => 200, :body => default_response_body.to_json)
96
+ expect(vk.call('scope.method')).to eq(default_response_body[:response])
97
+ end
98
+ end
99
+ end
@@ -0,0 +1,12 @@
1
+ require 'bundler/setup'
2
+
3
+ Bundler.setup
4
+
5
+ require 'webmock/rspec'
6
+ require 'smart_vk_api'
7
+
8
+ RSpec.configure do |config|
9
+ config.mock_with :rspec do |mocks|
10
+ mocks.verify_doubled_constant_names = true
11
+ end
12
+ end
metadata ADDED
@@ -0,0 +1,116 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: smart_vk_api
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Sergey Tsvetkov
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-02-26 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.7'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.7'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.2'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.2'
55
+ - !ruby/object:Gem::Dependency
56
+ name: webmock
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '1.20'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '1.20'
69
+ description:
70
+ email:
71
+ - sergey.a.tsvetkov@gmail.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - ".rspec"
78
+ - ".travis.yml"
79
+ - Gemfile
80
+ - LICENSE.txt
81
+ - README.md
82
+ - Rakefile
83
+ - lib/smart_vk_api.rb
84
+ - lib/smart_vk_api/call.rb
85
+ - lib/smart_vk_api/constants.rb
86
+ - lib/smart_vk_api/version.rb
87
+ - smart_vk_api.gemspec
88
+ - spec/call_spec.rb
89
+ - spec/spec_helper.rb
90
+ homepage: https://github.com/kimrgrey/smart_vk_api
91
+ licenses:
92
+ - MIT
93
+ metadata: {}
94
+ post_install_message:
95
+ rdoc_options: []
96
+ require_paths:
97
+ - lib
98
+ required_ruby_version: !ruby/object:Gem::Requirement
99
+ requirements:
100
+ - - ">="
101
+ - !ruby/object:Gem::Version
102
+ version: '0'
103
+ required_rubygems_version: !ruby/object:Gem::Requirement
104
+ requirements:
105
+ - - ">="
106
+ - !ruby/object:Gem::Version
107
+ version: '0'
108
+ requirements: []
109
+ rubyforge_project:
110
+ rubygems_version: 2.4.2
111
+ signing_key:
112
+ specification_version: 4
113
+ summary: Small, thin and fast client for VK API
114
+ test_files:
115
+ - spec/call_spec.rb
116
+ - spec/spec_helper.rb