ponominalu 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +16 -0
- data/.travis.yml +6 -0
- data/CHANGELOG.md +0 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +1 -0
- data/Rakefile +16 -0
- data/lib/generators/ponominalu/install/USAGE +2 -0
- data/lib/generators/ponominalu/install/install_generator.rb +9 -0
- data/lib/generators/ponominalu/install/templates/initializer.rb +22 -0
- data/lib/ponominalu.rb +25 -0
- data/lib/ponominalu/api.rb +48 -0
- data/lib/ponominalu/configuration.rb +60 -0
- data/lib/ponominalu/error.rb +32 -0
- data/lib/ponominalu/helpers.rb +27 -0
- data/lib/ponominalu/middleware.rb +58 -0
- data/lib/ponominalu/response.rb +39 -0
- data/lib/ponominalu/simple_methods.yml +2 -0
- data/lib/ponominalu/version.rb +4 -0
- data/ponominalu.gemspec +32 -0
- data/spec/ponominalu/api_spec.rb +58 -0
- data/spec/ponominalu/configuration_spec.rb +55 -0
- data/spec/ponominalu/error_spec.rb +40 -0
- data/spec/ponominalu/helpers_spec.rb +22 -0
- data/spec/ponominalu/middleware_spec.rb +112 -0
- data/spec/ponominalu/response_spec.rb +58 -0
- data/spec/ponominalu_spec.rb +17 -0
- data/spec/spec_helper.rb +14 -0
- metadata +191 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: e0874352084fa0f25f93070346e2fbe698c120d8
|
4
|
+
data.tar.gz: fc304746e61dc9f077ee0467b25aabd1f633fbfa
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: f639a3397dc6b5b53d50a9a1326da6cb1a5658ff007982bff6744394a39e2142d1e6d7e6f3f2fbc3f69b3dbc2c942536e716fca1e547048773fcc51ca3249ed5
|
7
|
+
data.tar.gz: a6948cb2478101635fe93a78de2446afc0478cbe6a4e1ac659495ada868418c10e84e33f2f6e638f1deb4f414e16525a5bb4c7755419ca5883f3cadc9601f278
|
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/CHANGELOG.md
ADDED
File without changes
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 Dmitry Kovalevsky
|
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 @@
|
|
1
|
+
## ponominalu [![Build Status](https://travis-ci.org/crossaidi/ponominalu.svg?branch=master)](https://travis-ci.org/crossaidi/ponominalu) [![Code Climate](https://codeclimate.com/github/crossaidi/ponominalu.png)](https://codeclimate.com/github/crossaidi/ponominalu) [![Dependency Status](https://gemnasium.com/crossaidi/ponominalu.png)](https://gemnasium.com/crossaidi/ponominalu.png)
|
data/Rakefile
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
require 'rake'
|
2
|
+
require 'bundler/gem_tasks'
|
3
|
+
|
4
|
+
require 'rspec/core'
|
5
|
+
require 'rspec/core/rake_task'
|
6
|
+
|
7
|
+
desc 'Turn on the console with preloaded ponominalu'
|
8
|
+
task :debug do
|
9
|
+
sh 'pry -I ./lib -r ./lib/ponominalu'
|
10
|
+
end
|
11
|
+
|
12
|
+
RSpec::Core::RakeTask.new(:spec) do |spec|
|
13
|
+
spec.pattern = FileList['spec/**/*_spec.rb']
|
14
|
+
end
|
15
|
+
|
16
|
+
task :default => :spec
|
@@ -0,0 +1,9 @@
|
|
1
|
+
# A rails generator `ponominalu:install`. It creates a config file in `config/initializers/ponominalu.rb`.
|
2
|
+
class Ponominalu::InstallGenerator < Rails::Generators::Base
|
3
|
+
source_root File.expand_path('../templates', __FILE__)
|
4
|
+
|
5
|
+
# Creates the config file.
|
6
|
+
def create_initializer
|
7
|
+
copy_file 'initializer.rb', 'config/initializers/ponominalu.rb'
|
8
|
+
end
|
9
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
Ponominalu.configure do |config|
|
2
|
+
# config.session = '123'
|
3
|
+
|
4
|
+
# Faraday adapter to make requests with:
|
5
|
+
# config.adapter = :net_http
|
6
|
+
|
7
|
+
# HTTP verb for API methods (:get or :post)
|
8
|
+
# config.http_verb = :get
|
9
|
+
|
10
|
+
# Logging parameters:
|
11
|
+
# log everything through the rails logger
|
12
|
+
config.logger = Rails.logger
|
13
|
+
|
14
|
+
# log requests' URLs
|
15
|
+
# config.log_requests = true
|
16
|
+
|
17
|
+
# log response JSON after errors
|
18
|
+
# config.log_errors = true
|
19
|
+
|
20
|
+
# log response JSON after successful responses
|
21
|
+
# config.log_responses = false
|
22
|
+
end
|
data/lib/ponominalu.rb
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
require 'faraday'
|
2
|
+
require 'faraday_middleware'
|
3
|
+
require 'yaml'
|
4
|
+
require 'hashie'
|
5
|
+
require 'oj'
|
6
|
+
|
7
|
+
require 'ponominalu/configuration'
|
8
|
+
require 'ponominalu/helpers'
|
9
|
+
require 'ponominalu/middleware'
|
10
|
+
require 'ponominalu/api'
|
11
|
+
require 'ponominalu/response'
|
12
|
+
require 'ponominalu/error'
|
13
|
+
|
14
|
+
module Ponominalu
|
15
|
+
extend Configuration
|
16
|
+
|
17
|
+
class << self
|
18
|
+
def method_missing(method, *args, &block)
|
19
|
+
API.call_method(method, *args, &block)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
# Register alias
|
24
|
+
Object.const_set(:Pn, Ponominalu)
|
25
|
+
end
|
@@ -0,0 +1,48 @@
|
|
1
|
+
module Ponominalu
|
2
|
+
module API
|
3
|
+
# Base part of Ponominalu API endpoint url.
|
4
|
+
BASE_URL = 'http://api.cultserv.ru/jtransport'.freeze
|
5
|
+
|
6
|
+
class << self
|
7
|
+
# API method call.
|
8
|
+
# @param [String] method_name A name of the method.
|
9
|
+
# @param [Hash] args Method arguments.
|
10
|
+
# @return [Hashie::Mash] Mashed server response.
|
11
|
+
def call_method(method_name, args = {}, &block)
|
12
|
+
method_name_str = method_name.to_s
|
13
|
+
url = create_url(method_name_str)
|
14
|
+
args = Helpers.flatten(args)
|
15
|
+
|
16
|
+
response = connection(url).send(Ponominalu.http_verb,
|
17
|
+
method_name_str, args).body
|
18
|
+
Response.process(response, block)
|
19
|
+
end
|
20
|
+
|
21
|
+
# Faraday connection.
|
22
|
+
# @param [String] url Connection URL (either full or just prefix).
|
23
|
+
# @return [Faraday::Connection] Created connection.
|
24
|
+
def connection(url)
|
25
|
+
Faraday.new(url, Ponominalu.faraday_options) do |faraday|
|
26
|
+
faraday.request :multipart
|
27
|
+
faraday.request :url_encoded
|
28
|
+
faraday.request :retry, Ponominalu.max_retries
|
29
|
+
faraday.response :ponominalu
|
30
|
+
faraday.adapter Ponominalu.adapter
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
private
|
35
|
+
# Creates a complete url from prefixes and the method name
|
36
|
+
# @param [String] method_name A name of the method.
|
37
|
+
# @return [String] url
|
38
|
+
def create_url(method_name)
|
39
|
+
filename = File.expand_path('../simple_methods.yml', __FILE__)
|
40
|
+
simple_methods = YAML.load_file(filename)
|
41
|
+
url_prefix = simple_methods.include?(method_name) ? '/simple/' :
|
42
|
+
'/partner/'
|
43
|
+
BASE_URL + url_prefix
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
@@ -0,0 +1,60 @@
|
|
1
|
+
require 'logger'
|
2
|
+
|
3
|
+
module Ponominalu
|
4
|
+
module Configuration
|
5
|
+
# Default global options
|
6
|
+
DEFAULT_OPTIONS = {
|
7
|
+
adapter: Faraday.default_adapter,
|
8
|
+
session: '123',
|
9
|
+
max_retries: 2,
|
10
|
+
empty_strict: false,
|
11
|
+
raw_json: false,
|
12
|
+
logger: ::Logger.new(STDOUT),
|
13
|
+
log_requests: true,
|
14
|
+
log_responses: false,
|
15
|
+
wrap_response: false,
|
16
|
+
http_verb: :post,
|
17
|
+
faraday_options: {}
|
18
|
+
}.freeze
|
19
|
+
|
20
|
+
attr_accessor *DEFAULT_OPTIONS.keys
|
21
|
+
|
22
|
+
alias_method :log_requests?, :log_requests
|
23
|
+
alias_method :log_responses?, :log_responses
|
24
|
+
|
25
|
+
# A global configuration set via the block or hash.
|
26
|
+
# @param [Hash] options Hash of options
|
27
|
+
# @example
|
28
|
+
# Ponominalu.configure do |config|
|
29
|
+
# config.adapter = :net_http
|
30
|
+
# config.logger = Rails.logger
|
31
|
+
# end
|
32
|
+
def configure(options={})
|
33
|
+
configure_by_hash(options) unless options.empty?
|
34
|
+
yield self if block_given?
|
35
|
+
self
|
36
|
+
end
|
37
|
+
|
38
|
+
# Reset configuration options to default values.
|
39
|
+
def reset
|
40
|
+
DEFAULT_OPTIONS.each do |k, v|
|
41
|
+
send("#{k}=", v)
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
# Set configuration options to their default values,
|
46
|
+
# when this module is extended.
|
47
|
+
def self.extended(base)
|
48
|
+
base.reset
|
49
|
+
end
|
50
|
+
|
51
|
+
private
|
52
|
+
# Configures global options via hash
|
53
|
+
# @param [Hash] options Hash of options
|
54
|
+
def configure_by_hash(options)
|
55
|
+
DEFAULT_OPTIONS.keys.each do |k|
|
56
|
+
send("#{k}=", options[k])
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
module Ponominalu
|
2
|
+
# An exception raised by `Ponominalu::Response` when given a response with an error.
|
3
|
+
class Error < StandardError
|
4
|
+
# An error code.
|
5
|
+
# @return [Fixnum]
|
6
|
+
attr_reader :error_code
|
7
|
+
# An exception is initialized by the data from response mash.
|
8
|
+
# @param [Hash] data Error data.
|
9
|
+
def initialize(data)
|
10
|
+
@error_code = data.code
|
11
|
+
@error_msg = data.message
|
12
|
+
@method_name = data.method_name
|
13
|
+
@session = data.session
|
14
|
+
@params = data.params
|
15
|
+
end
|
16
|
+
|
17
|
+
# A full description of the error.
|
18
|
+
# @return [String]
|
19
|
+
def message
|
20
|
+
message = "Ponominalu returned an error #{@error_code}: '#{@error_msg}'"\
|
21
|
+
" after calling method '#{@method_name}'"
|
22
|
+
|
23
|
+
if (@params.empty?)
|
24
|
+
message << " without parameters."
|
25
|
+
else
|
26
|
+
message << " with parameters #{@params.inspect}."
|
27
|
+
end
|
28
|
+
message << " App session is '#{@session}'."
|
29
|
+
message
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
module Ponominalu
|
2
|
+
module Helpers
|
3
|
+
class << self
|
4
|
+
# Flattens enumerable values of user param
|
5
|
+
# (for example "exclude")
|
6
|
+
# @param [Hash] Hash of unflatten arguments
|
7
|
+
# @return [Hash] Result hash
|
8
|
+
def flatten(args)
|
9
|
+
args.inject({}) do |hash, (k, v)|
|
10
|
+
hash[k] = v.respond_to?(:join) ? v.join(',') : v
|
11
|
+
hash
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
# Converts params string to hash
|
16
|
+
# @param [String] Part of the request url with user params
|
17
|
+
# @return [Hash] Hash of user params
|
18
|
+
def parse_params(params_str)
|
19
|
+
params_str.split('&').inject({}) do |hash, part|
|
20
|
+
key_value = part.split('=')
|
21
|
+
hash[key_value.first] = key_value.last
|
22
|
+
hash
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,58 @@
|
|
1
|
+
module Ponominalu
|
2
|
+
# Faraday middleware for a passing the session param to the request
|
3
|
+
# and config data to the response under the hood.
|
4
|
+
# Also it handles errors
|
5
|
+
class Middleware < Faraday::Response::Middleware
|
6
|
+
# Passes the session param
|
7
|
+
# @param [Hash] env Request data.
|
8
|
+
def initialize(app)
|
9
|
+
super(app)
|
10
|
+
@session = Ponominalu.session
|
11
|
+
@logger = Ponominalu.logger
|
12
|
+
end
|
13
|
+
|
14
|
+
def call(env)
|
15
|
+
# Parse the params and the method name from request body
|
16
|
+
@method_name = env.url.to_s.split('/').last
|
17
|
+
@params = Helpers.parse_params(env.body)
|
18
|
+
|
19
|
+
if Ponominalu.log_requests?
|
20
|
+
@logger.debug "Ponominalu: #{@method_name.upcase} #{env.url.to_s}"
|
21
|
+
@logger.debug "session: #{@session} params: #{@params}"
|
22
|
+
end
|
23
|
+
|
24
|
+
# Add the session to the user request params
|
25
|
+
env.body << "&session=#{@session}"
|
26
|
+
super
|
27
|
+
end
|
28
|
+
|
29
|
+
# Passes the user params and handle request errors
|
30
|
+
# @param [Hash] env Response data.
|
31
|
+
def on_complete(env)
|
32
|
+
if env.status != 200
|
33
|
+
@logger.error "Request failed with status code #{env.status}."
|
34
|
+
raise "Request failed with status code #{env.status}."
|
35
|
+
end
|
36
|
+
|
37
|
+
config_data = {
|
38
|
+
method_name: @method_name,
|
39
|
+
params: @params,
|
40
|
+
session: @session
|
41
|
+
}
|
42
|
+
|
43
|
+
env.body = Hashie::Mash.new(Oj.load(env.body).merge(config_data))
|
44
|
+
|
45
|
+
if env.body.code.zero? && !Ponominalu.empty_strict
|
46
|
+
@logger.warn 'Nothing was found. Result is empty.'
|
47
|
+
elsif env.body.code < 1
|
48
|
+
@logger.error "#{env.body.code}: #{env.body.message}."
|
49
|
+
raise Ponominalu::Error.new(env.body)
|
50
|
+
else
|
51
|
+
@logger.debug "body: #{env.body}" if Ponominalu.log_responses?
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
Faraday::Response.register_middleware ponominalu:
|
58
|
+
Ponominalu::Middleware
|
@@ -0,0 +1,39 @@
|
|
1
|
+
module Ponominalu
|
2
|
+
module Response
|
3
|
+
class << self
|
4
|
+
# The main method result processing.
|
5
|
+
# @param [Hashie::Mash] response The server response in mash format.
|
6
|
+
# @param [Proc] block A block passed to the API method.
|
7
|
+
# @return [Array, Hashie::Mash] The processed result.
|
8
|
+
# @raise [Ponominalu::Error] raised when Ponominalu returns
|
9
|
+
# an error response.
|
10
|
+
def process(response, block)
|
11
|
+
result = get_result(response)
|
12
|
+
result = Oj.dump(result) if Ponominalu.raw_json
|
13
|
+
|
14
|
+
if result.respond_to?(:each)
|
15
|
+
# enumerable result receives :map with a block when called
|
16
|
+
# with a block or is returned untouched otherwise
|
17
|
+
block.nil? ? result : result.map(&block)
|
18
|
+
else
|
19
|
+
# non-enumerable result is yielded if block_given?)
|
20
|
+
block.nil? ? result : block.call(result)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
private
|
25
|
+
def get_result(response)
|
26
|
+
# an empty array is returned if error code is 0 (not found)
|
27
|
+
# and empty_strict option is false
|
28
|
+
if response.code.zero? && !Ponominalu.empty_strict
|
29
|
+
[]
|
30
|
+
else
|
31
|
+
# if wrap_response option is true response is returned
|
32
|
+
# as element of response wrapper with api status code
|
33
|
+
# and params
|
34
|
+
Ponominalu.wrap_response ? response : response.message
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
data/ponominalu.gemspec
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
$: << File.expand_path('../lib', __FILE__)
|
3
|
+
require 'ponominalu/version'
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = 'ponominalu'
|
7
|
+
s.version = Ponominalu::VERSION
|
8
|
+
s.summary = "Ponominalu gem"
|
9
|
+
s.description = "Ponominalu API Ruby wrapper"
|
10
|
+
s.authors = ["Dmitry Kovalevsky"]
|
11
|
+
s.email = 'crossonrails@gmail.com'
|
12
|
+
s.files = ["lib/ponominalu.rb"]
|
13
|
+
s.homepage = 'https://github.com/crossaidi/ponominalu'
|
14
|
+
s.license = 'MIT'
|
15
|
+
|
16
|
+
s.files = `git ls-files`.split($/)
|
17
|
+
s.executables = s.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
s.test_files = s.files.grep(%r{^(test|spec|features)/})
|
19
|
+
s.require_paths = ['lib']
|
20
|
+
|
21
|
+
s.required_ruby_version = '>= 1.9.2'
|
22
|
+
|
23
|
+
s.add_runtime_dependency 'faraday', '~> 0.8'
|
24
|
+
s.add_runtime_dependency 'faraday_middleware', '~> 0.8'
|
25
|
+
s.add_runtime_dependency 'hashie', '~> 2.0'
|
26
|
+
s.add_runtime_dependency 'oj', '~> 2.8'
|
27
|
+
|
28
|
+
s.add_development_dependency 'bundler', '~> 1.3'
|
29
|
+
s.add_development_dependency 'rake'
|
30
|
+
s.add_development_dependency 'pry'
|
31
|
+
s.add_development_dependency 'rspec'
|
32
|
+
end
|
@@ -0,0 +1,58 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Ponominalu::API do
|
4
|
+
let(:connection) do
|
5
|
+
Faraday.new do |builder|
|
6
|
+
builder.adapter :test do |stub|
|
7
|
+
stub.post('/api_method') do
|
8
|
+
[200, {}, double('Response')]
|
9
|
+
end
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
describe ".call_method" do
|
15
|
+
|
16
|
+
before(:each) do
|
17
|
+
subject.stub(:connection).and_return(connection)
|
18
|
+
end
|
19
|
+
|
20
|
+
it "calls a connection" do
|
21
|
+
Ponominalu::Response.stub(:process)
|
22
|
+
expect(subject).to receive(:connection)
|
23
|
+
subject.call_method('api_method', { some: :params })
|
24
|
+
end
|
25
|
+
|
26
|
+
it "returns the response body" do
|
27
|
+
response = connection.send(Ponominalu.http_verb, 'api_method', {}).body
|
28
|
+
Ponominalu::Response.stub(:process).and_return(response)
|
29
|
+
|
30
|
+
expect(subject.call_method('api_method')).to eq(response)
|
31
|
+
end
|
32
|
+
|
33
|
+
it "gets an HTTP verb from Ponominalu.http_verb" do
|
34
|
+
Ponominalu::Response.stub(:process)
|
35
|
+
http_verb = double("HTTP verb")
|
36
|
+
Ponominalu.http_verb = http_verb
|
37
|
+
|
38
|
+
response = double("Response", body: double)
|
39
|
+
expect(connection).to receive(:send).with(http_verb, 'api_method', {}).and_return(response)
|
40
|
+
subject.call_method('api_method')
|
41
|
+
end
|
42
|
+
|
43
|
+
after(:each) do
|
44
|
+
Ponominalu.reset
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
describe '.connection' do
|
49
|
+
let(:url) { double('URL') }
|
50
|
+
let(:faraday_options) { double('Faraday options') }
|
51
|
+
|
52
|
+
it 'initiliazes new Faraday connection' do
|
53
|
+
Ponominalu.stub(:faraday_options).and_return(faraday_options)
|
54
|
+
expect(Faraday).to receive(:new).with(url, faraday_options)
|
55
|
+
subject.connection(url)
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
@@ -0,0 +1,55 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Ponominalu::Configuration do
|
4
|
+
|
5
|
+
class ConfigurationTest
|
6
|
+
extend Ponominalu::Configuration
|
7
|
+
end
|
8
|
+
|
9
|
+
subject { ConfigurationTest }
|
10
|
+
|
11
|
+
describe '.configure' do
|
12
|
+
let(:options) { { session: '321', empty_strict: true, max_retries: 4 } }
|
13
|
+
|
14
|
+
it 'configured by passed hash' do
|
15
|
+
subject.configure(options)
|
16
|
+
expect(subject.session).to eq('321')
|
17
|
+
expect(subject.empty_strict).to be_true
|
18
|
+
expect(subject.max_retries).to eq(4)
|
19
|
+
end
|
20
|
+
|
21
|
+
it 'yields self' do
|
22
|
+
expect(subject).to receive(:option)
|
23
|
+
subject.configure { |pn| pn.option }
|
24
|
+
end
|
25
|
+
|
26
|
+
it 'returns self' do
|
27
|
+
expect(subject.configure).to eq(subject)
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
it 'defines log params predicates as aliases' do
|
32
|
+
expect { subject.log_requests = false }.to change { subject.log_requests? }
|
33
|
+
.from(true).to(false)
|
34
|
+
expect { subject.log_responses = true }.to change { subject.log_responses? }
|
35
|
+
.from(false).to(true)
|
36
|
+
end
|
37
|
+
|
38
|
+
describe '.reset' do
|
39
|
+
it 'resets options to default values' do
|
40
|
+
subject.reset
|
41
|
+
|
42
|
+
expect(subject.adapter).to eq(:net_http)
|
43
|
+
expect(subject.logger).to be_instance_of(::Logger)
|
44
|
+
expect(subject.session).to eq('123')
|
45
|
+
expect(subject.max_retries).to eq(2)
|
46
|
+
expect(subject.empty_strict).to be_false
|
47
|
+
expect(subject.raw_json).to be_false
|
48
|
+
expect(subject.log_requests).to be_true
|
49
|
+
expect(subject.log_responses).to be_false
|
50
|
+
expect(subject.wrap_response).to be_false
|
51
|
+
expect(subject.http_verb).to eq(:post)
|
52
|
+
expect(subject.faraday_options).to eq({})
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Ponominalu::Error do
|
4
|
+
let(:error_data) do
|
5
|
+
Hashie::Mash.new(
|
6
|
+
{
|
7
|
+
code: -200,
|
8
|
+
message: 'Test error',
|
9
|
+
method_name: 'The method name',
|
10
|
+
session: '123'
|
11
|
+
}
|
12
|
+
)
|
13
|
+
end
|
14
|
+
|
15
|
+
describe '#message' do
|
16
|
+
let(:error) { Ponominalu::Error.new(error_data) }
|
17
|
+
|
18
|
+
it 'includes params message when params are passed' do
|
19
|
+
error_data.params = Hashie::Mash.new({alias: 'alias'})
|
20
|
+
|
21
|
+
message = "Ponominalu returned an error -200: 'Test error'"\
|
22
|
+
" after calling method 'The method name'"
|
23
|
+
message << " with parameters #{error_data.params.inspect}."
|
24
|
+
message << " App session is '123'."
|
25
|
+
|
26
|
+
expect(error.message).to eq(message)
|
27
|
+
end
|
28
|
+
|
29
|
+
it 'does not includes params message when params are not passed' do
|
30
|
+
error_data.params = []
|
31
|
+
|
32
|
+
message = "Ponominalu returned an error -200: 'Test error'"\
|
33
|
+
" after calling method 'The method name'"
|
34
|
+
message << " without parameters."
|
35
|
+
message << " App session is '123'."
|
36
|
+
|
37
|
+
expect(error.message).to eq(message)
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Ponominalu::Helpers do
|
4
|
+
|
5
|
+
describe '.flatten' do
|
6
|
+
let(:args) { { str_arg: 'string', array_arg: ['str1', 'str2'] } }
|
7
|
+
|
8
|
+
it 'leaves flat arguments untouched and flats a collection' do
|
9
|
+
expect(subject.flatten(args))
|
10
|
+
.to eq({ str_arg: 'string', array_arg: 'str1,str2' })
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
describe '.parse_params' do
|
15
|
+
let(:param_str) { 'alias=something&id=123&exclude=venue' }
|
16
|
+
|
17
|
+
it 'correctly parses the params' do
|
18
|
+
expect(subject.parse_params(param_str))
|
19
|
+
.to eq({ 'alias' => 'something', 'id' => '123', 'exclude' => 'venue'})
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,112 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Ponominalu::Middleware do
|
4
|
+
let(:logger) { double("Logger").as_null_object }
|
5
|
+
let(:app) { double("App").as_null_object }
|
6
|
+
let(:url) { double('URL', to_s: Ponominalu::API::BASE_URL + '/test_method' ) }
|
7
|
+
|
8
|
+
let(:env_request) { double('Env object', body: 'param_1=1¶m_2=2',
|
9
|
+
url: url, status: 200) }
|
10
|
+
|
11
|
+
let(:env_success) { double('Env success object',
|
12
|
+
body: double('Body', code: 1) , status: 200) }
|
13
|
+
|
14
|
+
let(:env_empty) { double('Env empty object', body: double('Body', code: 0),
|
15
|
+
status: 200) }
|
16
|
+
|
17
|
+
let(:api_error) { double('API error body', code: -200, message: 'API error',
|
18
|
+
method_name: 'test_method', session: Ponominalu.session, params: {} ) }
|
19
|
+
|
20
|
+
let(:env_api_error) { double('Env api error object', body: api_error,
|
21
|
+
status: 200) }
|
22
|
+
|
23
|
+
let(:env_fail) { double('Env fail object', body: double('Body', code: 1),
|
24
|
+
status: 404) }
|
25
|
+
|
26
|
+
before(:each) do
|
27
|
+
Ponominalu.logger = logger
|
28
|
+
Ponominalu.session = 'test_session'
|
29
|
+
Ponominalu.log_requests = false
|
30
|
+
Ponominalu.log_responses = false
|
31
|
+
end
|
32
|
+
|
33
|
+
subject { Ponominalu::Middleware.new(app) }
|
34
|
+
|
35
|
+
context 'when request is processing' do
|
36
|
+
it 'calls debug method on the logger with correct messages' do
|
37
|
+
Ponominalu.log_requests = true
|
38
|
+
expect(logger).to receive(:debug)
|
39
|
+
.with('Ponominalu: TEST_METHOD '\
|
40
|
+
'http://api.cultserv.ru/jtransport/test_method')
|
41
|
+
expect(logger).to receive(:debug)
|
42
|
+
.with('session: test_session params: {"param_1"=>"1", "param_2"=>"2"}')
|
43
|
+
subject.call(env_request)
|
44
|
+
end
|
45
|
+
|
46
|
+
it 'adds the session param to the request body' do
|
47
|
+
expect(env_request.body).to receive(:<<)
|
48
|
+
.with('&session=test_session')
|
49
|
+
subject.call(env_request)
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
context 'when response is processing' do
|
54
|
+
before(:each) do
|
55
|
+
Oj.stub(:load).and_return({})
|
56
|
+
end
|
57
|
+
|
58
|
+
context '(logging)' do
|
59
|
+
before(:each) { subject.stub(:raise) }
|
60
|
+
|
61
|
+
it 'calls warn method on the logger if result is empty' do
|
62
|
+
env_empty.stub(:body=)
|
63
|
+
|
64
|
+
expect(logger).to receive(:warn)
|
65
|
+
.with('Nothing was found. Result is empty.')
|
66
|
+
subject.on_complete(env_empty)
|
67
|
+
end
|
68
|
+
|
69
|
+
it 'calls error method on the logger if API error is returned' do
|
70
|
+
env_api_error.stub(:body=)
|
71
|
+
|
72
|
+
expect(logger).to receive(:error)
|
73
|
+
.with('-200: API error.')
|
74
|
+
subject.on_complete(env_api_error)
|
75
|
+
end
|
76
|
+
|
77
|
+
it 'calls error method on the logger if API error is returned' do
|
78
|
+
env_fail.stub(:body=)
|
79
|
+
|
80
|
+
expect(logger).to receive(:error)
|
81
|
+
.with('Request failed with status code 404.')
|
82
|
+
subject.on_complete(env_fail)
|
83
|
+
end
|
84
|
+
|
85
|
+
it 'calls debug method on the logger if log_responses is true' do
|
86
|
+
env_success.stub(:body=)
|
87
|
+
|
88
|
+
Ponominalu.log_responses = true
|
89
|
+
|
90
|
+
expect(logger).to receive(:debug)
|
91
|
+
.with("body: #{env_success.body}")
|
92
|
+
subject.on_complete(env_success)
|
93
|
+
end
|
94
|
+
end
|
95
|
+
|
96
|
+
context '(error raising)' do
|
97
|
+
it 'raises runtime error if response status is not 200' do
|
98
|
+
env_fail.stub(:body=)
|
99
|
+
|
100
|
+
expect { subject.on_complete(env_fail) }.to \
|
101
|
+
raise_error(RuntimeError, 'Request failed with status code 404.')
|
102
|
+
end
|
103
|
+
|
104
|
+
it 'raises Ponominalu::Error if response code is negative number' do
|
105
|
+
env_api_error.stub(:body=)
|
106
|
+
|
107
|
+
expect { subject.on_complete(env_api_error) }.to \
|
108
|
+
raise_error(Ponominalu::Error)
|
109
|
+
end
|
110
|
+
end
|
111
|
+
end
|
112
|
+
end
|
@@ -0,0 +1,58 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Ponominalu::Response do
|
4
|
+
let(:block) { proc(&:method) }
|
5
|
+
let(:data_enum) { double('Enumerable data', each: block ) }
|
6
|
+
let(:response_full_enum) { double('Full response', code: 1, message: data_enum) }
|
7
|
+
|
8
|
+
before { Ponominalu.reset }
|
9
|
+
|
10
|
+
describe '.process' do
|
11
|
+
it 'returns a string if the raw_json option is true' do
|
12
|
+
Ponominalu.raw_json = true
|
13
|
+
expect(Oj).to receive(:dump).with(data_enum)
|
14
|
+
subject.process(response_full_enum, nil)
|
15
|
+
end
|
16
|
+
|
17
|
+
context 'when block given' do
|
18
|
+
let(:data_non_enum) { double('Non-enumerable data') }
|
19
|
+
let(:response_full_non_enum) { double('Full response', code: 1, message: data_non_enum) }
|
20
|
+
|
21
|
+
it 'receives :map with a block if it is collection' do
|
22
|
+
subject.stub(:get_result).and_return(data_enum)
|
23
|
+
expect(data_enum).to receive(:map)
|
24
|
+
subject.process(response_full_enum, block)
|
25
|
+
end
|
26
|
+
|
27
|
+
it 'is yielded if result is non-enumerable' do
|
28
|
+
subject.stub(:get_result).and_return(data_non_enum)
|
29
|
+
expect(block).to receive(:call).with(data_non_enum)
|
30
|
+
subject.process(response_full_non_enum, block)
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
describe '.get_result' do
|
36
|
+
let(:response_empty) { double('Empty response', code: 0, message: 'empty') }
|
37
|
+
|
38
|
+
it 'returns an empty array if a result is not found' do
|
39
|
+
expect(subject.send(:get_result, response_empty)).to eq([])
|
40
|
+
end
|
41
|
+
|
42
|
+
it 'does not return an empty array if empty_strict is true' do
|
43
|
+
Ponominalu.empty_strict = true
|
44
|
+
expect(subject.send(:get_result, response_empty)).not_to eq([])
|
45
|
+
end
|
46
|
+
|
47
|
+
it 'returns a result if it is not empty' do
|
48
|
+
expect(subject.send(:get_result, response_full_enum))
|
49
|
+
.to eq(response_full_enum.message)
|
50
|
+
end
|
51
|
+
|
52
|
+
it 'returns a result in a wrapper if the wrap_response option is true' do
|
53
|
+
Ponominalu.wrap_response = true
|
54
|
+
expect(subject.send(:get_result, response_full_enum))
|
55
|
+
.to eq(response_full_enum)
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Ponominalu do
|
4
|
+
context 'when short alias is registered' do
|
5
|
+
it 'automatically registers the short alias' do
|
6
|
+
expect(subject).to eq(Pn)
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
10
|
+
context 'when delegating to API module' do
|
11
|
+
it 'delegates methods to API.call_method' do
|
12
|
+
expect(subject::API).to receive(:call_method)
|
13
|
+
.with(:test_method)
|
14
|
+
subject.test_method
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
require 'ponominalu'
|
2
|
+
|
3
|
+
# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
|
4
|
+
# Require this file using `require "spec_helper"` to ensure that it is only
|
5
|
+
# loaded once.
|
6
|
+
#
|
7
|
+
# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
|
8
|
+
RSpec.configure do |config|
|
9
|
+
config.treat_symbols_as_metadata_keys_with_true_values = true
|
10
|
+
config.run_all_when_everything_filtered = true
|
11
|
+
config.filter_run :focus
|
12
|
+
config.color = true
|
13
|
+
config.order = 'random'
|
14
|
+
end
|
metadata
ADDED
@@ -0,0 +1,191 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: ponominalu
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Dmitry Kovalevsky
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-05-14 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: faraday
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0.8'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0.8'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: faraday_middleware
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0.8'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0.8'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: hashie
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '2.0'
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '2.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: oj
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '2.8'
|
62
|
+
type: :runtime
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '2.8'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: bundler
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '1.3'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '1.3'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: rake
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - ">="
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0'
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: pry
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - ">="
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '0'
|
104
|
+
type: :development
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - ">="
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '0'
|
111
|
+
- !ruby/object:Gem::Dependency
|
112
|
+
name: rspec
|
113
|
+
requirement: !ruby/object:Gem::Requirement
|
114
|
+
requirements:
|
115
|
+
- - ">="
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: '0'
|
118
|
+
type: :development
|
119
|
+
prerelease: false
|
120
|
+
version_requirements: !ruby/object:Gem::Requirement
|
121
|
+
requirements:
|
122
|
+
- - ">="
|
123
|
+
- !ruby/object:Gem::Version
|
124
|
+
version: '0'
|
125
|
+
description: Ponominalu API Ruby wrapper
|
126
|
+
email: crossonrails@gmail.com
|
127
|
+
executables: []
|
128
|
+
extensions: []
|
129
|
+
extra_rdoc_files: []
|
130
|
+
files:
|
131
|
+
- ".gitignore"
|
132
|
+
- ".travis.yml"
|
133
|
+
- CHANGELOG.md
|
134
|
+
- Gemfile
|
135
|
+
- LICENSE.txt
|
136
|
+
- README.md
|
137
|
+
- Rakefile
|
138
|
+
- lib/generators/ponominalu/install/USAGE
|
139
|
+
- lib/generators/ponominalu/install/install_generator.rb
|
140
|
+
- lib/generators/ponominalu/install/templates/initializer.rb
|
141
|
+
- lib/ponominalu.rb
|
142
|
+
- lib/ponominalu/api.rb
|
143
|
+
- lib/ponominalu/configuration.rb
|
144
|
+
- lib/ponominalu/error.rb
|
145
|
+
- lib/ponominalu/helpers.rb
|
146
|
+
- lib/ponominalu/middleware.rb
|
147
|
+
- lib/ponominalu/response.rb
|
148
|
+
- lib/ponominalu/simple_methods.yml
|
149
|
+
- lib/ponominalu/version.rb
|
150
|
+
- ponominalu.gemspec
|
151
|
+
- spec/ponominalu/api_spec.rb
|
152
|
+
- spec/ponominalu/configuration_spec.rb
|
153
|
+
- spec/ponominalu/error_spec.rb
|
154
|
+
- spec/ponominalu/helpers_spec.rb
|
155
|
+
- spec/ponominalu/middleware_spec.rb
|
156
|
+
- spec/ponominalu/response_spec.rb
|
157
|
+
- spec/ponominalu_spec.rb
|
158
|
+
- spec/spec_helper.rb
|
159
|
+
homepage: https://github.com/crossaidi/ponominalu
|
160
|
+
licenses:
|
161
|
+
- MIT
|
162
|
+
metadata: {}
|
163
|
+
post_install_message:
|
164
|
+
rdoc_options: []
|
165
|
+
require_paths:
|
166
|
+
- lib
|
167
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
168
|
+
requirements:
|
169
|
+
- - ">="
|
170
|
+
- !ruby/object:Gem::Version
|
171
|
+
version: 1.9.2
|
172
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
173
|
+
requirements:
|
174
|
+
- - ">="
|
175
|
+
- !ruby/object:Gem::Version
|
176
|
+
version: '0'
|
177
|
+
requirements: []
|
178
|
+
rubyforge_project:
|
179
|
+
rubygems_version: 2.2.2
|
180
|
+
signing_key:
|
181
|
+
specification_version: 4
|
182
|
+
summary: Ponominalu gem
|
183
|
+
test_files:
|
184
|
+
- spec/ponominalu/api_spec.rb
|
185
|
+
- spec/ponominalu/configuration_spec.rb
|
186
|
+
- spec/ponominalu/error_spec.rb
|
187
|
+
- spec/ponominalu/helpers_spec.rb
|
188
|
+
- spec/ponominalu/middleware_spec.rb
|
189
|
+
- spec/ponominalu/response_spec.rb
|
190
|
+
- spec/ponominalu_spec.rb
|
191
|
+
- spec/spec_helper.rb
|