Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bump rack from 1.6.4 to 1.6.13 #6

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
GIT
remote: git://github.com/pivotal/sinclair.git
remote: https://github.com/pivotal/sinclair.git
revision: b8c88158d000e75c6b8296a96e2dfe6da82ed823
specs:
sinclair (0.2.0)
Expand Down Expand Up @@ -219,7 +219,7 @@ GEM
http_parser.rb (~> 0.6.0)
multi_json
puma (2.16.0)
rack (1.6.4)
rack (1.6.13)
rack-cors (0.4.0)
rack-test (0.6.3)
rack (>= 1.0)
Expand Down
Binary file added vendor/cache/ffi-1.9.14.gem
Binary file not shown.
Binary file added vendor/cache/rack-1.6.13.gem
Binary file not shown.
Binary file removed vendor/cache/rack-1.6.4.gem
Binary file not shown.
Binary file added vendor/cache/rubyzip-1.2.0.gem
Binary file not shown.
Binary file added vendor/cache/selenium-webdriver-2.53.4.gem
Binary file not shown.
Empty file.
8 changes: 8 additions & 0 deletions vendor/cache/sinclair-b8c88158d000/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/.bundle/
/.yardoc
/_yardoc/
/coverage/
/doc/
/pkg/
/spec/reports/
/tmp/
2 changes: 2 additions & 0 deletions vendor/cache/sinclair-b8c88158d000/.rspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
--format documentation
--color
1 change: 1 addition & 0 deletions vendor/cache/sinclair-b8c88158d000/.ruby-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
2.3.0
9 changes: 9 additions & 0 deletions vendor/cache/sinclair-b8c88158d000/.travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
language: ruby
rvm:
- 2.1.5

before_install: gem install bundler

addons:
code_climate:
repo_token: 8072f0a2d484fa3f658829f9c44ebbd4c10647800cb3a2fa0f3f7337be585050
4 changes: 4 additions & 0 deletions vendor/cache/sinclair-b8c88158d000/Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
source 'https://rubygems.org'

# Specify your gem's dependencies in sinclair.gemspec
gemspec
21 changes: 21 additions & 0 deletions vendor/cache/sinclair-b8c88158d000/LICENSE.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2015 TODO: Write your name

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
75 changes: 75 additions & 0 deletions vendor/cache/sinclair-b8c88158d000/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
# Sinclair

