ya_direct_api 0.0.1

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: 09046877f1e8478773c8f43f69964c6619e56344
4
+ data.tar.gz: c8591d23453740943c13137495e59a33e274abe5
5
+ SHA512:
6
+ metadata.gz: 4795b88ad7d020df01b5454b03afb8617a903ac5277b7d9babb6b9743731cd9d20830ed0a14bb00d7de7f47a34d6df3bdc3d0ebb0a8a9c414a557dc72a22d34c
7
+ data.tar.gz: 92d5e2d5cc2f0e67f06698913b0f434dd6516600786623ea5fdf92b3b4b7940e306f6884e846f83341201a260fce70c406d3b47f13395e69d72ad2b1fd74aee0
data/.gitignore ADDED
@@ -0,0 +1,18 @@
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
+ .DS_Store
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source 'https://rubygems.org'
2
+ gemspec
3
+
4
+ gem "bundler", "~> 1.5"
5
+ gem "rake"
6
+ gem "minitest"
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Andrey Savelyev
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,32 @@
1
+ # YaDirectApi
2
+
3
+ Simple gem for Yandex Direct Api using with ruby
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'ya_direct_api'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install ya_direct_api
18
+
19
+ ## Usage
20
+
21
+ ```
22
+ @api = Ya::Direct::Api.new('token', 'app_id', 'login', 'ru')
23
+ json = @api.get_campaigns_list
24
+ ```
25
+
26
+ ## Contributing
27
+
28
+ 1. Fork it ( http://github.com/AndreySavelyev/ya_direct_api/fork )
29
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
30
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
31
+ 4. Push to the branch (`git push origin my-new-feature`)
32
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,7 @@
1
+ require "bundler/gem_tasks"
2
+ require 'rake/testtask'
3
+
4
+ Rake::TestTask.new do |t|
5
+ t.libs << 'test'
6
+ t.pattern = "test/**/*_test.rb"
7
+ end
@@ -0,0 +1,18 @@
1
+ module Ya
2
+ module Direct
3
+ class Exception < StandardError
4
+ attr_reader :error_detail, :error_code, :error_str
5
+
6
+ def initialize(error_detail, error_str, error_code)
7
+ @error_detail = error_detail
8
+ @error_str = error_str
9
+ @error_code = error_code
10
+ end
11
+
12
+ def to_s
13
+ "#{@error_str} : #{@error_detail} : #{@error_code}"
14
+ end
15
+
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,59 @@
1
+ module Ya
2
+ module Direct
3
+ class Gateway
4
+ attr_reader :url
5
+
6
+ URLS = {
7
+ production: "https://soap.direct.yandex.ru/json-api/v4/",
8
+ development: "https://api-sandbox.direct.yandex.ru/json-api/v4/"
9
+ }
10
+
11
+ def initialize(env, config)
12
+ @url = URLS[env.to_sym]
13
+ @config = config
14
+ end
15
+
16
+ def request_options(method, params)
17
+ {
18
+ locale: @config[:locale],
19
+ login: @config[:login],
20
+ application_id: @config[:app_id],
21
+ token: @config[:token],
22
+ method: method
23
+ }.merge(params)
24
+ end
25
+
26
+ def request(method, params)
27
+ url = URI.parse(@url)
28
+
29
+ request = Net::HTTP::Post.new(url.path, initheader = {'Content-Type' =>'application/json'})
30
+ request.body = request_options(method, params).to_json
31
+
32
+ http = Net::HTTP.new(url.host, url.port)
33
+ http.use_ssl = true
34
+ http.verify_mode = OpenSSL::SSL::VERIFY_NONE
35
+ response = http.request(request)
36
+
37
+ if response.kind_of? Net::HTTPSuccess
38
+ return response.body
39
+ else
40
+ raise response.inspect
41
+ end
42
+ end
43
+
44
+ def get_data(method, params)
45
+ response = request(method, params)
46
+ parsed_response = JSON.parse(response)
47
+ validate_response!(parsed_response)
48
+ parsed_response
49
+ end
50
+
51
+ def validate_response!(response)
52
+ if response.has_key?('error_code')
53
+ raise Ya::Direct::Exception.new(response['error_detail'], response['error_str'], response['error_code'])
54
+ end
55
+ end
56
+
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,3 @@
1
+ module YaDirectApi
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,34 @@
1
+ require "net/http"
2
+ require "active_support/core_ext"
3
+
4
+ require 'ya_direct_api/version'
5
+ require 'ya_direct_api/gateway'
6
+ require 'ya_direct_api/exception'
7
+
8
+ module Ya
9
+ module Direct
10
+ class Api
11
+ attr_reader :config, :gateway, :env
12
+
13
+ def initialize(token, app_id, login, locale = 'en')
14
+ @config = {
15
+ token: token,
16
+ app_id: app_id,
17
+ login: login,
18
+ locale: locale
19
+ }
20
+ @env = resolve_env
21
+ @gateway = Ya::Direct::Gateway.new(@env, @config)
22
+ end
23
+
24
+ def method_missing(method_name, args = {})
25
+ @gateway.get_data(method_name.to_s.camelize, args)
26
+ end
27
+
28
+ def resolve_env
29
+ ENV['RAILS_ENV'] || ENV['RACK_ENV'] || 'development'
30
+ end
31
+
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,18 @@
1
+ require "test_helper"
2
+ require 'minitest/autorun'
3
+
4
+ class ExceptionTest < YaDirect::TestCase
5
+ def setup
6
+ @api = Ya::Direct::Api.new('token', 'app_id', 'login', 'ru')
7
+ @error_string = {"error_detail"=>"", "error_str"=>"Invalid request", "error_code"=>'0'}.to_json.to_s
8
+ end
9
+
10
+ def test_exception_getting
11
+ assert_raises Ya::Direct::Exception do
12
+ @api.gateway.stub(:request, @error_string) do
13
+ @api.get_campaigns_list
14
+ end
15
+ end
16
+ end
17
+
18
+ end
@@ -0,0 +1,13 @@
1
+ require "test_helper"
2
+ require 'minitest/autorun'
3
+
4
+ class GatewayTest < YaDirect::TestCase
5
+ def setup
6
+ @api = Ya::Direct::Api.new('token', 'app_id', 'login', 'ru')
7
+ end
8
+
9
+ def test_gateway_url
10
+ assert_equal "https://api-sandbox.direct.yandex.ru/json-api/v4/", @api.gateway.url
11
+ end
12
+
13
+ end
@@ -0,0 +1,13 @@
1
+ require "test_helper"
2
+ require 'minitest/autorun'
3
+
4
+ class YaDirectApiTest < YaDirect::TestCase
5
+ def setup
6
+ @api = Ya::Direct::Api.new('token', 'app_id', 'login', 'ru')
7
+ end
8
+
9
+ def test_that_default_env_is_development
10
+ assert_equal "development", @api.env
11
+ end
12
+
13
+ end
@@ -0,0 +1,14 @@
1
+ require 'bundler/setup'
2
+
3
+ require 'ya_direct_api'
4
+
5
+ require 'minitest/unit'
6
+ require "minitest/autorun"
7
+ require 'minitest/pride'
8
+
9
+ Bundler.require
10
+
11
+ module YaDirect
12
+ class TestCase < MiniTest::Unit::TestCase
13
+ end
14
+ end
@@ -0,0 +1,22 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'ya_direct_api/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "ya_direct_api"
8
+ spec.version = YaDirectApi::VERSION
9
+ spec.authors = ["Andrey Savelyev"]
10
+ spec.email = ["savelyev.andrey@gmail.com"]
11
+ spec.summary = %q{Yandex direct api wrapper gem}
12
+ spec.description = %q{Yandex direct api wrapper gem}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_runtime_dependency 'activesupport', '>= 3.0.0'
22
+ end
metadata ADDED
@@ -0,0 +1,77 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ya_direct_api
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Andrey Savelyev
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-04-16 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activesupport
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 3.0.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: 3.0.0
27
+ description: Yandex direct api wrapper gem
28
+ email:
29
+ - savelyev.andrey@gmail.com
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - ".gitignore"
35
+ - Gemfile
36
+ - LICENSE.txt
37
+ - README.md
38
+ - Rakefile
39
+ - lib/ya_direct_api.rb
40
+ - lib/ya_direct_api/exception.rb
41
+ - lib/ya_direct_api/gateway.rb
42
+ - lib/ya_direct_api/version.rb
43
+ - test/lib/ya_direct_api/exception_test.rb
44
+ - test/lib/ya_direct_api/gateway_test.rb
45
+ - test/lib/ya_direct_api_test.rb
46
+ - test/test_helper.rb
47
+ - ya_direct_api.gemspec
48
+ homepage: ''
49
+ licenses:
50
+ - MIT
51
+ metadata: {}
52
+ post_install_message:
53
+ rdoc_options: []
54
+ require_paths:
55
+ - lib
56
+ required_ruby_version: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ version: '0'
61
+ required_rubygems_version: !ruby/object:Gem::Requirement
62
+ requirements:
63
+ - - ">="
64
+ - !ruby/object:Gem::Version
65
+ version: '0'
66
+ requirements: []
67
+ rubyforge_project:
68
+ rubygems_version: 2.2.2
69
+ signing_key:
70
+ specification_version: 4
71
+ summary: Yandex direct api wrapper gem
72
+ test_files:
73
+ - test/lib/ya_direct_api/exception_test.rb
74
+ - test/lib/ya_direct_api/gateway_test.rb
75
+ - test/lib/ya_direct_api_test.rb
76
+ - test/test_helper.rb
77
+ has_rdoc: