atech-billy 1.0.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.
- data/lib/billy.rb +35 -0
- data/lib/billy/request.rb +76 -0
- data/lib/billy/service.rb +27 -0
- metadata +47 -0
data/lib/billy.rb
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
require 'time'
|
2
|
+
require 'uri'
|
3
|
+
require 'net/https'
|
4
|
+
require 'json'
|
5
|
+
|
6
|
+
require 'billy/request'
|
7
|
+
require 'billy/service'
|
8
|
+
|
9
|
+
module Billy
|
10
|
+
|
11
|
+
VERSION = '1.0.0'
|
12
|
+
|
13
|
+
class << self
|
14
|
+
|
15
|
+
## Application name & key
|
16
|
+
attr_accessor :product, :product_key
|
17
|
+
|
18
|
+
## Endpoint to send domains to
|
19
|
+
attr_writer :host
|
20
|
+
|
21
|
+
def host
|
22
|
+
@host ||= 'https://m.atechmedia.com'
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
class Error < StandardError; end
|
27
|
+
module Errors
|
28
|
+
class ServiceUnavailable < Error; end
|
29
|
+
class AccessDenied < Error; end
|
30
|
+
class NotFound < Error; end
|
31
|
+
class CommunicationError < Error; end
|
32
|
+
class ValidationError < Error; end
|
33
|
+
end
|
34
|
+
|
35
|
+
end
|
@@ -0,0 +1,76 @@
|
|
1
|
+
module Billy
|
2
|
+
class Request
|
3
|
+
|
4
|
+
def self.request(path, data = {})
|
5
|
+
req = self.new(path, :post)
|
6
|
+
req.data = data
|
7
|
+
req.make && req.success? ? req.output : false
|
8
|
+
end
|
9
|
+
|
10
|
+
attr_reader :path, :method, :client
|
11
|
+
attr_accessor :data
|
12
|
+
|
13
|
+
def initialize(path, method = :get)
|
14
|
+
@path = path
|
15
|
+
@method = method
|
16
|
+
end
|
17
|
+
|
18
|
+
def success?
|
19
|
+
@success || false
|
20
|
+
end
|
21
|
+
|
22
|
+
def output
|
23
|
+
@output || nil
|
24
|
+
end
|
25
|
+
|
26
|
+
def make
|
27
|
+
uri = URI.parse([Billy.host, "api/v1", @path].join('/'))
|
28
|
+
http_request = http_class.new(uri.request_uri)
|
29
|
+
http_request.initialize_http_header({"User-Agent" => "BillyRubyClient/#{Billy::VERSION}"})
|
30
|
+
http_request.add_field("X-Billy-Product", Billy.product)
|
31
|
+
http_request.add_field("X-Billy-Key", Billy.product_key)
|
32
|
+
|
33
|
+
http_request.set_form_data(@data)
|
34
|
+
|
35
|
+
http = Net::HTTP.new(uri.host, uri.port)
|
36
|
+
|
37
|
+
if uri.scheme == 'https'
|
38
|
+
http.use_ssl = true
|
39
|
+
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
|
40
|
+
end
|
41
|
+
|
42
|
+
http_result = http.request(http_request)
|
43
|
+
@output = JSON.parse(http_result.body)
|
44
|
+
@success = case http_result
|
45
|
+
when Net::HTTPSuccess
|
46
|
+
true
|
47
|
+
when Net::HTTPServiceUnavailable
|
48
|
+
raise Billy::Errors::ServiceUnavailable
|
49
|
+
when Net::HTTPForbidden, Net::HTTPUnauthorized
|
50
|
+
raise Billy::Errors::AccessDenied, "Access Denied for '#{Billy.token}'"
|
51
|
+
when Net::HTTPNotFound
|
52
|
+
json = JSON.parse(http_result.body)
|
53
|
+
raise Billy::Errors::NotFound, json['error']
|
54
|
+
when Net::HTTPClientError
|
55
|
+
json = JSON.parse(http_result.body)
|
56
|
+
raise Billy::Errors::ValidationError, json['errors'].to_s
|
57
|
+
else
|
58
|
+
raise Billy::Errors::CommunicationError, http_result.body
|
59
|
+
end
|
60
|
+
self
|
61
|
+
end
|
62
|
+
|
63
|
+
private
|
64
|
+
|
65
|
+
def http_class
|
66
|
+
case @method
|
67
|
+
when :post then Net::HTTP::Post
|
68
|
+
when :put then Net::HTTP::Put
|
69
|
+
when :delete then Net::HTTP::Delete
|
70
|
+
else
|
71
|
+
Net::HTTP::Get
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
end
|
76
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
module Billy
|
2
|
+
class Service
|
3
|
+
|
4
|
+
attr_reader :attributes
|
5
|
+
|
6
|
+
def initialize(attributes)
|
7
|
+
@attributes = attributes
|
8
|
+
end
|
9
|
+
|
10
|
+
## Create a new login session and return a URL which can be used to log the user
|
11
|
+
## into the billing application
|
12
|
+
def create_session(start_path = nil, return_url = nil)
|
13
|
+
session = Billy::Request.request('services/create_session', :id => @attributes['id'], :start_path => start_path, :return_url => return_url)
|
14
|
+
session ? [Billy.host, 'start', session['token']].join('/') : false
|
15
|
+
end
|
16
|
+
|
17
|
+
def method_missing(param)
|
18
|
+
@attributes[param.to_s] || super
|
19
|
+
end
|
20
|
+
|
21
|
+
def self.find(field, data)
|
22
|
+
attributes = Billy::Request.request('services/info', :field => field, :data => data)
|
23
|
+
attributes.is_a?(Hash) ? self.new(attributes) : nil
|
24
|
+
end
|
25
|
+
|
26
|
+
end
|
27
|
+
end
|
metadata
ADDED
@@ -0,0 +1,47 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: atech-billy
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Adam Cooke
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-05-22 00:00:00.000000000Z
|
13
|
+
dependencies: []
|
14
|
+
description:
|
15
|
+
email: adam@atechmedia.com
|
16
|
+
executables: []
|
17
|
+
extensions: []
|
18
|
+
extra_rdoc_files: []
|
19
|
+
files:
|
20
|
+
- lib/billy.rb
|
21
|
+
- lib/billy/request.rb
|
22
|
+
- lib/billy/service.rb
|
23
|
+
homepage: http://atechmedia.com
|
24
|
+
licenses: []
|
25
|
+
post_install_message:
|
26
|
+
rdoc_options: []
|
27
|
+
require_paths:
|
28
|
+
- lib
|
29
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
30
|
+
none: false
|
31
|
+
requirements:
|
32
|
+
- - ! '>='
|
33
|
+
- !ruby/object:Gem::Version
|
34
|
+
version: '0'
|
35
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
36
|
+
none: false
|
37
|
+
requirements:
|
38
|
+
- - ! '>='
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
requirements: []
|
42
|
+
rubyforge_project:
|
43
|
+
rubygems_version: 1.8.10
|
44
|
+
signing_key:
|
45
|
+
specification_version: 3
|
46
|
+
summary: Easy to use client library for Billy
|
47
|
+
test_files: []
|