paytunia 0.1alpha → 0.1.1alpha

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/README.md CHANGED
@@ -2,3 +2,29 @@ Paytunia
2
2
  =
3
3
 
4
4
  The official Ruby gem to integrate your app with [Paytunia](https://paytunia.com).
5
+
6
+ [![Build Status](https://secure.travis-ci.org/paytunia/paytunia.png?branch=master)](http://travis-ci.org/paytunia/paytunia)
7
+
8
+ ## Installation
9
+
10
+ ```
11
+ $ sudo gem install paytunia --pre
12
+ ```
13
+
14
+ or add it to your `Gemfile`
15
+
16
+ ```ruby
17
+ gem 'paytunia', '0.1alpha'
18
+ ```
19
+
20
+ ## Usage
21
+
22
+ ### As a Ruby gem
23
+
24
+ ### As a command-line executable
25
+
26
+ ## Authentication
27
+
28
+ ### HTTP Basic
29
+
30
+ ### OAuthv2
data/bin/paytunia CHANGED
@@ -6,8 +6,11 @@ Signal.trap("INT") { exit 1 }
6
6
 
7
7
  require 'paytunia'
8
8
 
9
- Paytunia.cli!
9
+ print "Account ID : "
10
+ username = $stdin.gets.chomp
10
11
 
11
- ARGV.each do |command|
12
- puts Paytunia::Connection.send(command)
13
- end
12
+ print "Password : "
13
+ password = STDIN.noecho { $stdin.gets }.chomp
14
+ print "\n"
15
+
16
+ puts Paytunia::Connection.send(ARGV[0])
@@ -1,12 +1,10 @@
1
1
  module Paytunia
2
2
  module Api
3
- module Account
4
-
5
- # Returns the list of account operations
6
- def get_ledger
7
- token.get('/api/v1/account_operations').body
8
- end
9
3
 
4
+ # Returns the list of account operations
5
+ def get_ledger
6
+ account.get('/account_operations')
10
7
  end
8
+
11
9
  end
12
10
  end
@@ -1,22 +1,20 @@
1
1
  module Paytunia
2
2
  module Api
3
- module Trading
4
3
 
5
- # Returns the active trade orders (pending_execution, active, or insufficient_funds)
6
- def list_active_orders
7
- token.get('/api/v1/trade_orders/active').body
8
- end
9
-
10
- # Returns all orders, ever
11
- def list_orders
12
- token.get('/api/v1/trade_orders').body
13
- end
4
+ # Returns the active trade orders (pending_execution, active, or insufficient_funds)
5
+ def list_active_orders
6
+ account.get('/trade_orders/active')
7
+ end
14
8
 
15
- # Returns the ticker
16
- def get_ticker
17
- get('/api/v1/ticker')
18
- end
9
+ # Returns a paginated list of orders
10
+ def list_orders
11
+ account.get('/trade_orders')
12
+ end
19
13
 
14
+ # Returns the ticker
15
+ def get_ticker
16
+ Ticker.new(get('/ticker'))
20
17
  end
18
+
21
19
  end
22
20
  end
@@ -4,62 +4,68 @@ require 'singleton'
4
4
  require 'io/console'
5
5
 
6
6
  module Paytunia
7
-
8
- @@cli = false
9
-
10
- def self.cli
11
- @@cli
12
- end
13
-
14
- def self.cli!
15
- @@cli = true
16
- end
17
-
18
7
  class Connection
19
8
 
20
9
  include Singleton
21
-
22
10
  include HTTParty
23
11
 
24
- include Paytunia::Api::Account
25
- include Paytunia::Api::Trading
12
+ include Paytunia::Api
26
13
 
27
14
  APP_ID = '6fcf1c32f6e14cd773a7a6640832bdbf83a5b2b8d4382e839c6aff83a8f1bb3b'
28
15
  APP_SECRET = '55554ecad5627f0465034c4a116e59a38d9c3ab272487a18404078ccc0b64798'
29
16
 
30
- SITE = 'https://bitcoin-central.net'
17
+ CA_FILE = File.dirname(__FILE__) + '/../../certs/ca-certificates.crt'
18
+
19
+ SITE = 'https://bitcoin-central.net/api/v1'
31
20
 
32
21
  def get(url)
33
- self.class.get(SITE + url).body
22
+ self.class.get(SITE + url)
34
23
  end
35
24
 
36
- def token
37
- @token ||= connect
25
+ def account
26
+ @basic_auth_wrapper || @oauth2_wrapper || raise('No authenticated connection available')
38
27
  end
39
28
 
40
29
  def connect(credentials = nil)
41
- unless credentials
42
- if Paytunia.cli
43
- print 'Account ID: '
44
- username = $stdin.gets.chomp
45
-
46
- print 'Password: '
47
- password = STDIN.noecho { $stdin.gets }.chomp
48
- print "\n"
49
- else
50
- raise 'No credentials provided, unable to request them interactively'
51
- end
30
+ if credentials[:basic_auth]
31
+ do_basic_auth_connect!(credentials[:basic_auth][:username], credentials[:basic_auth][:password])
52
32
  end
53
33
 
54
- client = OAuth2::Client.new(APP_ID, APP_SECRET,
55
- site: SITE,
56
- ssl: { ca_file: File.dirname(__FILE__) + '/../../certs/ca-certificates.crt' })
34
+ if credentials[:oauth2] && !@basic_auth_wrapper
35
+ do_oauth2_connect!(credentials[:oauth2][:username],
36
+ credentials[:oauth2][:password],
37
+ credentials[:oauth2][:access_token]
38
+ )
39
+ end
57
40
 
58
- @token = client.password.get_token(username, password, scope: 'read trade merchant')
41
+ unless @basic_auth_wrapper || @oauth2_wrapper
42
+ raise 'No credentials provided, unable to request them interactively'
43
+ end
44
+
45
+ self
46
+ end
47
+
48
+
49
+ protected
50
+
51
+ def do_basic_auth_connect!(username, password)
52
+ @basic_auth_wrapper = Paytunia::ConnectionWrappers::BasicAuthWrapper.new(username, password, SITE)
53
+ end
54
+
55
+ def do_oauth2_connect!(username, password, access_token)
56
+ if access_token
57
+ # TODO : Implement me
58
+ else
59
+ client = OAuth2::Client.new(APP_ID, APP_SECRET,
60
+ site: SITE,
61
+ ssl: { ca_file: CA_FILE })
62
+
63
+ @token = client.password.get_token(username, password, scope: 'read trade merchant')
64
+ end
59
65
  end
60
66
 
61
67
  def self.method_missing(method, *args, &block)
62
- instance.respond_to?(method) ? instance.send(method, *args, &block) : super
68
+ Paytunia::Connection.instance.respond_to?(method) ? Paytunia::Connection.instance.send(method, *args, &block) : super
63
69
  end
64
70
  end
65
71
  end
@@ -0,0 +1,33 @@
1
+ require 'net/https'
2
+ require 'uri'
3
+
4
+ module Paytunia
5
+ module ConnectionWrappers
6
+ class BasicAuthWrapper
7
+
8
+ def initialize(username, password, site)
9
+ @username = username
10
+ @password = password
11
+ @site = site
12
+ end
13
+
14
+ def get(path)
15
+
16
+ uri = URI.parse(@site + path)
17
+
18
+ https = Net::HTTP.new(uri.host, 443)
19
+
20
+ https.use_ssl = true
21
+
22
+ https.verify_mode = OpenSSL::SSL::VERIFY_PEER
23
+ https.ca_file = Paytunia::Connection::CA_FILE
24
+
25
+ request = Net::HTTP::Get.new(uri.request_uri)
26
+
27
+ request.basic_auth(@username, @password)
28
+
29
+ JSON.parse(https.request(request).body)
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,16 @@
1
+ module Paytunia
2
+ module ConnectionWrappers
3
+ class OAuthWrapper
4
+ attr_reader :token, :site
5
+
6
+ def initialize(token, site)
7
+ @token = token
8
+ @site = site
9
+ end
10
+
11
+ def get(path)
12
+ JSON.parse(token.get(@site + path).body)
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,11 @@
1
+ module Paytunia
2
+ module Api
3
+ class Ticker
4
+ attr_accessor :high, :low, :volume, :bid, :ask, :midpoint, :at, :price, :variation, :currency
5
+
6
+ def initialize args
7
+ args.each { |k,v| send("#{k}=", v) }
8
+ end
9
+ end
10
+ end
11
+ end
@@ -1,3 +1,5 @@
1
1
  module Paytunia
2
- VERSION = "0.1alpha"
2
+
3
+ VERSION = '0.1.1alpha'
4
+
3
5
  end
data/lib/paytunia.rb CHANGED
@@ -1,5 +1,36 @@
1
- Dir[File.dirname(__FILE__) + '/paytunia/api/**.rb'].each do |file|
2
- require file
1
+ Dir.glob(File.dirname(__FILE__) + '/paytunia/**/*.rb').each { |file| require file }
2
+
3
+ # Override HTTParty's default JSON parser with monkeypatched pure parser
4
+ # because we'd like all float values to be deserialized as BigDecimal instances directly
5
+ require 'json/pure'
6
+
7
+ module JSON
8
+ module Pure
9
+ class Parser
10
+ alias_method :parse_value_original, :parse_value
11
+
12
+ def parse_value
13
+ case
14
+ when scan(FLOAT)
15
+ BigDecimal(self[1])
16
+ else
17
+ parse_value_original
18
+ end
19
+ end
20
+ end
21
+ end
22
+ end
23
+
24
+ module HTTParty
25
+ class Parser
26
+ def json
27
+ JSON.parse(body)
28
+ end
29
+ end
3
30
  end
4
31
 
5
- require 'paytunia/connection'
32
+ module Paytunia
33
+ def self.method_missing(method, *args, &block)
34
+ Paytunia::Connection.instance.respond_to?(method) ? Paytunia::Connection.instance.send(method, *args, &block) : super
35
+ end
36
+ end
metadata CHANGED
@@ -1,8 +1,8 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: paytunia
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1alpha
5
- prerelease: 3
4
+ version: 0.1.1alpha
5
+ prerelease: 5
6
6
  platform: ruby
7
7
  authors:
8
8
  - Alexis DENEUX
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2013-02-20 00:00:00.000000000 Z
13
+ date: 2013-02-25 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: oauth2
@@ -44,6 +44,22 @@ dependencies:
44
44
  - - ! '>='
45
45
  - !ruby/object:Gem::Version
46
46
  version: '0'
47
+ - !ruby/object:Gem::Dependency
48
+ name: json_pure
49
+ requirement: !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ type: :runtime
56
+ prerelease: false
57
+ version_requirements: !ruby/object:Gem::Requirement
58
+ none: false
59
+ requirements:
60
+ - - ! '>='
61
+ - !ruby/object:Gem::Version
62
+ version: '0'
47
63
  - !ruby/object:Gem::Dependency
48
64
  name: rspec
49
65
  requirement: !ruby/object:Gem::Requirement
@@ -60,6 +76,54 @@ dependencies:
60
76
  - - ! '>='
61
77
  - !ruby/object:Gem::Version
62
78
  version: '0'
79
+ - !ruby/object:Gem::Dependency
80
+ name: vcr
81
+ requirement: !ruby/object:Gem::Requirement
82
+ none: false
83
+ requirements:
84
+ - - ! '>='
85
+ - !ruby/object:Gem::Version
86
+ version: '0'
87
+ type: :development
88
+ prerelease: false
89
+ version_requirements: !ruby/object:Gem::Requirement
90
+ none: false
91
+ requirements:
92
+ - - ! '>='
93
+ - !ruby/object:Gem::Version
94
+ version: '0'
95
+ - !ruby/object:Gem::Dependency
96
+ name: webmock
97
+ requirement: !ruby/object:Gem::Requirement
98
+ none: false
99
+ requirements:
100
+ - - ! '>='
101
+ - !ruby/object:Gem::Version
102
+ version: '0'
103
+ type: :development
104
+ prerelease: false
105
+ version_requirements: !ruby/object:Gem::Requirement
106
+ none: false
107
+ requirements:
108
+ - - ! '>='
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: rake
113
+ requirement: !ruby/object:Gem::Requirement
114
+ none: false
115
+ requirements:
116
+ - - ! '>='
117
+ - !ruby/object:Gem::Version
118
+ version: '0'
119
+ type: :development
120
+ prerelease: false
121
+ version_requirements: !ruby/object:Gem::Requirement
122
+ none: false
123
+ requirements:
124
+ - - ! '>='
125
+ - !ruby/object:Gem::Version
126
+ version: '0'
63
127
  description: Supports all API calls, HTTP Basic and OAuth2 authentication
64
128
  email:
65
129
  - support@paytunia.com
@@ -72,6 +136,9 @@ files:
72
136
  - lib/paytunia/api/account.rb
73
137
  - lib/paytunia/api/trading.rb
74
138
  - lib/paytunia/connection.rb
139
+ - lib/paytunia/connection_wrappers/basic_auth_wrapper.rb
140
+ - lib/paytunia/connection_wrappers/oauth_wrapper.rb
141
+ - lib/paytunia/models/ticker.rb
75
142
  - lib/paytunia/version.rb
76
143
  - lib/paytunia.rb
77
144
  - LICENSE