localbitcoins 0.0.4 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +13 -5
- data/.gitignore +2 -0
- data/Gemfile.lock +16 -19
- data/README.md +319 -35
- data/Rakefile +1 -1
- data/lib/localbitcoins/client/ads.rb +45 -9
- data/lib/localbitcoins/client/contacts.rb +66 -0
- data/lib/localbitcoins/client/escrows.rb +8 -12
- data/lib/localbitcoins/client/markets.rb +27 -0
- data/lib/localbitcoins/client/public.rb +78 -0
- data/lib/localbitcoins/client/users.rb +16 -0
- data/lib/localbitcoins/client/wallet.rb +28 -0
- data/lib/localbitcoins/client.rb +13 -1
- data/lib/localbitcoins/request.rb +11 -15
- data/lib/localbitcoins/version.rb +1 -1
- data/localbitcoins.gemspec +3 -3
- data/spec/client_spec.rb +397 -8
- data/spec/fixtures/account_info.json +14 -0
- data/spec/fixtures/ad_create.json +5 -0
- data/spec/fixtures/ad_list.json +103 -0
- data/spec/fixtures/ad_single.json +55 -0
- data/spec/fixtures/ad_update.json +5 -0
- data/spec/fixtures/ads.json +103 -0
- data/spec/fixtures/contact_message.json +5 -0
- data/spec/fixtures/contacts.json +52 -0
- data/spec/fixtures/contacts_active.json +165 -0
- data/spec/fixtures/contacts_active_buyers.json +60 -0
- data/spec/fixtures/contacts_active_sellers.json +111 -0
- data/spec/fixtures/contacts_cancel.json +5 -0
- data/spec/fixtures/contacts_canceled_contacts.json +162 -0
- data/spec/fixtures/contacts_closed_contacts.json +109 -0
- data/spec/fixtures/contacts_contact_info.json +52 -0
- data/spec/fixtures/contacts_contacts_info.json +111 -0
- data/spec/fixtures/contacts_create.json +10 -0
- data/spec/fixtures/contacts_messages.json +43 -0
- data/spec/fixtures/contacts_released_contacts.json +60 -0
- data/spec/fixtures/currencies.json +505 -0
- data/spec/fixtures/currency_ticker.json +236 -0
- data/spec/fixtures/escrow_release.json +5 -0
- data/spec/fixtures/escrows.json +24 -22
- data/spec/fixtures/local_buy_ads.json +93 -0
- data/spec/fixtures/local_sell_ads.json +93 -0
- data/spec/fixtures/logout.json +0 -0
- data/spec/fixtures/myself.json +14 -0
- data/spec/fixtures/online_buy_ads.json +637 -0
- data/spec/fixtures/online_sell_ads.json +2160 -0
- data/spec/fixtures/orderbook.json +1739 -0
- data/spec/fixtures/payment_methods.json +95 -0
- data/spec/fixtures/places.json +15 -0
- data/spec/fixtures/ticker.json +254 -0
- data/spec/fixtures/trades.json +3002 -0
- data/spec/fixtures/wallet.json +34 -0
- data/spec/fixtures/wallet_addr.json +6 -0
- data/spec/fixtures/wallet_balance.json +16 -0
- data/spec/fixtures/wallet_send.json +5 -0
- data/spec/spec_helper.rb +26 -29
- metadata +51 -7
@@ -0,0 +1,27 @@
|
|
1
|
+
require 'open-uri'
|
2
|
+
module LocalBitcoins
|
3
|
+
module Markets
|
4
|
+
|
5
|
+
# LocalBitcoins.com provides public trade data available for market chart services and tickers
|
6
|
+
# The API is in bitcoincharts.com format
|
7
|
+
# Currently supported currencies:
|
8
|
+
# ARS, AUD, BRL, CAD, CHF, CZK, DKK, EUR, GBP, HKD, ILS, INR, MXN, NOK, NZD, PLN, RUB, SEK, SGD, THB, USD, ZAR
|
9
|
+
|
10
|
+
ROOT = 'https://localbitcoins.com'
|
11
|
+
|
12
|
+
def ticker
|
13
|
+
ticker_uri = open("#{ROOT}/bitcoinaverage/ticker-all-currencies/")
|
14
|
+
Hashie::Mash.new(JSON.parse(ticker_uri.read)) if ticker_uri.status.first=='200'
|
15
|
+
end
|
16
|
+
|
17
|
+
def trades(currency, last_tid=nil)
|
18
|
+
trade_uri = open("#{ROOT}/bitcoincharts/#{currency}/trades.json?since=#{last_tid}")
|
19
|
+
JSON.parse(trade_uri.read) if trade_uri.status.first=='200'
|
20
|
+
end
|
21
|
+
|
22
|
+
def orderbook(currency)
|
23
|
+
orderbook_uri = open("#{ROOT}/bitcoincharts/#{currency}/orderbook.json")
|
24
|
+
Hashie::Mash.new(JSON.parse(orderbook_uri.read)) if orderbook_uri.status.first=='200'
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,78 @@
|
|
1
|
+
require 'open-uri'
|
2
|
+
module LocalBitcoins
|
3
|
+
module Public
|
4
|
+
ROOT = 'https://localbitcoins.com'
|
5
|
+
|
6
|
+
# Valid API endpoints include:
|
7
|
+
# /buy-bitcoins-online/{countrycode:2}/{country_name}/{payment_method}/.json
|
8
|
+
# /buy-bitcoins-online/{countrycode:2}/{country_name}/.json
|
9
|
+
# /buy-bitcoins-online/{currency:3}/{payment_method}/.json
|
10
|
+
# /buy-bitcoins-online/{currency:3}/.json
|
11
|
+
# /buy-bitcoins-online/{payment_method}/.json
|
12
|
+
# /buy-bitcoins-online/.json
|
13
|
+
#
|
14
|
+
# NOTE: countrycode must be 2 characters and currency must be 3 characters
|
15
|
+
#
|
16
|
+
def online_buy_ads_lookup(params={})
|
17
|
+
params.each_key do |k|
|
18
|
+
params[k]<<'/'
|
19
|
+
end
|
20
|
+
online_buy_ads_uri = open("#{ROOT}/buy-bitcoins-online/#{params[:countrycode]}#{params[:currency]}#{params[:country_name]}#{params[:payment_method]}.json")
|
21
|
+
Hashie::Mash.new(JSON.parse(online_buy_ads_uri.read)) if online_buy_ads_uri.status.first=='200'
|
22
|
+
end
|
23
|
+
|
24
|
+
# NOTE: Same format as online_buy_ads_lookup, but buy is replaced with sell
|
25
|
+
#
|
26
|
+
def online_sell_ads_lookup(params={})
|
27
|
+
params.each do |k,v|
|
28
|
+
params[k]=v<<'/' unless v.nil?
|
29
|
+
end
|
30
|
+
online_sell_ads_uri = open("#{ROOT}/sell-bitcoins-online/#{params[:countrycode]}#{params[:currency]}#{params[:country_name]}#{params[:payment_method]}.json")
|
31
|
+
Hashie::Mash.new(JSON.parse(online_sell_ads_uri.read)) if online_sell_ads_uri.status.first=='200'
|
32
|
+
end
|
33
|
+
|
34
|
+
# - Required fields -
|
35
|
+
# location_id - id for location found using places method
|
36
|
+
# location_slug - slug name for location found using places method
|
37
|
+
#
|
38
|
+
# - Optional fields -
|
39
|
+
# lat - latitude of location [float]
|
40
|
+
# lon - longitude of location [float]
|
41
|
+
#
|
42
|
+
def local_buy_ad(params={})
|
43
|
+
local_buy_ad_uri = open("#{ROOT}/buy-bitcoins-with-cash/#{params[:location_id]}/#{params[:location_slug].downcase}/.json?lat=#{params[:lat]}&lon=#{params[:lon]}")
|
44
|
+
Hashie::Mash.new(JSON.parse(local_buy_ad_uri.read)) if local_buy_ad_uri.status.first=='200'
|
45
|
+
end
|
46
|
+
|
47
|
+
# NOTE: Same format as local_buy_ad, but buy is replaced with sell
|
48
|
+
#
|
49
|
+
def local_sell_ad(params={})
|
50
|
+
local_sell_ad_uri = open("#{ROOT}/sell-bitcoins-with-cash/#{params[:location_id]}/#{params[:location_slug].downcase}/.json?lat=#{params[:lat]}&lon=#{params[:lon]}")
|
51
|
+
Hashie::Mash.new(JSON.parse(local_sell_ad_uri.read)) if local_sell_ad_uri.status.first=='200'
|
52
|
+
end
|
53
|
+
|
54
|
+
def payment_methods(countrycode=nil)
|
55
|
+
countrycode<<'/' unless countrycode.nil?
|
56
|
+
payment_methods_uri = open("#{ROOT}/api/payment_methods/#{countrycode}")
|
57
|
+
Hashie::Mash.new(JSON.parse(payment_methods_uri.read)).data if payment_methods_uri.status.first=='200'
|
58
|
+
end
|
59
|
+
|
60
|
+
def currencies
|
61
|
+
currencies_uri = open("#{ROOT}/api/currencies/")
|
62
|
+
Hashie::Mash.new(JSON.parse(currencies_uri.read)).data if currencies_uri.status.first=='200'
|
63
|
+
end
|
64
|
+
|
65
|
+
# - Required fields -
|
66
|
+
# lat - latitude of location [float]
|
67
|
+
# lon - longitude of location [float]
|
68
|
+
#
|
69
|
+
# - Optional fields -
|
70
|
+
# countrycode - 2 letter countrycode
|
71
|
+
# location_string - location name in string form
|
72
|
+
#
|
73
|
+
def places(params={})
|
74
|
+
places_uri = open("#{ROOT}/api/places/?#{params.to_query}")
|
75
|
+
Hashie::Mash.new(JSON.parse(places_uri.read)).data if places_uri.status.first=='200'
|
76
|
+
end
|
77
|
+
end
|
78
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
module LocalBitcoins
|
2
|
+
module Users
|
3
|
+
def myself
|
4
|
+
oauth_request(:get, '/api/myself/').data
|
5
|
+
end
|
6
|
+
|
7
|
+
def account_info(username)
|
8
|
+
oauth_request(:get, "/api/account_info/#{username}/").data
|
9
|
+
end
|
10
|
+
|
11
|
+
# immediately expires the currently authorized access_token
|
12
|
+
def logout
|
13
|
+
oauth_request(:post, '/api/logout/')
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
module LocalBitcoins
|
2
|
+
module Wallet
|
3
|
+
# Gets information about the token owner's wallet balance
|
4
|
+
def wallet
|
5
|
+
oauth_request(:get, '/api/wallet/').data
|
6
|
+
end
|
7
|
+
|
8
|
+
def wallet_balance
|
9
|
+
oauth_request(:get, '/api/wallet-balance/').data
|
10
|
+
end
|
11
|
+
|
12
|
+
def wallet_send(address, amount)
|
13
|
+
oauth_request(:post, '/api/wallet-send/', {:address=>address, :amount=>amount}).data
|
14
|
+
end
|
15
|
+
|
16
|
+
def wallet_pin_send(address, amount, pin)
|
17
|
+
oauth_request(:post, '/api/wallet-send/', {:address=>address, :amount=>amount, :pin=>pin}).data if valid_pin?(pin)
|
18
|
+
end
|
19
|
+
|
20
|
+
def valid_pin?(pin)
|
21
|
+
oauth_request(:post, '/api/pincode/', {:pincode=>pin}).data.pincode_ok
|
22
|
+
end
|
23
|
+
|
24
|
+
def wallet_addr
|
25
|
+
oauth_request(:post, '/api/wallet-addr/').data
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
data/lib/localbitcoins/client.rb
CHANGED
@@ -1,12 +1,24 @@
|
|
1
1
|
require 'localbitcoins/client'
|
2
2
|
require 'localbitcoins/client/escrows'
|
3
3
|
require 'localbitcoins/client/ads'
|
4
|
+
require 'localbitcoins/client/users'
|
5
|
+
require 'localbitcoins/client/contacts'
|
6
|
+
require 'localbitcoins/client/markets'
|
7
|
+
require 'localbitcoins/client/wallet'
|
8
|
+
require 'localbitcoins/client/public'
|
4
9
|
|
5
10
|
module LocalBitcoins
|
6
11
|
class Client
|
7
12
|
include LocalBitcoins::Request
|
8
13
|
include LocalBitcoins::Escrows
|
9
14
|
include LocalBitcoins::Ads
|
15
|
+
include LocalBitcoins::Users
|
16
|
+
include LocalBitcoins::Contacts
|
17
|
+
include LocalBitcoins::Markets
|
18
|
+
include LocalBitcoins::Wallet
|
19
|
+
include LocalBitcoins::Contacts
|
20
|
+
include LocalBitcoins::Public
|
21
|
+
|
10
22
|
|
11
23
|
attr_reader :oauth_client, :access_token
|
12
24
|
|
@@ -26,7 +38,7 @@ module LocalBitcoins
|
|
26
38
|
options[:client_secret],
|
27
39
|
authorize_url: "/oauth2/authorize",
|
28
40
|
token_url: "/oauth2/access_token",
|
29
|
-
site: "https://
|
41
|
+
site: "https://localbitcoins.com"
|
30
42
|
)
|
31
43
|
|
32
44
|
@access_token = OAuth2::AccessToken.new(
|
@@ -6,35 +6,31 @@ require 'oauth2'
|
|
6
6
|
|
7
7
|
module LocalBitcoins
|
8
8
|
module Request
|
9
|
-
API_URL = "https://
|
9
|
+
API_URL = "https://localbitcoins.com"
|
10
10
|
|
11
11
|
protected
|
12
12
|
|
13
13
|
# Perform an OAuth API request. The client must be initialized
|
14
|
-
# with a valid OAuth access token
|
15
|
-
# LocalBitcoins currently require an access token.
|
14
|
+
# with a valid OAuth access token to make requests with this method
|
16
15
|
#
|
17
16
|
# path - Request path
|
18
|
-
#
|
17
|
+
# body - Parameters for requests - GET and POST
|
19
18
|
#
|
20
|
-
def oauth_request(http_method, path,
|
19
|
+
def oauth_request(http_method, path, body={})
|
21
20
|
raise 'OAuth access token required!' unless @access_token
|
22
|
-
params
|
23
|
-
|
24
|
-
|
21
|
+
params = { :Accept =>'application/json', :access_token => @access_token.token }
|
22
|
+
params.merge!(body) if http_method == :get
|
23
|
+
resp = @access_token.request(http_method, path, :params => params, :body => body)
|
25
24
|
case resp
|
26
25
|
when Net::HTTPUnauthorized
|
27
26
|
raise LocalBitcoins::Unauthorized
|
28
27
|
when Net::HTTPNotFound
|
29
28
|
raise LocalBitcoins::NotFound
|
30
29
|
end
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
def parse(resp)
|
36
|
-
object = JSON.parse(resp.body)
|
37
|
-
object
|
30
|
+
hash = Hashie::Mash.new(JSON.parse(resp.body))
|
31
|
+
raise Error.new(hash.error) if hash.error
|
32
|
+
raise Error.new(hash.errors.join(',')) if hash.errors
|
33
|
+
hash
|
38
34
|
end
|
39
35
|
end
|
40
36
|
end
|
data/localbitcoins.gemspec
CHANGED
@@ -6,8 +6,8 @@ Gem::Specification.new do |s|
|
|
6
6
|
s.summary = "LocalBitcoins API wrapper"
|
7
7
|
s.description = "Ruby wrapper for the LocalBitcoins API"
|
8
8
|
s.homepage = "http://github.com/pemulis/localbitcoins"
|
9
|
-
s.authors = ["John Shutt"]
|
10
|
-
s.email = ["john.d.shutt@gmail.com"]
|
9
|
+
s.authors = ["John Shutt", "Will Newman", "Albert Brown"]
|
10
|
+
s.email = ["john.d.shutt@gmail.com","will.newman@rutgers.edu", "albert_brown@brown.edu"]
|
11
11
|
s.homepage = "http://shutt.in"
|
12
12
|
s.license = "MIT"
|
13
13
|
|
@@ -18,7 +18,7 @@ Gem::Specification.new do |s|
|
|
18
18
|
s.add_runtime_dependency 'rest-client', '~> 1.6'
|
19
19
|
s.add_runtime_dependency 'hashie', '~> 2.0.2'
|
20
20
|
s.add_runtime_dependency 'activesupport', '~> 3'
|
21
|
-
s.add_runtime_dependency 'oauth2', '~> 0.9.
|
21
|
+
s.add_runtime_dependency 'oauth2', '~> 0.9.4'
|
22
22
|
|
23
23
|
s.files = `git ls-files`.split("\n")
|
24
24
|
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|