[![Build Status](https://travis-ci.org/pivotal/sinclair.svg)](https://travis-ci.org/pivotal/sinclair)
[![Code Climate](https://codeclimate.com/github/pivotal/sinclair/badges/gpa.svg)](https://codeclimate.com/github/pivotal/sinclair)
[![Test Coverage](https://codeclimate.com/github/pivotal/sinclair/badges/coverage.svg)](https://codeclimate.com/github/pivotal/sinclair/coverage)

![Sinclair the Dinosaur](http://www.monsterbashnews.com/Morepics/SinclairOilLogo.jpg)

Sinclair is a gem that makes using the OpenAir API tolerable.

## Usage

### Basic Usage

Create the API client using your connection parameters:

```
client = Sinclair::OpenAirApiClient.new(
username: 'Username', password: 'Password', company: 'Company', client: 'Client', key: 'APIKEY'
)
```

Create an XML ERB template containing your OpenAir request:

```
<Read type='Customer' enable_custom='1' method='all' limit='1000'>
<Customer>
<name><%= name %></name>
</Customer>
</Read>

```

Make the request:

```
template = File.read('your template file')
customers = client.send_request(template: template, model: 'Customer', locals: { name: 'Foo' })
```

Note that `model` should be the type of data returned by the API. In the above example, we are requesting `Customer` data, so the `model` would be `Customer`. The return value includes all of the XML response data that is nested under `model`.

By default, Sinclair will make a `Read` request. To change the type of request, supply a `method` argument:

```
customers = client.send_request(template: template, model: 'Customer', method: 'Add')
```

Sinclair will always return an array of items regardless of the number of items returned.

### Pagination

Sinclair will attempt to make additional requests if the number of items returned is equal to the limit supplied in the request. When this happens, Sinclair will add an `offset` parameter to the locals used to render the template. You will need to change your request template to include the `offset`.

```
<Read type='Customer' enable_custom='1' method='all' limit='<%= offset %>,1000'>
<Customer>
<name><%= name %></name>
</Customer>
</Read>
```

### Debugging

If you want to see the actual requests Sinclair is making, you can supply a logger which Sinclair will use to log both the requests and responses seen from OpenAir:

```
client.logger = Logger.new(STDOUT)
client.logger.level = Logger::DEBUG
```

## License

The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).

6 changes: 6 additions & 0 deletions vendor/cache/sinclair-b8c88158d000/Rakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
require "bundler/gem_tasks"
require "rspec/core/rake_task"

RSpec::Core::RakeTask.new(:spec)

task :default => :spec
14 changes: 14 additions & 0 deletions vendor/cache/sinclair-b8c88158d000/bin/console
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#!/usr/bin/env ruby

require "bundler/setup"
require "sinclair"

# You can add fixtures and/or initialization code here to make experimenting
# with your gem easier. You can also use a different console, if you like.

# (If you use this, don't forget to add pry to your Gemfile!)
# require "pry"
# Pry.start

require "irb"
IRB.start
7 changes: 7 additions & 0 deletions vendor/cache/sinclair-b8c88158d000/bin/setup
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/bin/bash
set -euo pipefail
IFS=$'\n\t'

bundle install

# Do any other automated setup that you need to do here
12 changes: 12 additions & 0 deletions vendor/cache/sinclair-b8c88158d000/lib/sinclair.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
require 'sinclair/version'
require 'sinclair/request'
require 'sinclair/open_air_api_client'
require 'sinclair/errors/open_air_authentication_failure'
require 'sinclair/errors/open_air_response_error'
require 'sinclair/errors/open_air_invalid_data'
require 'sinclair/errors/open_air_response_timeout'
require 'sinclair/errors/open_air_response_unrecognized'
require 'sinclair/errors/open_air_user_locked'

module Sinclair
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module Sinclair
class OpenAirAuthenticationFailure < StandardError
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module Sinclair
class OpenAirInvalidData < StandardError
attr_reader :errors

def initialize(errors)
@errors = errors
end

def message
errors.join(', ')
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module Sinclair
class OpenAirResponseError < StandardError
attr_reader :status

def initialize(status)
@status = status
end

def message
"Error making OpenAir request. Got status #{@status}."
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module Sinclair
class OpenAirResponseTimeout < StandardError
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module Sinclair
class OpenAirResponseUnrecognized < StandardError
attr_reader :response

def initialize(response)
@response = response
end

def message
"Unknown OpenAir response: #{response}"
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module Sinclair
class OpenAirUserLocked < StandardError
end
end
134 changes: 134 additions & 0 deletions vendor/cache/sinclair-b8c88158d000/lib/sinclair/open_air_api_client.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
require 'nori'
require 'faraday'
require 'nokogiri'

module Sinclair
class OpenAirApiClient
attr_accessor :logger, :last_request, :last_response

def initialize(username:, password:, company:, client:, key:, url: 'https://www.openair.com', limit: '1000', timeout: 180, open_timeout: 120)
@username = username
@password = password
@company = company
@client = client
@key = key
@url = url
@limit = limit
@timeout = timeout
@open_timeout = open_timeout
end

def send_request(template: , model: , method: 'Read', locals: {})
response = {}
response[model] = []

while true
page = process_page(template: template, method: method, model: model, locals: {offset: response[model].length}.merge(locals))
break unless page[model]
returned_models = page.keys
returned_models.each do |m|
response[m] ? response[m] += page[m] : response[m] = page[m]
end
break if page[model].length < @limit.to_i
end
response
end

private

def process_page(template:, method:, model:, locals: {})
response = make_request(locals, template)

log_request unless logger.nil?

check_auth_status(response)

read = response['response'][method]
read = [read] unless read.is_a?(Array)

check_response_status(read, model)
map_response_by_model(read)
end

def make_request(locals, template)
response = get_response(template, locals)
self.last_response = response.body
Nori.new(advanced_typecasting: false).parse(response.body)
end

def invalid_read_status(status)
status != 0 && status != 601
end

def map_response_by_model(response)
result = {}
returned_models = find_returned_models(response)
returned_models.each { |m| result[m] = [] }

response.each do |r|
model = returned_models.find{|m| r.keys.include?(m)}
result[model] << r[model]
end

result.map do |model, items|
result[model] = items.flatten.compact
end

result
end

def find_returned_models(response)
response.map do |r|
r.keys[0]
end.compact.uniq
end

def check_auth_status(response)
raise Sinclair::OpenAirResponseUnrecognized.new(response) if response['response']['Auth'].nil?

auth_status = response['response']['Auth']['@status'].to_i
raise Sinclair::OpenAirUserLocked if auth_status == 416
raise Sinclair::OpenAirAuthenticationFailure if auth_status != 0
end

def check_response_status(read, model)
statuses = read.map { |r| r['@status'].to_i }
if statuses.any? { |s| s == 1002 }
errors = read.select{|r| r['@status'].to_i == 1002}.map{|r| r[model]['errors']}
raise Sinclair::OpenAirInvalidData.new(errors)
elsif statuses.any? { |s| invalid_read_status(s) }
raise Sinclair::OpenAirResponseError.new(statuses.find { |s| invalid_read_status(s) })
end
end

def get_response(template, locals = {})
options = {request: {timeout: @timeout, open_timeout: @open_timeout}}
begin
locals = locals.merge(username: @username, password: @password, company: @company, client: @client, key: @key, limit: @limit)
Faraday.new(@url, options).post('/api.pl') do |request|
request.body = Sinclair::Request.new(template).render(locals)
request.headers.merge!({'Accept-Encoding' => 'identity'})
self.last_request = request.body
end
rescue Faraday::TimeoutError
raise Sinclair::OpenAirResponseTimeout
end
end

def log_request
logger.debug('#' * 80)
logger.debug('# REQUEST')
logger.debug('#' * 80)
logger.debug(last_request)

logger.debug('#' * 80)
logger.debug('# RESPONSE')
logger.debug('#' * 80)
logger.debug(last_response)
end

def wrap_response(response)
response.is_a?(Array) ? response : [response]
end
end
end
Loading