direct-api-v5 0.0.1 → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (38) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +2 -1
  3. data/.rspec +2 -0
  4. data/.rubocop.yml +19 -0
  5. data/.travis.yml +17 -0
  6. data/Gemfile +2 -0
  7. data/README.md +127 -3
  8. data/direct-api-v5.gemspec +11 -5
  9. data/lib/direct/api/v5.rb +48 -3
  10. data/lib/direct/api/v5/client.rb +18 -0
  11. data/lib/direct/api/v5/default_settings.rb +42 -0
  12. data/lib/direct/api/v5/errors.rb +10 -0
  13. data/lib/direct/api/v5/params_builder.rb +46 -0
  14. data/lib/direct/api/v5/refinements/camelize.rb +22 -0
  15. data/lib/direct/api/v5/request.rb +66 -0
  16. data/lib/direct/api/v5/response.rb +32 -0
  17. data/lib/direct/api/v5/response/error.rb +21 -0
  18. data/lib/direct/api/v5/response/units.rb +34 -0
  19. data/lib/direct/api/v5/service.rb +32 -0
  20. data/lib/direct/api/v5/settings.rb +19 -0
  21. data/lib/direct/api/v5/version.rb +1 -1
  22. data/spec/acceptance/error_spec.rb +32 -0
  23. data/spec/acceptance/get_campaigns_spec.rb +72 -0
  24. data/spec/direct/api/v5/client_spec.rb +28 -0
  25. data/spec/direct/api/v5/default_settings_spec.rb +42 -0
  26. data/spec/direct/api/v5/params_builder_spec.rb +39 -0
  27. data/spec/direct/api/v5/refinements/camelize_spec.rb +21 -0
  28. data/spec/direct/api/v5/request_spec.rb +60 -0
  29. data/spec/direct/api/v5/response/error_spec.rb +30 -0
  30. data/spec/direct/api/v5/response/units_spec.rb +31 -0
  31. data/spec/direct/api/v5/response_spec.rb +66 -0
  32. data/spec/direct/api/v5/settings_spec.rb +42 -0
  33. data/spec/direct/api/v5_spec.rb +37 -0
  34. data/spec/fixtures/settings.yml +5 -0
  35. data/spec/shared/direct_api_helper.rb +66 -0
  36. data/spec/spec_helper.rb +25 -0
  37. data/spec/support/webmock.rb +3 -0
  38. metadata +127 -11
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: bbdcb212c1664f42f2fb8a647eaa5331c5eab341
4
- data.tar.gz: 3e2c3b16e9003da6a36d24ba36b33c40a7f29492
3
+ metadata.gz: e0b7ada51310f1611e7cd6c4b0b1ac4e7b85d216
4
+ data.tar.gz: 0aa5fe6526f859074b69aee22679337c8e5a7f3d
5
5
  SHA512:
6
- metadata.gz: 577c0ee649f3508d3a39682f3de3a712953c262d3fe9e54b7a542d7a57a961b300502a74ef20ba417bc3e66ff1d43387e1802e8706bb853fe9804f8d2398468c
7
- data.tar.gz: ece1ac9ef9c62549bf72e7d818172f49b3606813c02416a5180405a92511960d814336754bed484f5c099a52eb5eb9bca1e2a9e816726c3a59e5a02aa210e99d
6
+ metadata.gz: 6713261cb4abccccc582f94b834e12cb4f95434b125b57dc4e771bd6e3553d53c47fdf750b9570888b9260f103eb0b75efc0dcc04d59d2138b5e28b1a8c7998d
7
+ data.tar.gz: c9cf7c9c34511c8ee0906affbdd5e47c0f5b2bf0e785417f350761a49f4d8fac92342e2664419b76ef671a8e859688191d264beba405123f6c14001795338811
data/.gitignore CHANGED
@@ -20,4 +20,5 @@ tmp
20
20
  *.o
21
21
  *.a
22
22
  mkmf.log
