yadirect 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG ADDED
@@ -0,0 +1 @@
1
+ v0.1. Initial
data/Gemfile ADDED
@@ -0,0 +1,5 @@
1
+ source :rubygems
2
+ gemspec
3
+ gem 'rspec'
4
+ gem 'json'
5
+ gem 'curb'
data/Gemfile.lock ADDED
@@ -0,0 +1,32 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ yadirect (0.0.1)
5
+
6
+ GEM
7
+ remote: http://rubygems.org/
8
+ specs:
9
+ curb (0.7.15)
10
+ diff-lcs (1.1.2)
11
+ english (0.6.3)
12
+ language
13
+ json (1.5.3)
14
+ language (0.6.0)
15
+ rspec (2.6.0)
16
+ rspec-core (~> 2.6.0)
17
+ rspec-expectations (~> 2.6.0)
18
+ rspec-mocks (~> 2.6.0)
19
+ rspec-core (2.6.4)
20
+ rspec-expectations (2.6.0)
21
+ diff-lcs (~> 1.1.2)
22
+ rspec-mocks (2.6.0)
23
+
24
+ PLATFORMS
25
+ ruby
26
+
27
+ DEPENDENCIES
28
+ curb
29
+ english
30
+ json
31
+ rspec
32
+ yadirect!
data/LICENSE ADDED
File without changes
data/Manifest ADDED
@@ -0,0 +1,19 @@
1
+ CHANGELOG
2
+ Gemfile
3
+ Gemfile.lock
4
+ LICENSE
5
+ Manifest
6
+ README
7
+ Rakefile
8
+ lib/api_error.rb
9
+ lib/hash.rb
10
+ lib/hashit.rb
11
+ lib/proxy.rb
12
+ lib/string.rb
13
+ lib/yadirect.rb
14
+ spec/hash_spec.rb
15
+ spec/proxy_spec.rb
16
+ spec/spec_helper.rb
17
+ spec/string_spec.rb
18
+ tags
19
+ yadirect.gemspec
data/README ADDED
@@ -0,0 +1,4 @@
1
+ Простая библиотека для работа с API Яндекс.Директ
2
+
3
+ Инициализация: TODO
4
+
data/Rakefile ADDED
@@ -0,0 +1,14 @@
1
+ require 'psych'
2
+ require 'rubygems'
3
+ require 'rake'
4
+ require 'echoe'
5
+
6
+ Echoe.new('yadirect', '0.0.1') do |p|
7
+ p.summary = "A yandex.direct ruby wrapper"
8
+ p.description = "A simple yandex.direct ruby wrapper"
9
+ p.url = "http://github.com/sashich/yadirect"
10
+ p.author = "Alexander Chaychuk"
11
+ p.email = "a.chaychuk@gmail.com"
12
+ p.ignore_pattern = ["tmp/*", "script/*"]
13
+ p.development_dependencies = ["curb"]
14
+ end
data/lib/api_error.rb ADDED
@@ -0,0 +1,16 @@
1
+ module Yadirect
2
+ class ApiError < RuntimeError
3
+ attr :error_detail, :error_str, :error_code
4
+
5
+ def initialize(hash)
6
+ @error_detail = hash["error_detail"]
7
+ @error_str = hash["error_str"]
8
+ @error_code = hash["error_code"]
9
+ end
10
+
11
+ def to_s
12
+ "@error_detail=\"#{@error_detail}\", @error_str=\"#{@error_str}\", @error_code=\"#{@error_code}\""
13
+ end
14
+
15
+ end
16
+ end
data/lib/hash.rb ADDED
@@ -0,0 +1,13 @@
1
+ require 'string'
2
+
3
+ class Hash
4
+ def camelize_keys
5
+ hash = {}
6
+ self.each do |k, v|
7
+ key = k.to_s.to_camelcase
8
+ value = v.is_a?(Hash) ? v.camelize_keys : v
9
+ hash.store(key, value)
10
+ end
11
+ return hash
12
+ end
13
+ end
data/lib/hashit.rb ADDED
@@ -0,0 +1,9 @@
1
+ class Hashit
2
+ def initialize(hash)
3
+ hash.each do |k,v|
4
+ self.instance_variable_set("@#{k}", v) ## create and initialize an instance variable for this key/value pair
5
+ self.class.send(:define_method, k, proc{self.instance_variable_get("@#{k}")}) ## create the getter that returns the instance variable
6
+ self.class.send(:define_method, "#{k}=", proc{|v| self.instance_variable_set("@#{k}", v)}) ## create the setter that sets the instance variable
7
+ end
8
+ end
9
+ end
data/lib/proxy.rb ADDED
@@ -0,0 +1,56 @@
1
+ require 'curb'
2
+ require 'json'
3
+ require 'hashit'
4
+ require 'api_error'
5
+ require 'hash'
6
+ require 'string'
7
+
8
+ module Yadirect
9
+ class Proxy
10
+ EP_YANDEX_DIRECT_V4 = 'https://soap.direct.yandex.ru/json-api/v4/'
11
+
12
+ def initialize params
13
+ @params = params
14
+ @locale = 'RU' || params[:locale]
15
+ end
16
+
17
+ def invoke method, args = {}
18
+ json_object = {:method => method, :locale => @locale, :param => args}.to_json
19
+ puts json_object.to_s
20
+ c = Curl::Easy.http_post(EP_YANDEX_DIRECT_V4, json_object) do |curl|
21
+ curl.cacert = @params[:cacert]
22
+ curl.cert_key = @params[:cert_key]
23
+ curl.certtype = "PEM"
24
+ curl.cert = @params[:cert]
25
+ curl.headers['Accept'] = 'application/json'
26
+ curl.headers['Content-Type'] = 'application/json'
27
+ curl.headers['Api-Version'] = '2.2'
28
+ curl.verbose = true if @params[:debug] == true
29
+ end
30
+
31
+ hash = JSON.parse(c.body_str)
32
+
33
+ if(hash.include?("error_code"))
34
+ raise Yadirect::ApiError, hash
35
+ else
36
+ object_result = Hashit.new(hash)
37
+ object_result.data
38
+ end
39
+ end
40
+
41
+ def method_missing(name, *args, &block)
42
+ ya_params = to_hash_params(*args)
43
+ invoke(name.to_s.to_camelcase, ya_params)
44
+ end
45
+
46
+ def to_hash_params *args
47
+ return {} if args.empty?
48
+ first_arg = args.shift
49
+ if first_arg.is_a?(Hash)
50
+ return first_arg.camelize_keys
51
+ else
52
+ return args
53
+ end
54
+ end
55
+ end
56
+ end
data/lib/string.rb ADDED
@@ -0,0 +1,70 @@
1
+ #encoding: utf-8
2
+
3
+ class String
4
+
5
+ MAP_WORDS = {
6
+ "ping_api"=>"PingAPI",
7
+ "im_client"=>"IMClient",
8
+ "ogrn"=>"OGRN",
9
+ "phrase_id"=>"PhraseID",
10
+ "campaign_id" => "CampaignID",
11
+ "campaign_ids" => "CampaignIDS"
12
+ }
13
+
14
+ def to_camelcase
15
+ MAP_WORDS[self] || camelize(self)
16
+ end
17
+
18
+ def to_underscore
19
+ underscore(self)
20
+ end
21
+
22
+ def find_exeption_words
23
+ MAP_WORDS[self]
24
+ end
25
+
26
+ # By default, +camelize+ converts strings to UpperCamelCase. If the argument to +camelize+
27
+ # is set to <tt>:lower</tt> then +camelize+ produces lowerCamelCase.
28
+ #
29
+ # +camelize+ will also convert '/' to '::' which is useful for converting paths to namespaces.
30
+ #
31
+ # Examples:
32
+ # "active_record".camelize # => "ActiveRecord"
33
+ # "active_record".camelize(:lower) # => "activeRecord"
34
+ # "active_record/errors".camelize # => "ActiveRecord::Errors"
35
+ # "active_record/errors".camelize(:lower) # => "activeRecord::Errors"
36
+ #
37
+ # As a rule of thumb you can think of +camelize+ as the inverse of +underscore+,
38
+ # though there are cases where that does not hold:
39
+ #
40
+ # "SSLError".underscore.camelize # => "SslError"
41
+ def camelize(lower_case_and_underscored_word, first_letter_in_uppercase = true)
42
+ if first_letter_in_uppercase
43
+ lower_case_and_underscored_word.to_s.gsub(/\/(.?)/) { "::#{$1.upcase}" }.gsub(/(?:^|_)(.)/) { $1.upcase }
44
+ else
45
+ lower_case_and_underscored_word.to_s[0].chr.downcase + camelize(lower_case_and_underscored_word)[1..-1]
46
+ end
47
+ end
48
+
49
+ # Makes an underscored, lowercase form from the expression in the string.
50
+ #
51
+ # Changes '::' to '/' to convert namespaces to paths.
52
+ #
53
+ # Examples:
54
+ # "ActiveRecord".underscore # => "active_record"
55
+ # "ActiveRecord::Errors".underscore # => active_record/errors
56
+ #
57
+ # As a rule of thumb you can think of +underscore+ as the inverse of +camelize+,
58
+ # though there are cases where that does not hold:
59
+ #
60
+ # "SSLError".underscore.camelize # => "SslError"
61
+ def underscore(camel_cased_word)
62
+ word = camel_cased_word.to_s.dup
63
+ word.gsub!(/::/, '/')
64
+ word.gsub!(/([A-Z]+)([A-Z][a-z])/,'\1_\2')
65
+ word.gsub!(/([a-z\d])([A-Z])/,'\1_\2')
66
+ word.tr!("-", "_")
67
+ word.downcase!
68
+ word
69
+ end
70
+ end
data/lib/yadirect.rb ADDED
@@ -0,0 +1,4 @@
1
+ module Yadirect
2
+ require 'proxy'
3
+ end
4
+
data/spec/hash_spec.rb ADDED
@@ -0,0 +1,13 @@
1
+ #encoding: utf-8
2
+ require 'spec_helper'
3
+ require 'hash'
4
+
5
+ describe Hash do
6
+ it "should return camelize keys of hash" do
7
+ hash = {:name => "Таня", :last_name => "Сенникова",
8
+ :options => {:size => 3, :smlie => {:form_face => "Cool"}}}
9
+ camelize_hash = hash.camelize_keys
10
+ puts camelize_hash
11
+ end
12
+ end
13
+
@@ -0,0 +1,53 @@
1
+ #encoding: utf-8
2
+ require 'spec_helper'
3
+ require 'yadirect'
4
+ require 'json'
5
+
6
+ describe Yadirect::Proxy do
7
+
8
+ before(:all) do
9
+ cert_dir = '/tmp/yadirect/cert'
10
+ cert_key = File.join(cert_dir, 'private.key')
11
+ cacert = File.join(cert_dir, 'cacert.pem')
12
+ cert = File.join(cert_dir, 'cert.crt')
13
+ params = {:cert_key => cert_key, :cacert => cacert, :cert => cert}
14
+ @proxy = Yadirect::Proxy.new(params)
15
+ end
16
+
17
+ it "PingAPI" do
18
+ result = @proxy.ping_api
19
+ result.should == 1
20
+ end
21
+
22
+ it "GetKeywordsSuggestion" do
23
+ suggestions = @proxy.get_keywords_suggestion(:keywords => ["недвижимость"])
24
+ suggestions.should_not be nil
25
+ suggestions.empty?.should be false
26
+ end
27
+
28
+ it "GetRegions" do
29
+ regions = @proxy.get_regions
30
+ regions.should_not be nil
31
+ regions.empty?.should be false
32
+ end
33
+
34
+ it "GetCampaignsList" do
35
+ campaigns = @proxy.get_campaigns_list "mr.sashich"
36
+ campaigns.should_not be nil
37
+ campaigns.empty?.should be false
38
+ end
39
+
40
+ it "GetWordstatReportList" do
41
+ list = @proxy.get_wordstat_report_list
42
+ list.should_not be nil
43
+ list.empty?.should be false
44
+ end
45
+
46
+ it "GetBanners" do
47
+ banners = @proxy.get_banners :campaign_ids=>[2929027]
48
+ banners.should_not be nil
49
+ banners.empty?.should be false
50
+ end
51
+
52
+
53
+ end
@@ -0,0 +1,6 @@
1
+ require 'rubygems'
2
+ require 'bundler/setup'
3
+
4
+ RSpec.configure do |config|
5
+ # some (optional) config here
6
+ end
@@ -0,0 +1,11 @@
1
+ require 'spec_helper'
2
+ require 'string'
3
+ describe String do
4
+ it "camelcase should return string in camel case" do
5
+ "auto_budget_priority".to_camelcase.should eq("AutoBudgetPriority")
6
+ end
7
+
8
+ it "camelcase with exceptions" do
9
+ "ping_api".to_camelcase.should eq("PingAPI")
10
+ end
11
+ end
data/tags ADDED
@@ -0,0 +1,24 @@
1
+ !_TAG_FILE_FORMAT 2 /extended format; --format=1 will not append ;" to lines/
2
+ !_TAG_FILE_SORTED 1 /0=unsorted, 1=sorted, 2=foldcase/
3
+ !_TAG_PROGRAM_AUTHOR Darren Hiebert /dhiebert@users.sourceforge.net/
4
+ !_TAG_PROGRAM_NAME Exuberant Ctags //
5
+ !_TAG_PROGRAM_URL http://ctags.sourceforge.net /official site/
6
+ !_TAG_PROGRAM_VERSION 5.9~svn20110310 //
7
+ Hashit lib/hashit.rb /^class Hashit$/;" c
8
+ Proxy lib/campaign_stat_api.rb /^ class Proxy$/;" c class:Yadirect
9
+ Proxy lib/keywords_api.rb /^ class Proxy$/;" c class:Yadirect
10
+ Proxy lib/miscellaneous_api.rb /^ class Proxy$/;" c class:Yadirect
11
+ Proxy lib/proxy.rb /^ class Proxy$/;" c class:Yadirect
12
+ Yadirect lib/campaign_stat_api.rb /^module Yadirect$/;" m
13
+ Yadirect lib/keywords_api.rb /^module Yadirect$/;" m
14
+ Yadirect lib/miscellaneous_api.rb /^module Yadirect$/;" m
15
+ Yadirect lib/proxy.rb /^module Yadirect$/;" m
16
+ Yadirect lib/yadirect.rb /^module Yadirect$/;" m
17
+ YadirectError lib/yadirect_error.rb /^class YadirectError < RuntimeError$/;" c
18
+ create_new_report lib/campaign_stat_api.rb /^ def create_new_report *args$/;" f class:Yadirect.Proxy
19
+ get_keywords_suggestion lib/keywords_api.rb /^ def get_keywords_suggestion *args$/;" f class:Yadirect.Proxy
20
+ initialize lib/hashit.rb /^ def initialize(hash)$/;" f class:Hashit
21
+ initialize lib/proxy.rb /^ def initialize params$/;" f class:Yadirect.Proxy
22
+ initialize lib/yadirect_error.rb /^ def initialize(error_detail, error_str, error_code)$/;" f class:YadirectError
23
+ invoke lib/proxy.rb /^ def invoke method, args = {}$/;" f class:Yadirect.Proxy
24
+ ping_api lib/miscellaneous_api.rb /^ def ping_api$/;" f class:Yadirect.Proxy
data/yadirect.gemspec ADDED
@@ -0,0 +1,32 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = %q{yadirect}
5
+ s.version = "0.0.1"
6
+
7
+ s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
8
+ s.authors = [%q{Alexander Chaychuk}]
9
+ s.date = %q{2011-06-24}
10
+ s.description = %q{A simple yandex.direct ruby wrapper}
11
+ s.email = %q{a.chaychuk@gmail.com}
12
+ s.extra_rdoc_files = [%q{CHANGELOG}, %q{LICENSE}, %q{README}, %q{lib/api_error.rb}, %q{lib/hash.rb}, %q{lib/hashit.rb}, %q{lib/proxy.rb}, %q{lib/string.rb}, %q{lib/yadirect.rb}]
13
+ s.files = [%q{CHANGELOG}, %q{Gemfile}, %q{Gemfile.lock}, %q{LICENSE}, %q{Manifest}, %q{README}, %q{Rakefile}, %q{lib/api_error.rb}, %q{lib/hash.rb}, %q{lib/hashit.rb}, %q{lib/proxy.rb}, %q{lib/string.rb}, %q{lib/yadirect.rb}, %q{spec/hash_spec.rb}, %q{spec/proxy_spec.rb}, %q{spec/spec_helper.rb}, %q{spec/string_spec.rb}, %q{tags}, %q{yadirect.gemspec}]
14
+ s.homepage = %q{http://github.com/sashich/yadirect}
15
+ s.rdoc_options = [%q{--line-numbers}, %q{--inline-source}, %q{--title}, %q{Yadirect}, %q{--main}, %q{README}]
16
+ s.require_paths = [%q{lib}]
17
+ s.rubyforge_project = %q{yadirect}
18
+ s.rubygems_version = %q{1.8.5}
19
+ s.summary = %q{A yandex.direct ruby wrapper}
20
+
21
+ if s.respond_to? :specification_version then
22
+ s.specification_version = 3
23
+
24
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
25
+ s.add_development_dependency(%q<curb>, [">= 0"])
26
+ else
27
+ s.add_dependency(%q<curb>, [">= 0"])
28
+ end
29
+ else
30
+ s.add_dependency(%q<curb>, [">= 0"])
31
+ end
32
+ end
metadata ADDED
@@ -0,0 +1,89 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: yadirect
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Alexander Chaychuk
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2011-06-24 00:00:00.000000000Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: curb
16
+ requirement: &79563000 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: *79563000
25
+ description: A simple yandex.direct ruby wrapper
26
+ email: a.chaychuk@gmail.com
27
+ executables: []
28
+ extensions: []
29
+ extra_rdoc_files:
30
+ - CHANGELOG
31
+ - LICENSE
32
+ - README
33
+ - lib/api_error.rb
34
+ - lib/hash.rb
35
+ - lib/hashit.rb
36
+ - lib/proxy.rb
37
+ - lib/string.rb
38
+ - lib/yadirect.rb
39
+ files:
40
+ - CHANGELOG
41
+ - Gemfile
42
+ - Gemfile.lock
43
+ - LICENSE
44
+ - Manifest
45
+ - README
46
+ - Rakefile
47
+ - lib/api_error.rb
48
+ - lib/hash.rb
49
+ - lib/hashit.rb
50
+ - lib/proxy.rb
51
+ - lib/string.rb
52
+ - lib/yadirect.rb
53
+ - spec/hash_spec.rb
54
+ - spec/proxy_spec.rb
55
+ - spec/spec_helper.rb
56
+ - spec/string_spec.rb
57
+ - tags
58
+ - yadirect.gemspec
59
+ homepage: http://github.com/sashich/yadirect
60
+ licenses: []
61
+ post_install_message:
62
+ rdoc_options:
63
+ - --line-numbers
64
+ - --inline-source
65
+ - --title
66
+ - Yadirect
67
+ - --main
68
+ - README
69
+ require_paths:
70
+ - lib
71
+ required_ruby_version: !ruby/object:Gem::Requirement
72
+ none: false
73
+ requirements:
74
+ - - ! '>='
75
+ - !ruby/object:Gem::Version
76
+ version: '0'
77
+ required_rubygems_version: !ruby/object:Gem::Requirement
78
+ none: false
79
+ requirements:
80
+ - - ! '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '1.2'
83
+ requirements: []
84
+ rubyforge_project: yadirect
85
+ rubygems_version: 1.8.5
86
+ signing_key:
87
+ specification_version: 3
88
+ summary: A yandex.direct ruby wrapper
89
+ test_files: []