empireavenue 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/LICENSE.md +44 -0
- data/README.md +30 -0
- data/Rakefile +8 -0
- data/empireavenue.gemspec +27 -0
- data/lib/empireavenue/action/buy_group_spec.rb +31 -0
- data/lib/empireavenue/api/arguments.rb +13 -0
- data/lib/empireavenue/api/portfolio_base.rb +13 -0
- data/lib/empireavenue/api/portfolio_get.rb +13 -0
- data/lib/empireavenue/api/profile_bank_balance.rb +9 -0
- data/lib/empireavenue/api/profile_info.rb +11 -0
- data/lib/empireavenue/api/profile_shareholders.rb +13 -0
- data/lib/empireavenue/api/search_recent.rb +9 -0
- data/lib/empireavenue/api/shares_buy.rb +11 -0
- data/lib/empireavenue/api/shares_sell.rb +11 -0
- data/lib/empireavenue/base.rb +126 -0
- data/lib/empireavenue/basic_user.rb +10 -0
- data/lib/empireavenue/client.rb +89 -0
- data/lib/empireavenue/configurable.rb +84 -0
- data/lib/empireavenue/default.rb +97 -0
- data/lib/empireavenue/entity.rb +7 -0
- data/lib/empireavenue/error/client_error.rb +35 -0
- data/lib/empireavenue/error/configuration_error.rb +8 -0
- data/lib/empireavenue/error/decode_error.rb +9 -0
- data/lib/empireavenue/error/forbidden.rb +10 -0
- data/lib/empireavenue/error/identity_map_key_error.rb +9 -0
- data/lib/empireavenue/error/internal_server_error.rb +11 -0
- data/lib/empireavenue/error/not_found.rb +10 -0
- data/lib/empireavenue/error/server_error.rb +28 -0
- data/lib/empireavenue/error/service_unavailable.rb +11 -0
- data/lib/empireavenue/error.rb +33 -0
- data/lib/empireavenue/identity.rb +51 -0
- data/lib/empireavenue/identity_map.rb +22 -0
- data/lib/empireavenue/response/parse_json.rb +22 -0
- data/lib/empireavenue/response/raise_error.rb +31 -0
- data/lib/empireavenue/user.rb +9 -0
- data/lib/empireavenue/version.rb +18 -0
- data/lib/empireavenue.rb +32 -0
- data/spec/empireavenue/api/portfolio/portfolio_base_spec.rb +49 -0
- data/spec/empireavenue/api/portfolio/portfolio_base_spec.rb~ +49 -0
- data/spec/empireavenue/api/portfolio/portfolio_get_spec.rb +49 -0
- data/spec/empireavenue/api/portfolio/portfolio_get_spec.rb~ +39 -0
- data/spec/empireavenue/api/profile/profile_bank_balance_spec.rb +18 -0
- data/spec/empireavenue/api/profile/profile_bank_balance_spec.rb~ +18 -0
- data/spec/empireavenue/api/profile/profile_info_spec.rb +38 -0
- data/spec/empireavenue/api/profile/profile_info_spec.rb~ +40 -0
- data/spec/empireavenue/api/profile/profile_shareholders_spec.rb +50 -0
- data/spec/empireavenue/api/profile/profile_shareholders_spec.rb~ +50 -0
- data/spec/empireavenue/api/search/search_recent_spec.rb +16 -0
- data/spec/empireavenue/api/search/search_recent_spec.rb~ +16 -0
- data/spec/empireavenue/api/shares/shares_buy_spec.rb +31 -0
- data/spec/empireavenue/api/shares/shares_buy_spec.rb~ +30 -0
- data/spec/empireavenue/api/shares/shares_sell_spec.rb +31 -0
- data/spec/empireavenue/api/shares/shares_sell_spec.rb~ +31 -0
- data/spec/empireavenue/base_spec.rb +111 -0
- data/spec/empireavenue/basic_user_spec.rb +23 -0
- data/spec/empireavenue/basic_user_spec.rb~ +23 -0
- data/spec/empireavenue/client_spec.rb +149 -0
- data/spec/empireavenue/client_spec.rb~ +149 -0
- data/spec/empireavenue/error/client_error_spec.rb +27 -0
- data/spec/empireavenue/error/client_error_spec.rb~ +27 -0
- data/spec/empireavenue/error/server_error_spec.rb +20 -0
- data/spec/empireavenue/error/server_error_spec.rb~ +20 -0
- data/spec/empireavenue/error_spec.rb +20 -0
- data/spec/empireavenue/error_spec.rb~ +20 -0
- data/spec/empireavenue/identifiable_spec.rb +50 -0
- data/spec/empireavenue/identifiable_spec.rb~ +50 -0
- data/spec/factory_classes.rb +137 -0
- data/spec/factory_classes.rb~ +137 -0
- data/spec/helper.rb +53 -0
- data/spec/helper.rb~ +51 -0
- metadata +219 -0
data/LICENSE.md
ADDED
@@ -0,0 +1,44 @@
|
|
1
|
+
Copyright (c) 2013 Ralph Janke
|
2
|
+
All rights reserved
|
3
|
+
|
4
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
5
|
+
a copy of this software and associated documentation files (the
|
6
|
+
"Software"), to use this software under the AGPL 3.0 licence which can
|
7
|
+
be obtained at http://www.gnu.org/licences/agpl-3.0.html
|
8
|
+
|
9
|
+
The above copyright notice and this permission notice shall be
|
10
|
+
included in all copies or substantial portions of the Software.
|
11
|
+
|
12
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
13
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
14
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
15
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
16
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
17
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
18
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
19
|
+
|
20
|
+
|
21
|
+
Portions of this software have been reused either in unmodified or modified
|
22
|
+
form from the twitter API library (http://github.com/sferik/twitter). The
|
23
|
+
orginal form of those portions are licenced as following:
|
24
|
+
|
25
|
+
Copyright (c) 2006-2012 John Nunemaker, Wynn Netherland, Erik Michaels-Ober, Steve Richert
|
26
|
+
|
27
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
28
|
+
a copy of this software and associated documentation files (the
|
29
|
+
"Software"), to deal in the Software without restriction, including
|
30
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
31
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
32
|
+
permit persons to whom the Software is furnished to do so, subject to
|
33
|
+
the following conditions:
|
34
|
+
|
35
|
+
The above copyright notice and this permission notice shall be
|
36
|
+
included in all copies or substantial portions of the Software.
|
37
|
+
|
38
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
39
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
40
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
41
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
42
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
43
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
44
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
empireavenue
|
2
|
+
============
|
3
|
+
[![Build Status](https://travis-ci.org/txwikinger/empireavenue.png)](https://travis-ci.org/txwikinger/empireavenue)
|
4
|
+
[gem]: https://rubygems.org/gems/empireavenue
|
5
|
+
|
6
|
+
|
7
|
+
A Ruby interface to the Empire Avenue API
|
8
|
+
|
9
|
+
## Installation
|
10
|
+
gem install empireavenue
|
11
|
+
|
12
|
+
|
13
|
+
The interface can be used by calling by
|
14
|
+
|
15
|
+
```ruby
|
16
|
+
require 'empireavenue'
|
17
|
+
|
18
|
+
ea = EmpireAvenue::Client.new({client_id = <client_id of app from EAv>, oauth_token = <oauth_token received from EAv authentication>)
|
19
|
+
```
|
20
|
+
|
21
|
+
Now this client can perform requests to the Empire Avenue API
|
22
|
+
|
23
|
+
Profile Info of accounts
|
24
|
+
|
25
|
+
```ruby
|
26
|
+
ea.profile_info(ticker = nil)
|
27
|
+
```
|
28
|
+
|
29
|
+
receives the profile information of the given ticker. If no argument is given, the associated profile of the authenticated user is provided. Ticker can be a list of up to 100 tickers in an Array.
|
30
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'empireavenue/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.add_dependency 'faraday', ['~> 0.8', '< 0.10']
|
8
|
+
spec.add_dependency 'multi_json', '~> 1.0'
|
9
|
+
spec.add_dependency 'simple_oauth', '~> 0.2'
|
10
|
+
spec.add_development_dependency 'bundler', '~> 1.0'
|
11
|
+
spec.authors = ["Ralph Janke"]
|
12
|
+
#spec.cert_chain = ['public_cert.pem']
|
13
|
+
spec.description = %q{A Ruby interface to the Empire Avenue API.}
|
14
|
+
spec.email = ['txwikinger@ubuntu.com']
|
15
|
+
spec.files = %w(LICENSE.md README.md Rakefile empireavenue.gemspec)
|
16
|
+
spec.files += Dir.glob("lib/**/*.rb")
|
17
|
+
spec.files += Dir.glob("spec/**/*")
|
18
|
+
spec.homepage = 'http://txwinger.github.com/empireavenue/'
|
19
|
+
spec.licenses = ['AGPL']
|
20
|
+
spec.name = 'empireavenue'
|
21
|
+
spec.require_paths = ['lib']
|
22
|
+
spec.required_rubygems_version = '>= 1.3.6'
|
23
|
+
#spec.signing_key = File.expand_path("~/.gem/private_key.pem") if $0 =~ /gem\z/
|
24
|
+
spec.summary = spec.description
|
25
|
+
spec.test_files = Dir.glob("spec/**/*")
|
26
|
+
spec.version = EmpireAvenue::Version
|
27
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
require 'helper'
|
2
|
+
|
3
|
+
describe EmpireAvenue::API::Profile do
|
4
|
+
|
5
|
+
subject do
|
6
|
+
EmpireAvenue::Client.new(:client_id => "CK", :client_secret => "CS", :oauth_token => "OT", :oauth_token_secret => "OS")
|
7
|
+
end
|
8
|
+
|
9
|
+
before do
|
10
|
+
@ticker_list = ["AVIDBEAVER", "TXWIKINGER"]
|
11
|
+
end
|
12
|
+
|
13
|
+
it "buys all tickers in list with given share number" do
|
14
|
+
|
15
|
+
end
|
16
|
+
|
17
|
+
|
18
|
+
it "buys all tickers in list with maximum given amount" do
|
19
|
+
|
20
|
+
end
|
21
|
+
|
22
|
+
|
23
|
+
it "buys all tickers in list with closest above given amount" do
|
24
|
+
|
25
|
+
end
|
26
|
+
|
27
|
+
it "buys all tickers in list " do
|
28
|
+
|
29
|
+
end
|
30
|
+
|
31
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
module EmpireAvenue
|
2
|
+
module API
|
3
|
+
module Portfolio
|
4
|
+
def portfolio_base (ticker = nil, page = nil, maxresults = nil)
|
5
|
+
params = {}
|
6
|
+
params = params.merge({:ticker => ticker}) if ticker
|
7
|
+
params = params.merge({:page => page}) if page
|
8
|
+
params = params.merge({:maxresults => maxresults}) if maxresults
|
9
|
+
get("/portfolio/base", params)
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
module EmpireAvenue
|
2
|
+
module API
|
3
|
+
module Portfolio
|
4
|
+
def portfolio_get (ticker = nil, page = nil, maxresults = nil)
|
5
|
+
params = {}
|
6
|
+
params = params.merge({:ticker => ticker}) if ticker
|
7
|
+
params = params.merge({:page => page}) if page
|
8
|
+
params = params.merge({:maxresults => maxresults}) if maxresults
|
9
|
+
get("/portfolio/get", params)
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
module EmpireAvenue
|
2
|
+
module API
|
3
|
+
module Profile
|
4
|
+
def profile_shareholders (ticker = nil, page = nil, maxresults = nil)
|
5
|
+
params = {}
|
6
|
+
params = params.merge({:ticker => ticker}) if ticker
|
7
|
+
params = params.merge({:page => page}) if page
|
8
|
+
params = params.merge({:maxresults => maxresults}) if maxresults
|
9
|
+
get("/profile/shareholders", params)
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
@@ -0,0 +1,11 @@
|
|
1
|
+
module EmpireAvenue
|
2
|
+
module API
|
3
|
+
module Shares
|
4
|
+
def shares_buy (ticker, shares, last_trade = nil)
|
5
|
+
last_trade = profile_info(ticker)[:body][:data][0][:last_trade] unless last_trade
|
6
|
+
params = {:ticker => ticker, :shares => shares, :last_trade => last_trade}
|
7
|
+
post("/shares/buy", params)
|
8
|
+
end
|
9
|
+
end
|
10
|
+
end
|
11
|
+
end
|
@@ -0,0 +1,11 @@
|
|
1
|
+
module EmpireAvenue
|
2
|
+
module API
|
3
|
+
module Shares
|
4
|
+
def shares_sell (ticker, shares, last_trade = nil)
|
5
|
+
last_trade = profile_info(ticker)[:body][:data][0][:last_trade] unless last_trade
|
6
|
+
params = {:ticker => ticker, :shares => shares, :last_trade => last_trade}
|
7
|
+
post("/shares/sell", params)
|
8
|
+
end
|
9
|
+
end
|
10
|
+
end
|
11
|
+
end
|
@@ -0,0 +1,126 @@
|
|
1
|
+
require 'empireavenue/identity_map'
|
2
|
+
require 'empireavenue/error/identity_map_key_error'
|
3
|
+
|
4
|
+
module EmpireAvenue
|
5
|
+
class Base
|
6
|
+
# Define methods that retrieve the value from an initialized instance variable Hash, using the attribute as a key
|
7
|
+
#
|
8
|
+
# @param attrs [Array, Set, Symbol]
|
9
|
+
def self.attr_reader(*attrs)
|
10
|
+
mod = Module.new do
|
11
|
+
attrs.each do |attribute|
|
12
|
+
define_method attribute do
|
13
|
+
@attrs[attribute.to_sym]
|
14
|
+
end
|
15
|
+
define_method "#{attribute}?" do
|
16
|
+
!!@attrs[attribute.to_sym]
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
const_set(:Attributes, mod)
|
21
|
+
include mod
|
22
|
+
end
|
23
|
+
|
24
|
+
# return [EmpireAvenue::IdentityMap]
|
25
|
+
def self.identity_map
|
26
|
+
return unless EmpireAvenue.identity_map
|
27
|
+
@identity_map = EmpireAvenue.identity_map.new unless defined?(@identity_map) && @identity_map.class == EmpireAvenue.identity_map
|
28
|
+
@identity_map
|
29
|
+
end
|
30
|
+
|
31
|
+
# Retrieves an object from the identity map.
|
32
|
+
#
|
33
|
+
# @param attrs [Hash]
|
34
|
+
# @return [EmpireAvenue::Base]
|
35
|
+
def self.fetch(attrs)
|
36
|
+
return unless identity_map
|
37
|
+
if object = identity_map.fetch(Marshal.dump(attrs))
|
38
|
+
return object
|
39
|
+
end
|
40
|
+
return yield if block_given?
|
41
|
+
raise EmpireAvenue::Error::IdentityMapKeyError, "key not found"
|
42
|
+
end
|
43
|
+
|
44
|
+
# Stores an object in the identity map.
|
45
|
+
#
|
46
|
+
# @param object [Object]
|
47
|
+
# @return [EmpireAvenue::Base]
|
48
|
+
def self.store(object)
|
49
|
+
return object unless identity_map
|
50
|
+
identity_map.store(Marshal.dump(object.attrs), object)
|
51
|
+
end
|
52
|
+
|
53
|
+
# Returns a new object based on the response hash
|
54
|
+
#
|
55
|
+
# @param response [Hash]
|
56
|
+
# @return [EmpireAvenue::Base]
|
57
|
+
def self.from_response(response={})
|
58
|
+
fetch_or_new(response[:body])
|
59
|
+
end
|
60
|
+
|
61
|
+
# Retrieves an object from the identity map, or stores it in the
|
62
|
+
# identity map if it doesn't already exist.
|
63
|
+
#
|
64
|
+
# @param attrs [Hash]
|
65
|
+
# @return [EmpireAvenue::Base]
|
66
|
+
def self.fetch_or_new(attrs={})
|
67
|
+
return unless attrs
|
68
|
+
return new(attrs) unless identity_map
|
69
|
+
|
70
|
+
fetch(attrs) do
|
71
|
+
object = new(attrs)
|
72
|
+
store(object)
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
# Initializes a new object
|
77
|
+
#
|
78
|
+
# @param attrs [Hash]
|
79
|
+
# @return [EmpireAvenue::Base]
|
80
|
+
def initialize(attrs={})
|
81
|
+
@attrs = attrs
|
82
|
+
end
|
83
|
+
|
84
|
+
# Fetches an attribute of an object using hash notation
|
85
|
+
#
|
86
|
+
# @param method [String, Symbol] Message to send to the object
|
87
|
+
def [](method)
|
88
|
+
send(method.to_sym)
|
89
|
+
rescue NoMethodError
|
90
|
+
nil
|
91
|
+
end
|
92
|
+
|
93
|
+
# Retrieve the attributes of an object
|
94
|
+
#
|
95
|
+
# @return [Hash]
|
96
|
+
def attrs
|
97
|
+
@attrs
|
98
|
+
end
|
99
|
+
alias to_hash attrs
|
100
|
+
|
101
|
+
# Update the attributes of an object
|
102
|
+
#
|
103
|
+
# @param attrs [Hash]
|
104
|
+
# @return [EmpireAvenue::Base]
|
105
|
+
def update(attrs)
|
106
|
+
@attrs.update(attrs)
|
107
|
+
self
|
108
|
+
end
|
109
|
+
|
110
|
+
protected
|
111
|
+
|
112
|
+
# @param attr [Symbol]
|
113
|
+
# @param other [EmpireAvenue::Base]
|
114
|
+
# @return [Boolean]
|
115
|
+
def attr_equal(attr, other)
|
116
|
+
self.class == other.class && !other.send(attr).nil? && send(attr) == other.send(attr)
|
117
|
+
end
|
118
|
+
|
119
|
+
# @param other [EmpireAvenue::Base]
|
120
|
+
# @return [Boolean]
|
121
|
+
def attrs_equal(other)
|
122
|
+
self.class == other.class && !other.attrs.empty? && attrs == other.attrs
|
123
|
+
end
|
124
|
+
|
125
|
+
end
|
126
|
+
end
|
@@ -0,0 +1,89 @@
|
|
1
|
+
require 'faraday'
|
2
|
+
require 'multi_json'
|
3
|
+
#require 'oauth2'
|
4
|
+
require 'uri'
|
5
|
+
require 'empireavenue/default'
|
6
|
+
require 'empireavenue/api/profile_info'
|
7
|
+
require 'empireavenue/api/profile_bank_balance'
|
8
|
+
require 'empireavenue/api/profile_shareholders'
|
9
|
+
require 'empireavenue/api/portfolio_get'
|
10
|
+
require 'empireavenue/api/portfolio_base'
|
11
|
+
require 'empireavenue/api/search_recent'
|
12
|
+
require 'empireavenue/api/shares_buy'
|
13
|
+
require 'empireavenue/api/shares_sell'
|
14
|
+
require 'empireavenue/error/client_error'
|
15
|
+
require 'empireavenue/error/decode_error'
|
16
|
+
|
17
|
+
module EmpireAvenue
|
18
|
+
# Wrapper for the Empire Avenue API
|
19
|
+
#
|
20
|
+
class Client
|
21
|
+
include EmpireAvenue::API::Profile
|
22
|
+
include EmpireAvenue::API::Portfolio
|
23
|
+
include EmpireAvenue::API::Shares
|
24
|
+
include EmpireAvenue::API::Search
|
25
|
+
include EmpireAvenue::Configurable
|
26
|
+
|
27
|
+
# Initializes a new Client object
|
28
|
+
#
|
29
|
+
# @param options [Hash]
|
30
|
+
# @return [EmpireAvenue::Client]
|
31
|
+
def initialize(options={})
|
32
|
+
EmpireAvenue::Configurable.keys.each do |key|
|
33
|
+
instance_variable_set(:"@#{key}", options[key] || EmpireAvenue.instance_variable_get(:"@#{key}"))
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
# Perform an HTTP DELETE request
|
38
|
+
def delete(path, params={})
|
39
|
+
request(:delete, path, params)
|
40
|
+
end
|
41
|
+
|
42
|
+
# Perform an HTTP GET request
|
43
|
+
def get(path, params={})
|
44
|
+
request(:get, path, params)
|
45
|
+
end
|
46
|
+
|
47
|
+
# Perform an HTTP POST request
|
48
|
+
def post(path, params={})
|
49
|
+
signature_params = params.values.any?{|value| value.respond_to?(:to_io)} ? {} : params
|
50
|
+
request(:post, path, params, signature_params)
|
51
|
+
end
|
52
|
+
|
53
|
+
# Perform an HTTP PUT request
|
54
|
+
def put(path, params={})
|
55
|
+
request(:put, path, params)
|
56
|
+
end
|
57
|
+
|
58
|
+
private
|
59
|
+
|
60
|
+
def request(method, path, params={}, signature_params=params)
|
61
|
+
#params[:client_id] ||= @client_id
|
62
|
+
#params[:access_token] = params[:oauth_token]
|
63
|
+
#params[:access_token] ||= @oauth_token
|
64
|
+
#connection.params = params
|
65
|
+
connection.send(method.to_sym, path, params) do |request|
|
66
|
+
#request.params[:client_id] = params[:client_id]
|
67
|
+
#request.params[:access_token] = params[:access_token]
|
68
|
+
#request.headers[:authorization] = auth_header(method.to_sym, path, signature_params).to_s
|
69
|
+
end.env
|
70
|
+
rescue Faraday::Error::ClientError
|
71
|
+
raise EmpireAvenue::Error::ClientError
|
72
|
+
rescue MultiJson::DecodeError
|
73
|
+
raise EmpireAvenue::Error::DecodeError
|
74
|
+
end
|
75
|
+
|
76
|
+
# Returns a Faraday::Connection object
|
77
|
+
#
|
78
|
+
# @return [Faraday::Connection]
|
79
|
+
def connection
|
80
|
+
@connection ||= Faraday.new(@endpoint, @connection_options.merge(:builder => @middleware).merge({:params => {:client_id => @client_id, :access_token => @oauth_token}}))
|
81
|
+
end
|
82
|
+
|
83
|
+
#def auth_header(method, path, params={})
|
84
|
+
# uri = URI(@endpoint + path)
|
85
|
+
# #OAuth2::Header.new(method, uri, params, credentials)
|
86
|
+
#end
|
87
|
+
|
88
|
+
end
|
89
|
+
end
|
@@ -0,0 +1,84 @@
|
|
1
|
+
require 'forwardable'
|
2
|
+
require 'empireavenue/error/configuration_error'
|
3
|
+
|
4
|
+
module EmpireAvenue
|
5
|
+
module Configurable
|
6
|
+
extend Forwardable
|
7
|
+
attr_accessor :client_id, :client_secret, :oauth_token, :oauth_token_secret
|
8
|
+
attr_accessor :endpoint, :connection_options, :identity_map, :middleware
|
9
|
+
def_delegator :options, :hash
|
10
|
+
|
11
|
+
class << self
|
12
|
+
|
13
|
+
def keys
|
14
|
+
@keys ||= [
|
15
|
+
:client_id,
|
16
|
+
:client_secret,
|
17
|
+
:oauth_token,
|
18
|
+
:oauth_token_secret,
|
19
|
+
:endpoint,
|
20
|
+
:connection_options,
|
21
|
+
:identity_map,
|
22
|
+
:middleware,
|
23
|
+
]
|
24
|
+
end
|
25
|
+
|
26
|
+
end
|
27
|
+
|
28
|
+
# Convenience method to allow configuration options to be set in a block
|
29
|
+
#
|
30
|
+
# @raise [EmpireAvenue::Error::ConfigurationError] Error is raised when supplied
|
31
|
+
# empire avenue credentials are not a String or Symbol.
|
32
|
+
def configure
|
33
|
+
yield self
|
34
|
+
validate_credential_type!
|
35
|
+
self
|
36
|
+
end
|
37
|
+
|
38
|
+
# @return [Boolean]
|
39
|
+
def credentials?
|
40
|
+
credentials.values.all?
|
41
|
+
end
|
42
|
+
|
43
|
+
def reset!
|
44
|
+
EmpireAvenue::Configurable.keys.each do |key|
|
45
|
+
instance_variable_set(:"@#{key}", EmpireAvenue::Default.options[key])
|
46
|
+
end
|
47
|
+
self
|
48
|
+
end
|
49
|
+
alias setup reset!
|
50
|
+
|
51
|
+
private
|
52
|
+
|
53
|
+
# @return [Hash]
|
54
|
+
def credentials
|
55
|
+
{
|
56
|
+
:client_id => @client_id,
|
57
|
+
:client_secret => @client_secret,
|
58
|
+
:token => @oauth_token,
|
59
|
+
:token_secret => @oauth_token_secret,
|
60
|
+
}
|
61
|
+
end
|
62
|
+
|
63
|
+
# @return [Hash]
|
64
|
+
def options
|
65
|
+
Hash[EmpireAvenue::Configurable.keys.map{|key| [key, instance_variable_get(:"@#{key}")]}]
|
66
|
+
end
|
67
|
+
|
68
|
+
# Ensures that all credentials set during configuration are of a
|
69
|
+
# valid type. Valid types are String and Symbol.
|
70
|
+
#
|
71
|
+
# @raise [EmpireAvenue::Error::ConfigurationError] Error is raised when
|
72
|
+
# supplied empire avenue credentials are not a String or Symbol.
|
73
|
+
def validate_credential_type!
|
74
|
+
credentials.each do |credential, value|
|
75
|
+
next if value.nil?
|
76
|
+
|
77
|
+
unless value.is_a?(String) || value.is_a?(Symbol)
|
78
|
+
raise(Error::ConfigurationError, "Invalid #{credential} specified: #{value} must be a string or symbol.")
|
79
|
+
end
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
end
|
84
|
+
end
|
@@ -0,0 +1,97 @@
|
|
1
|
+
require 'faraday'
|
2
|
+
#require 'faraday/request/multipart'
|
3
|
+
require 'empireavenue/configurable'
|
4
|
+
require 'empireavenue/error/client_error'
|
5
|
+
require 'empireavenue/error/server_error'
|
6
|
+
#require 'empireavenue/request/multipart_with_file'
|
7
|
+
require 'empireavenue/response/parse_json'
|
8
|
+
require 'empireavenue/response/raise_error'
|
9
|
+
require 'empireavenue/version'
|
10
|
+
|
11
|
+
module EmpireAvenue
|
12
|
+
module Default
|
13
|
+
ENDPOINT = 'https://api.empireavenue.com' unless defined? EmpireAvenue::Default::ENDPOINT
|
14
|
+
CONNECTION_OPTIONS = {
|
15
|
+
:headers => {
|
16
|
+
:accept => 'application/json',
|
17
|
+
:user_agent => "EmpireAvenue Ruby Gem #{EmpireAvenue::Version}",
|
18
|
+
},
|
19
|
+
:request => {
|
20
|
+
:open_timeout => 5,
|
21
|
+
:timeout => 10,
|
22
|
+
},
|
23
|
+
:params => {
|
24
|
+
},
|
25
|
+
:ssl => {
|
26
|
+
:verify => false
|
27
|
+
},
|
28
|
+
} unless defined? EmpireAvenue::Default::CONNECTION_OPTIONS
|
29
|
+
IDENTITY_MAP = false unless defined? EmpireAvenue::Default::IDENTITY_MAP
|
30
|
+
MIDDLEWARE = Faraday::Builder.new do |builder|
|
31
|
+
# Convert file uploads to Faraday::UploadIO objects
|
32
|
+
#builder.use EmpireAvenue::Request::MultipartWithFile
|
33
|
+
# Checks for files in the payload
|
34
|
+
#builder.use Faraday::Request::Multipart
|
35
|
+
# Convert request params to "www-form-urlencoded"
|
36
|
+
builder.use Faraday::Request::UrlEncoded
|
37
|
+
# Handle 4xx server responses
|
38
|
+
builder.use EmpireAvenue::Response::RaiseError, EmpireAvenue::Error::ClientError
|
39
|
+
# Parse JSON response bodies using MultiJson
|
40
|
+
builder.use EmpireAvenue::Response::ParseJson
|
41
|
+
# Handle 5xx server responses
|
42
|
+
builder.use EmpireAvenue::Response::RaiseError, EmpireAvenue::Error::ServerError
|
43
|
+
# Set Faraday's HTTP adapter
|
44
|
+
builder.adapter Faraday.default_adapter
|
45
|
+
end unless defined? EmpireAvenue::Default::MIDDLEWARE
|
46
|
+
|
47
|
+
class << self
|
48
|
+
|
49
|
+
# @return [Hash]
|
50
|
+
def options
|
51
|
+
Hash[EmpireAvenue::Configurable.keys.map{|key| [key, send(key)]}]
|
52
|
+
end
|
53
|
+
|
54
|
+
# @return [String]sferik
|
55
|
+
def client_id
|
56
|
+
ENV['EMPIRE_AVENUE_CLIENT_ID']
|
57
|
+
end
|
58
|
+
|
59
|
+
# @return [String]
|
60
|
+
def client_secret
|
61
|
+
ENV['EMPIRE_AVENUE_CLIENT_SECRET']
|
62
|
+
end
|
63
|
+
|
64
|
+
# @return [String]
|
65
|
+
def oauth_token
|
66
|
+
ENV['EMPIRE_AVENUE_OAUTH_TOKEN']
|
67
|
+
end
|
68
|
+
|
69
|
+
# @return [String]
|
70
|
+
def oauth_token_secret
|
71
|
+
ENV['EMPIRE_AVENUE_OAUTH_TOKEN_SECRET']
|
72
|
+
end
|
73
|
+
|
74
|
+
# @note This is configurable in case you want to use a Empire Avenue-compatible endpoint.
|
75
|
+
# @return [String]
|
76
|
+
def endpoint
|
77
|
+
ENDPOINT
|
78
|
+
end
|
79
|
+
|
80
|
+
def connection_options
|
81
|
+
CONNECTION_OPTIONS
|
82
|
+
end
|
83
|
+
|
84
|
+
def identity_map
|
85
|
+
IDENTITY_MAP
|
86
|
+
end
|
87
|
+
|
88
|
+
# @note Faraday's middleware stack implementation is comparable to that of Rack middleware. The order of middleware is important: the first middleware on the list wraps all others, while the last middleware is the innermost one.
|
89
|
+
# @see http://mislav.uniqpath.com/2011/07/faraday-advanced-http/
|
90
|
+
# @return [Faraday::Builder]
|
91
|
+
def middleware
|
92
|
+
MIDDLEWARE
|
93
|
+
end
|
94
|
+
|
95
|
+
end
|
96
|
+
end
|
97
|
+
end
|