23
- .idea/*
23
+ .ruby-gemset
24
+ .ruby-version
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --require spec_helper
@@ -0,0 +1,19 @@
1
+ AllCops:
2
+ DisplayCopNames: true
3
+ DisplayStyleGuide: true
4
+
5
+ Metrics/LineLength:
6
+ Max: 120
7
+
8
+ Metrics/MethodLength:
9
+ Max: 15
10
+
11
+ Style/BlockComments:
12
+ Exclude:
13
+ - 'spec/spec_helper.rb'
14
+
15
+ Style/Documentation:
16
+ Enabled: false
17
+
18
+ Style/ClassAndModuleChildren:
19
+ Enabled: false
@@ -0,0 +1,17 @@
1
+ language: ruby
2
+ script:
3
+ - bundle exec rspec
4
+ rvm:
5
+ - 2.1.8
6
+ - 2.2.4
7
+ - 2.3.0
8
+ notifications:
9
+ webhooks:
10
+ urls:
11
+ - https://webhooks.gitter.im/e/d9db7a73fbd8f58f1432
12
+ on_success: change # options: [always|never|change] default: always
13
+ on_failure: always # options: [always|never|change] default: always
14
+ on_start: never # options: [always|never|change] default: always
15
+ addons:
16
+ code_climate:
17
+ repo_token: 69b68e6f16334696d97d7171ead60f33ab98eb50026a92bde56963f7722492f8
data/Gemfile CHANGED
@@ -1,3 +1,5 @@
1
1
  source 'https://rubygems.org'
2
2
 
3
3
  gemspec
4
+
5
+ gem 'codeclimate-test-reporter', group: :test, require: nil
data/README.md CHANGED
@@ -1,6 +1,13 @@
1
1
  # Direct::API::V5
2
2
 
3
- TODO: Write a gem description
3
+ [![Gem Version](https://badge.fury.io/rb/direct-api-v5.svg)](https://badge.fury.io/rb/direct-api-v5)
4
+ [![Travis CI](https://travis-ci.org/hrom512/direct-api-v5.svg?branch=dev)](https://travis-ci.org/hrom512/direct-api-v5)
5
+ [![Code Climate](https://codeclimate.com/github/Hrom512/direct-api-v5/badges/gpa.svg)](https://codeclimate.com/github/Hrom512/direct-api-v5)
6
+ [![Test Coverage](https://codeclimate.com/github/Hrom512/direct-api-v5/badges/coverage.svg)](https://codeclimate.com/github/Hrom512/direct-api-v5/coverage)
7
+ [![Dependency Status](https://gemnasium.com/hrom512/direct-api-v5.svg)](https://gemnasium.com/hrom512/direct-api-v5)
8
+
9
+ Ruby wrapper for Yandex Direct API V5.
10
+
4
11
 
5
12
  ## Installation
6
13
 
@@ -16,13 +23,130 @@ Or install it yourself as:
16
23
 
17
24
  $ gem install direct-api-v5
18
25
 
26
+
19
27
  ## Usage
20
28
 
21
- TODO: Write usage instructions here
29
+ ### Settings
30
+
31
+ 1. Load settings from YML file
32
+
33
+ ```ruby
34
+ Direct::API::V5.load_settings('config/yandex_direct_api.yml')
35
+ ```
36
+
37
+ ```yml
38
+ production:
39
+ host: api.direct.yandex.com
40
+ auth_token: token_string
41
+ client_login: login_string
42
+
43
+ development:
44
+ host: api-sandbox.direct.yandex.com
45
+ auth_token: token_string
46
+ client_login: login_string
47
+ ```
48
+
49
+ 2. Configure in block
50
+
51
+ ```ruby
52
+ Direct::API::V5.configure do |config|
53
+ config.host = 'api.direct.yandex.com'
54
+ config.auth_token = 'token_string'
55
+ config.client_login = 'login_string'
56
+ end
57
+ ```
58
+
59
+ ### Create client
60
+
61
+ ```ruby
62
+ api = Direct::API::V5.client
63
+ ```
64
+
65
+ You can set client login (if advertising agency)
66
+
67
+ ```ruby
68
+ api = Direct::API::V5.client(client_login: 'login_string')
69
+ ```
70
+
71
+ Also you can overwrite any default settings:
72
+
73
+ ```ruby
74
+ api = Direct::API::V5.client(host: 'api.direct.yandex.com', auth_token: 'token')
75
+ ```
76
+
77
+ ### Call methods
78
+
79
+ Base structure:
80
+
81
+ ```ruby
82
+ api.service_name.method(params)
83
+ ```
84
+
85
+ For example:
86
+
87
+ ```ruby
88
+ response = api.campaigns.get(
89
+ fields: [:id, :name],
90
+ criteria: { states: %(ON) },
91
+ page: { limit: 100, offset: 200 }
92
+ )
93
+
94
+ response.error?
95
+ # => false
96
+
97
+ response.result
98
+ # =>
99
+ # {
100
+ # Campaigns: [
101
+ # { Id: 1, Name: "Campaign 1" },
102
+ # ...
103
+ # ]
104
+ # }
105
+
106
+ response.request_id
107
+ # => 123456
108
+
109
+ response.units.spent
110
+ # => 10
111
+
112
+ response.units.available
113
+ # => 1000
114
+
115
+ response.units.daily_limit
116
+ # => 2000
117
+
118
+ response.units.raw
119
+ # => 10/1000/2000
120
+ ```
121
+
122
+ If API return error:
123
+
124
+ ```ruby
125
+ response.error?
126
+ # => true
127
+
128
+ response.error.code
129
+ # => 54
130
+
131
+ response.error.message
132
+ # => "No rights"
133
+
134
+ response.error.details
135
+ # => "No rights to indicated client"
136
+
137
+ response.error.to_h
138
+ # =>
139
+ # {
140
+ # code: 54,
141
+ # message: "No rights",
142
+ # details: "No rights to indicated client"
143
+ # }
144
+ ```
145
+
22
146
 
23
147
  ## Contributing
24
148
 
25
- 1. Fork it ( https://github.com/[my-github-username]/direct-api-v5/fork )
149
+ 1. Fork it ( https://github.com/hrom512/direct-api-v5/fork )
26
150
  2. Create your feature branch (`git checkout -b my-new-feature`)
27
151
  3. Commit your changes (`git commit -am 'Add some feature'`)
28
152
  4. Push to the branch (`git push origin my-new-feature`)
@@ -6,10 +6,10 @@ require 'direct/api/v5/version'
6
6
  Gem::Specification.new do |spec|
7
7
  spec.name = 'direct-api-v5'
8
8
  spec.version = Direct::API::V5::VERSION
9
- spec.author = 'Khrebtov Roman'
9
+ spec.author = 'Roman Khrebtov'
10
10
  spec.email = 'roman@alltmb.ru'
11
11
  spec.summary = 'Yandex Direct API V5'
12
- spec.description = 'Ruby client for Yandex Direct API V5'
12
+ spec.description = 'Ruby wrapper for Yandex Direct API V5'
13
13
  spec.homepage = 'https://github.com/Hrom512/direct-api-v5'
14
14
  spec.license = 'MIT'
15
15
 
@@ -18,8 +18,14 @@ Gem::Specification.new do |spec|
18
18
  spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
19
  spec.require_paths = ['lib']
20
20
 
21
- spec.required_ruby_version = '>= 2.0'
21
+ spec.required_ruby_version = '>= 2.1'
22
22
 
23
- spec.add_development_dependency 'bundler', '~> 1.6'
24
- spec.add_development_dependency 'rake', '~> 10.1'
23
+ spec.add_dependency 'faraday', '~> 0.9'
24
+ spec.add_dependency 'multi_json', '~> 1.12'
25
+
26
+ spec.add_development_dependency 'bundler', '~> 1.12'
27
+ spec.add_development_dependency 'rake', '~> 10.5'
28
+ spec.add_development_dependency 'rspec', '~> 3.5'
29
+ spec.add_development_dependency 'webmock', '~> 2.1'
30
+ spec.add_development_dependency 'rubocop'
25
31
  end
@@ -1,8 +1,53 @@
1
+ require 'faraday'
2
+ require 'multi_json'
3
+
1
4
  require 'direct/api/v5/version'
5
+ require 'direct/api/v5/errors'
6
+ require 'direct/api/v5/refinements/camelize'
7
+ require 'direct/api/v5/default_settings'
8
+ require 'direct/api/v5/settings'
9
+ require 'direct/api/v5/client'
10
+ require 'direct/api/v5/service'
11
+ require 'direct/api/v5/request'
12
+ require 'direct/api/v5/params_builder'
13
+ require 'direct/api/v5/response'
14
+ require 'direct/api/v5/response/error'
15
+ require 'direct/api/v5/response/units'
16
+
17
+ module Direct::API::V5
18
+ class << self
19
+ # Create API client
20
+ # @param settings [Hash] API settings (:host, :auth_token, :client_login)
21
+ # @return [Direct::API::V5::Client] new client object
22
+ #
23
+ # @example Without settings
24
+ # client = Direct::API::V5.client
25
+ #
26
+ # @example With settings
27
+ # client = Direct::API::V5.client(host: 'api.direct.yandex.com',
28
+ # auth_token: 'token',
29
+ # client_login: 'login')
30
+ def client(settings = {})
31
+ Client.new(settings)
32
+ end
33
+
34
+ # Set default settings from block
35
+ # @example
36
+ # Direct::API::V5.configure do |config|
37
+ # config.host = 'api.direct.yandex.com'
38
+ # config.auth_token = 'token'
39
+ # config.client_login = 'login'
40
+ # end
41
+ def configure(&block)
42
+ DefaultSettings.configure(&block)
43
+ end
2
44
 
3
- module Direct
4
- module API
5
- module V5
45
+ # Set default settings from .yml file
46
+ # @param yml_file [String] path to .yml file
47
+ # @example
48
+ # Direct::API::V5.load_settings('config/yandex_direct_api.yml')
49
+ def load_settings(yml_file)
50
+ DefaultSettings.load_from_yml(yml_file)
6
51
  end
7
52
  end
8
53
  end
@@ -0,0 +1,18 @@
1
+ module Direct::API::V5
2
+ class Client
3
+ attr_reader :settings
4
+
5
+ def initialize(settings = {})
6
+ @settings = Settings.new(settings)
7
+ end
8
+
9
+ # Direct services methods
10
+ def method_missing(method, *args)
11
+ if args.empty?
12
+ Service.new(client: self, service_name: method)
13
+ else
14
+ super
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,42 @@
1
+ module Direct
2
+ module API
3
+ module V5
4
+ # Default API settings store
5
+ class DefaultSettings
6
+ class << self
7
+ DEFAULT_HOST = 'api.direct.yandex.com'.freeze
8
+ DEFAULT_LANGUAGE = 'en'.freeze
9
+
10
+ attr_accessor :host
11
+ attr_accessor :language
12
+ attr_accessor :auth_token
13
+ attr_accessor :client_login
14
+
15
+ # Return configured host or default value
16
+ def host
17
+ @host || DEFAULT_HOST
18
+ end
19
+
20
+ # Return configured language or default value
21
+ def language
22
+ @language || DEFAULT_LANGUAGE
23
+ end
24
+
25
+ # Set default settings from .yml file
26
+ def load_from_yml(yml_file)
27
+ settings = YAML.load_file(yml_file)[ENV['RAILS_ENV']] || {}
28
+ @host = settings['host']
29
+ @language = settings['language']
30
+ @auth_token = settings['auth_token']
31
+ @client_login = settings['client_login']
32
+ end
33
+
34
+ # Set default settings from block
35
+ def configure
36
+ yield(self)
37
+ end
38
+ end
39
+ end
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,10 @@
1
+ module Direct::API::V5
2
+ class Error < StandardError
3
+ end
4
+
5
+ class RequestError < Error
6
+ end
7
+
8
+ class InvalidResponseError < Error
9
+ end
10
+ end
@@ -0,0 +1,46 @@
1
+ module Direct::API::V5
2
+ class ParamsBuilder
3
+ using Refinements::Camelize
4
+
5
+ def initialize(params = {})
6
+ @params = params
7
+ end
8
+
9
+ def build
10
+ process_value(@params)
11
+ end
12
+
13
+ private
14
+
15
+ def process_value(value)
16
+ if value.is_a?(Hash)
17
+ process_hash(value)
18
+ elsif value.is_a?(Array)
19
+ process_array(value)
20
+ elsif value.is_a?(Symbol)
21
+ value.camelize
22
+ else
23
+ value
24
+ end
25
+ end
26
+
27
+ def process_hash(hash)
28
+ result = {}
29
+
30
+ hash.each do |key, value|
31
+ key = :selection_criteria if key == :criteria
32
+ key = "#{key.to_s[0..-7]}field_names".to_sym if key.to_s.end_with?('fields')
33
+
34
+ new_key = process_value(key)
35
+ new_value = process_value(value)
36
+ result[new_key] = new_value
37
+ end
38
+
39
+ result
40
+ end
41
+
42
+ def process_array(array)
43
+ array.map { |item| process_value(item) }
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,22 @@
1
+ module Direct::API::V5
2
+ module Refinements
3
+ # Add camelize method to String and Symbol
4
+ module Camelize
5
+ refine String do
6
+ def camelize
7
+ if self =~ /[A-Z]/
8
+ self
9
+ else
10
+ split('_').map(&:capitalize).join('')
11
+ end
12
+ end
13
+ end
14
+
15
+ refine Symbol do
16
+ def camelize
17
+ to_s.camelize.to_sym
18
+ end
19
+ end
20
+ end
21
+ end
22
+ end