blupee 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: ef0f440d08a682ab70e94af6af3e858a0ffe78e4
4
+ data.tar.gz: a53abc212a85a0600bdd76a56e694a7a9dc64bd5
5
+ SHA512:
6
+ metadata.gz: 71549040f02d1dc736229154ca6032151b32a5df13b85f6acfa738e2efe7f6377e50ed47b97e511c79bf6ee9356a851fde7dd6554a62f244b3fa629a18e6a6d8
7
+ data.tar.gz: d683c0abe0f40604683674c58cd56dbae19dc8f080678c5f7c01314a989827e7f00ead78e92a8c63c5de59114988b4cd1b8f2c18f070445496fb416acc100b94
@@ -0,0 +1,25 @@
1
+ require_relative "blupee/version"
2
+ require_relative "blupee/ether"
3
+ require_relative "blupee/auth"
4
+ require_relative "blupee/configuration"
5
+ require_relative "blupee/http_service"
6
+ require_relative "blupee/http_service/request"
7
+ require_relative "blupee/http_service/response"
8
+
9
+ module Blupee
10
+
11
+ class << self
12
+ def configure
13
+ yield config
14
+ end
15
+
16
+ # See Blupee::Configuration.
17
+ def config
18
+ @config ||= Configuration.new
19
+ end
20
+ end
21
+
22
+ def self.make_request(path, args, verb, options = {})
23
+ HTTPService.make_request(HTTPService::Request.new(path: path, args: args, verb: verb, options: options))
24
+ end
25
+ end
@@ -0,0 +1,80 @@
1
+ # OpenSSL and Base64 are required to support signed_request
2
+ require 'openssl'
3
+ require 'base64'
4
+
5
+ module Blupee
6
+ class Auth
7
+ attr_reader :client_id, :client_secret
8
+
9
+ # Creates a new client.
10
+ #
11
+ # @param client_id [String, Integer] a Blupee client ID
12
+ # @param client_secret a Blupee client secret
13
+ def initialize(client_id = nil, client_secret = nil)
14
+ @client_id = client_id || Blupee.config.client_id
15
+ @client_secret = client_secret || Blupee.config.client_secret
16
+ end
17
+
18
+ # access tokens
19
+
20
+ # Fetches an access token, token expiration, and other info from Blupee.
21
+ # Useful when you've received an OAuth code using the server-side authentication process.
22
+ # @see url_for_oauth_code
23
+ #
24
+ # @note (see #url_for_oauth_code)
25
+ #
26
+ # @param code (see #url_for_access_token)
27
+ # @param options any additional parameters to send to Blupee when redeeming the token
28
+ #
29
+ # @raise Blupee::OAuthTokenRequestError if Blupee returns an error response
30
+ #
31
+ # @return a hash of the access token info returned by Blupee (token, expiration, etc.)
32
+ def get_access_token_info(options = {})
33
+ # convenience method to get a the application's sessionless access token
34
+ get_token_from_server({}, true, options)
35
+ end
36
+
37
+ # Fetches the application's access token (ignoring expiration and other info).
38
+ # @see get_app_access_token_info
39
+ #
40
+ # @param (see #get_app_access_token_info)
41
+ #
42
+ # @return the application access token
43
+ def get_access_token(options = {})
44
+ if info = get_access_token_info(options)
45
+ Blupee.config.access_token = info["access_token"]
46
+ end
47
+ end
48
+
49
+ protected
50
+
51
+ def get_token_from_server(args, post = false, options = {})
52
+ # fetch the result from Blupee's servers
53
+ response = fetch_token_string(args, post, "auth", options)
54
+ parse_access_token(response)
55
+ end
56
+
57
+ def parse_access_token(response_text)
58
+ JSON.parse(response_text)
59
+ rescue JSON::ParserError
60
+ response_text.split("&").inject({}) do |hash, bit|
61
+ key, value = bit.split("=")
62
+ hash.merge!(key => value)
63
+ end
64
+ end
65
+
66
+
67
+ def fetch_token_string(args, post = false, endpoint = "auth", options = {})
68
+ response = Blupee.make_request("/#{endpoint}", {
69
+ :client_id => @client_id,
70
+ :client_secret => @client_secret
71
+ }.merge!(args), post ? "post" : "get", {:use_ssl => true, format: :json}.merge!(options))
72
+
73
+ # raise ServerError.new(response.status, response.body) if response.status >= 500
74
+ # raise OAuthTokenRequestError.new(response.status, response.body) if response.status >= 400
75
+
76
+ response.body
77
+ end
78
+
79
+ end
80
+ end
@@ -0,0 +1,20 @@
1
+ # Global configuration for Blupee.
2
+ class Blupee::Configuration
3
+ # The default access token to be used if none is otherwise supplied.
4
+ attr_accessor :access_token
5
+
6
+ # The default client secret value to be used if none is otherwise supplied.
7
+ attr_accessor :client_secret
8
+
9
+ # The default client ID to use if none is otherwise supplied.
10
+ attr_accessor :client_id
11
+
12
+ attr_accessor :api_server
13
+
14
+ attr_accessor :api_version
15
+
16
+ def initialize
17
+ self.api_server = "api.blupee.io"
18
+ self.api_version = "v1"
19
+ end
20
+ end
@@ -0,0 +1,55 @@
1
+ require 'faraday'
2
+ require 'json'
3
+
4
+ module Blupee
5
+ class Ether
6
+ attr_reader :coin_symbol, :balance, :wallet, :transaction_hash
7
+
8
+ def initialize(attributes)
9
+ @coin_symbol = attributes["coin_symbol"]
10
+ @balance = attributes["balance"]
11
+ @wallet_address = attributes["wallet_address"]
12
+ end
13
+
14
+ def self.balance(id, options = {})
15
+ response = Blupee.make_request("/eth/#{id}", {}, "get", {:use_ssl => true}.merge!(options))
16
+ # raise ServerError.new(response.status, response.body) if response.status >= 500
17
+ # raise OAuthTokenRequestError.new(response.status, response.body) if response.status >= 400
18
+ attributes = JSON.parse(response.body)
19
+ new(attributes)
20
+ end
21
+
22
+
23
+ def self.new_wallet(password, options = {})
24
+ response = Blupee.make_request("/eth/new", {
25
+ :password => password
26
+ }, "post", {:use_ssl => true, format: :json}.merge!(options))
27
+
28
+ # raise ServerError.new(response.status, response.body) if response.status >= 500
29
+ # raise OAuthTokenRequestError.new(response.status, response.body) if response.status >= 400
30
+ attributes = JSON.parse(response.body)
31
+ new(attributes)
32
+ end
33
+
34
+ # {:to_address=>"", :from_address=>"", :password=>"", :quantity=>0.001}
35
+ def self.send_ether(args, options = {})
36
+ response = Blupee.make_request("/eth/send",
37
+ {}.merge!(args),
38
+ "post",
39
+ {:use_ssl => true,
40
+ format: :json}.merge!(options))
41
+
42
+ # raise ServerError.new(response.status, response.body) if response.status >= 500
43
+ # raise OAuthTokenRequestError.new(response.status, response.body) if response.status >= 400
44
+ puts response.body
45
+ attributes = JSON.parse(response.body)
46
+ new(attributes)
47
+ end
48
+
49
+
50
+ def self.all
51
+ eth_wallet = JSON.parse(response.body)
52
+ eth_wallet.map { |attributes| new(attributes) }
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,65 @@
1
+ require 'faraday'
2
+ require_relative 'http_service/request'
3
+ require_relative 'http_service/response'
4
+
5
+ module Blupee
6
+ module HTTPService
7
+ class << self
8
+ # A customized stack of Faraday middleware that will be used to make each request.
9
+ attr_accessor :faraday_middleware
10
+ attr_accessor :http_options
11
+ end
12
+
13
+ @http_options ||= {}
14
+
15
+ # Blupee's default middleware stack.
16
+ # and use whichever adapter has been configured for this application.
17
+ DEFAULT_MIDDLEWARE = Proc.new do |builder|
18
+ builder.request :url_encoded
19
+ builder.adapter Faraday.default_adapter
20
+ end
21
+
22
+ # Makes a request directly to Blupee.
23
+ # @note You'll rarely need to call this method directly.
24
+ #
25
+ #
26
+ # @param request a Blupee::HTTPService::Request object
27
+ #
28
+ # @raise an appropriate connection error if unable to make the request to Blupee
29
+ #
30
+ # @return [Blupee::HTTPService::Response] a response object representing the results from Blupee
31
+ def self.make_request(request)
32
+ # set up our Faraday connection
33
+ conn = Faraday.new(request.server, faraday_options(request.options), &(faraday_middleware || DEFAULT_MIDDLEWARE))
34
+ if request.verb == "post" && request.json?
35
+ # JSON requires a bit more handling
36
+ # remember, all non-GET requests are turned into POSTs, so this covers everything but GETs
37
+ response = conn.post do |req|
38
+ req.path = request.path
39
+ req.headers["Content-Type"] = "application/json"
40
+ req.headers['Bearer'] = Blupee.config.access_token if Blupee.config.access_token
41
+ req.body = request.post_args.to_json
42
+ req
43
+ end
44
+ else
45
+ # response = conn.send(request.verb, request.path, request.post_args)
46
+ response = conn.get do |req|
47
+ req.path = request.path
48
+ req.headers['Bearer'] = Blupee.config.access_token if Blupee.config.access_token
49
+ end
50
+ end
51
+
52
+ # Log URL information
53
+ # Blupee::Utils.debug "#{request.verb.upcase}: #{request.path} params: #{request.raw_args.inspect}"
54
+ Blupee::HTTPService::Response.new(response.status.to_i, response.body, response.headers)
55
+ end
56
+
57
+
58
+ private
59
+
60
+ def self.faraday_options(options)
61
+ valid_options = [:request, :proxy, :ssl, :builder, :url, :parallel_manager, :params, :headers, :builder_class]
62
+ Hash[ options.select { |key,value| valid_options.include?(key) } ]
63
+ end
64
+ end
65
+ end
@@ -0,0 +1,101 @@
1
+ module Blupee
2
+ module HTTPService
3
+ class Request
4
+ attr_reader :raw_path, :raw_args, :raw_verb, :raw_options
5
+
6
+ # @param path the server path for this request
7
+ # @param args
8
+ # @param verb the HTTP method to use.
9
+ # If not get or post, this will be turned into a POST request with the appropriate :method
10
+ # specified in the arguments.
11
+ # @param options various flags to indicate which server to use.
12
+ # @param options
13
+ # @option options :use_ssl force https, even if not needed
14
+ # @option options :json whether or not to send JSON to Blupee
15
+ def initialize(path:, verb:, args: {}, options: {})
16
+ @raw_path = path
17
+ @raw_args = args
18
+ @raw_verb = verb
19
+ @raw_options = options
20
+ end
21
+
22
+ # Determines which type of request to send to Blupee. Blupee natively accepts GETs and POSTs, for others we have to include the method in the post body.
23
+ #
24
+ # @return one of get or post
25
+ def verb
26
+ ["get", "post"].include?(raw_verb) ? raw_verb : "post"
27
+ end
28
+
29
+ # Determines the path to be requested on Blupee, incorporating an API version if specified.
30
+ #
31
+ # @return the original path, with API version if appropriate.
32
+ def path
33
+ # if an api_version is specified and the path does not already contain
34
+ # one, prepend it to the path
35
+ api_version = raw_options[:api_version] || Blupee.config.api_version
36
+ "/#{api_version}/#{raw_path}"
37
+ end
38
+
39
+ # Determines any arguments to be sent in a POST body.
40
+ #
41
+ # @return {} for GET; the provided args for POST; those args with the method parameter for
42
+ # other values
43
+ def post_args
44
+ if raw_verb == "get"
45
+ {}
46
+ elsif raw_verb == "post"
47
+ args
48
+ else
49
+ args.merge(method: raw_verb)
50
+ end
51
+ end
52
+
53
+ def get_args
54
+ raw_verb == "get" ? args : {}
55
+ end
56
+
57
+ # Calculates a set of request options to pass to Faraday.
58
+ #
59
+ # @return a hash combining GET parameters (if appropriate), default options, and
60
+ # any specified for the request.
61
+ def options
62
+ # figure out our options for this request
63
+ add_ssl_options(
64
+ # for GETs, we pass the params to Faraday to encode
65
+ {params: get_args}.merge(HTTPService.http_options).merge(raw_options)
66
+ )
67
+ end
68
+
69
+ # Whether or not this request should use JSON.
70
+ #
71
+ # @return true or false
72
+ def json?
73
+ raw_options[:format] == :json
74
+ end
75
+
76
+ # The address of the appropriate Blupee server.
77
+ #
78
+ # @return a complete server address with protocol
79
+ def server
80
+ uri = "#{options[:use_ssl] ? "https" : "http"}://#{Blupee.config.api_server}"
81
+ end
82
+
83
+ protected
84
+
85
+ # The arguments to include in the request.
86
+ def args
87
+ raw_args
88
+ end
89
+
90
+ def add_ssl_options(opts)
91
+ # require https if there's a token
92
+ return opts unless raw_args["access_token"]
93
+
94
+ {
95
+ use_ssl: true,
96
+ ssl: {verify: true}.merge(opts[:ssl] || {})
97
+ }.merge(opts)
98
+ end
99
+ end
100
+ end
101
+ end
@@ -0,0 +1,20 @@
1
+ module Blupee
2
+ module HTTPService
3
+ class Response
4
+ attr_reader :status, :body, :headers
5
+
6
+ # Creates a new Response object, which standardizes the response received by Blupee API for use within Blupee.
7
+ def initialize(status, body, headers)
8
+ @status = status
9
+ @body = body
10
+ @headers = headers
11
+ end
12
+
13
+ def data
14
+ # quirks_mode is needed because Blupee sometimes returns a raw true or false value --
15
+ # in Ruby 2.4 we can drop that.
16
+ @data ||= JSON.parse(body, quirks_mode: true) unless body.empty?
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,3 @@
1
+ module Blupee
2
+ VERSION = "0.1.0"
3
+ end
metadata ADDED
@@ -0,0 +1,200 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: blupee
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Blupee Inc.
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-10-29 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.6'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.6'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 10.4.2
34
+ - - ">="
35
+ - !ruby/object:Gem::Version
36
+ version: 12.0.1
37
+ type: :development
38
+ prerelease: false
39
+ version_requirements: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - "~>"
42
+ - !ruby/object:Gem::Version
43
+ version: 10.4.2
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: 12.0.1
47
+ - !ruby/object:Gem::Dependency
48
+ name: minitest
49
+ requirement: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - "~>"
52
+ - !ruby/object:Gem::Version
53
+ version: 5.8.3
54
+ - - ">="
55
+ - !ruby/object:Gem::Version
56
+ version: 5.10.2
57
+ type: :development
58
+ prerelease: false
59
+ version_requirements: !ruby/object:Gem::Requirement
60
+ requirements:
61
+ - - "~>"
62
+ - !ruby/object:Gem::Version
63
+ version: 5.8.3
64
+ - - ">="
65
+ - !ruby/object:Gem::Version
66
+ version: 5.10.2
67
+ - !ruby/object:Gem::Dependency
68
+ name: vcr
69
+ requirement: !ruby/object:Gem::Requirement
70
+ requirements:
71
+ - - "~>"
72
+ - !ruby/object:Gem::Version
73
+ version: 3.0.0
74
+ - - ">="
75
+ - !ruby/object:Gem::Version
76
+ version: 3.0.3
77
+ type: :development
78
+ prerelease: false
79
+ version_requirements: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - "~>"
82
+ - !ruby/object:Gem::Version
83
+ version: 3.0.0
84
+ - - ">="
85
+ - !ruby/object:Gem::Version
86
+ version: 3.0.3
87
+ - !ruby/object:Gem::Dependency
88
+ name: pry
89
+ requirement: !ruby/object:Gem::Requirement
90
+ requirements:
91
+ - - "~>"
92
+ - !ruby/object:Gem::Version
93
+ version: 0.10.0
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: 0.10.9
97
+ type: :development
98
+ prerelease: false
99
+ version_requirements: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: 0.10.0
104
+ - - ">="
105
+ - !ruby/object:Gem::Version
106
+ version: 0.10.9
107
+ - !ruby/object:Gem::Dependency
108
+ name: webmock
109
+ requirement: !ruby/object:Gem::Requirement
110
+ requirements:
111
+ - - "~>"
112
+ - !ruby/object:Gem::Version
113
+ version: 3.1.0
114
+ - - ">="
115
+ - !ruby/object:Gem::Version
116
+ version: 3.1.9
117
+ type: :development
118
+ prerelease: false
119
+ version_requirements: !ruby/object:Gem::Requirement
120
+ requirements:
121
+ - - "~>"
122
+ - !ruby/object:Gem::Version
123
+ version: 3.1.0
124
+ - - ">="
125
+ - !ruby/object:Gem::Version
126
+ version: 3.1.9
127
+ - !ruby/object:Gem::Dependency
128
+ name: faraday
129
+ requirement: !ruby/object:Gem::Requirement
130
+ requirements:
131
+ - - "~>"
132
+ - !ruby/object:Gem::Version
133
+ version: 0.11.0
134
+ type: :runtime
135
+ prerelease: false
136
+ version_requirements: !ruby/object:Gem::Requirement
137
+ requirements:
138
+ - - "~>"
139
+ - !ruby/object:Gem::Version
140
+ version: 0.11.0
141
+ - !ruby/object:Gem::Dependency
142
+ name: json
143
+ requirement: !ruby/object:Gem::Requirement
144
+ requirements:
145
+ - - "~>"
146
+ - !ruby/object:Gem::Version
147
+ version: '1.8'
148
+ - - ">="
149
+ - !ruby/object:Gem::Version
150
+ version: 1.8.3
151
+ type: :runtime
152
+ prerelease: false
153
+ version_requirements: !ruby/object:Gem::Requirement
154
+ requirements:
155
+ - - "~>"
156
+ - !ruby/object:Gem::Version
157
+ version: '1.8'
158
+ - - ">="
159
+ - !ruby/object:Gem::Version
160
+ version: 1.8.3
161
+ description: Official Gem to wrap blupee.io API
162
+ email:
163
+ - admin@blupee.io
164
+ executables: []
165
+ extensions: []
166
+ extra_rdoc_files: []
167
+ files:
168
+ - lib/blupee.rb
169
+ - lib/blupee/auth.rb
170
+ - lib/blupee/configuration.rb
171
+ - lib/blupee/ether.rb
172
+ - lib/blupee/http_service.rb
173
+ - lib/blupee/http_service/request.rb
174
+ - lib/blupee/http_service/response.rb
175
+ - lib/blupee/version.rb
176
+ homepage: https://github.com/fogonthedowns/blupee_ruby
177
+ licenses:
178
+ - MIT
179
+ metadata: {}
180
+ post_install_message:
181
+ rdoc_options: []
182
+ require_paths:
183
+ - lib
184
+ required_ruby_version: !ruby/object:Gem::Requirement
185
+ requirements:
186
+ - - ">="
187
+ - !ruby/object:Gem::Version
188
+ version: '0'
189
+ required_rubygems_version: !ruby/object:Gem::Requirement
190
+ requirements:
191
+ - - ">="
192
+ - !ruby/object:Gem::Version
193
+ version: '0'
194
+ requirements: []
195
+ rubyforge_project:
196
+ rubygems_version: 2.6.13
197
+ signing_key:
198
+ specification_version: 4
199
+ summary: Gem to wrap blupee.io API
200
+ test_files: []