localbitcoins 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 328de725d7d4599c74ab908fd9ad80060ffbe506
4
+ data.tar.gz: a2a025486733fb792f1451300e1881f8fbc72d76
5
+ SHA512:
6
+ metadata.gz: 12d1c22f51687f49306df32c0968999a9462981347a9b630e4f6cad74f69943ce0f8216e9ed2d7c5178cfe746bc48e201b537f42f17e7b17e1e694f93e204ed4
7
+ data.tar.gz: 969062b433e58717379b54bbaac91652fc27b45327d4ef8f9f157632b35df77edb710e97dfce622f787914012335a85798db8fc0dda93d4e6c9ffb079e058b1d
data/.gitignore ADDED
@@ -0,0 +1,22 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ coverage
6
+ InstalledFiles
7
+ lib/bundler/man
8
+ pkg
9
+ rdoc
10
+ spec/reports
11
+ test/tmp
12
+ test/version_tmp
13
+ tmp
14
+
15
+ # YARD artifacts
16
+ .yardoc
17
+ _yardoc
18
+ doc/
19
+
20
+ # Vim temp files
21
+ *~
22
+ *.swp
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
data/README.md ADDED
@@ -0,0 +1,102 @@
1
+ # LocalBitcoins API Client
2
+
3
+ This gem provides a simple, extensible Ruby client to access the [LocalBitcoins API](https://localbitcoins.com/api-docs/).
4
+
5
+ ## Installation
6
+
7
+ Install the gem:
8
+ ```
9
+ gem install localbitcoins
10
+ ```
11
+
12
+ Or include it in your Gemfile:
13
+ ```
14
+ gem 'openlibrary'
15
+ ```
16
+
17
+ ## Setting Up Your OAuth Client
18
+
19
+ First, you need to [register your application](https://localbitcoins.com/accounts/api/) with LocalBitcoins to get your API keys. You will get a Client ID and a Client Secret, which you will use to get your OAuth access token. An OAuth access token is required for all requests to the LocalBitcoins API.
20
+
21
+ There are a number of ways to implement OAuth, and it is largely left up to you to decide how to do it. If you've never used OAuth before, reading [this tutorial](http://aaronparecki.com/articles/2012/07/29/1/oauth2-simplified) is a good place to start!
22
+
23
+ Once you have your token, you can set up your client with the following code:
24
+
25
+ ``` ruby
26
+ # long version
27
+ client = LocalBitcoins::Client.new(oauth_token: 'OAUTH_TOKEN')
28
+ # short version
29
+ client = LocalBitcoins.new(oauth_token: 'OAUTH_TOKEN')
30
+ ```
31
+
32
+ ### Global Configuration
33
+
34
+ To make things easier, you can define your client credentials at a global level:
35
+
36
+ ``` ruby
37
+ # Set the configuration
38
+ LocalBitcoins.configure(
39
+ client_id: 'ID',
40
+ client_secret: 'SECRET'
41
+ )
42
+
43
+ # Get the configuration
44
+ LocalBitcoins.configuration # => { client_id => 'ID', client_secret => 'SECRET' }
45
+
46
+ # Reset the configuration
47
+ LocalBitcoins.reset_configuration
48
+ ```
49
+
50
+ If you're using Rails, you can set the configuration with an initializer file in `config/initializers`.
51
+
52
+ REMEMBER: Keep your client credentials secret! Do not include them in git repos, or place them anywhere that users can see them. If you suspect that your credentials have been accessed by someone else, immediately reset your client secret from your LocalBitcoins Apps Dashboard.
53
+
54
+ There are several ways to solve the problem of keeping your credentials secret. For Rails apps, I like to store them as environment variables and access them with [figaro](https://github.com/laserlemon/figaro).
55
+
56
+ ## Usage
57
+
58
+ You can get a list of the token owner's releaseable escrows through the OAuth client. You can also use the client to release an escrow.
59
+
60
+ ### View Releaseable Escrows
61
+
62
+ ``` ruby
63
+ escrows = client.escrows
64
+
65
+ escrows.each do |e|
66
+ e.data.created_at # => UTC datetime escrow was created at
67
+ e.data.buyer_username # => username of the buyer
68
+ e.data.reference_code # => reference code for the escrow
69
+
70
+ e.actions.release_url # => url to release to escrow
71
+ end
72
+ ```
73
+
74
+ Use the release_url from `escrows` method in the `escrow_release` method below:
75
+
76
+ ### Release An Escrow
77
+
78
+ ``` ruby
79
+ # returns a complimentary message if the escrow successfully released
80
+ release = client.escrow_release(release_url)
81
+ ```
82
+
83
+ ### List Ads
84
+
85
+ You can get a list of the token owner's ads with the following method:
86
+
87
+ ``` ruby
88
+ ads = client.ads
89
+
90
+ ads.each do |a|
91
+ a.data.visible # => boolean value of the ad's visibility
92
+ a.data.email # => valid e-mail string or null
93
+ a.data.location_string # => human-readable location
94
+ # and many more pieces of data!
95
+ end
96
+ ```
97
+
98
+ For a full list of info you can get with this method, view the [API documentation](https://localbitcoins.com/api-docs/).
99
+
100
+ ## License
101
+
102
+ All code is released under the MIT License.
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
+ RSpec::Core::RakeTask.new('spec')
4
+
5
+ RSpec::Core::RakeTask.new(:test) do |t|
6
+ t.pattern = 'spec/*_spec.rb'
7
+ t.verbose = false
8
+ end
9
+
10
+ task :default => :test
@@ -0,0 +1,24 @@
1
+ module LocalBitcoins
2
+ module Ads
3
+ # Get a list of the token owner's ads
4
+ #
5
+ def ads()
6
+ data = oauth_request(:get, '/api/ads')
7
+ Hashie::Mash.new(data['ad_list'])
8
+ end
9
+
10
+ # Update one of the token owner's ads
11
+ #
12
+ # id - id of the ad you want to update
13
+ # visibility - the ad's visibility [boolean]
14
+ # min_amount - minimum selling price [string or nil]
15
+ # max_amount - maximum buying price [string or nil]
16
+ # price_equation - equation to calculate price [string]
17
+ #
18
+ # NOTE: Omitting min_amount or max_amount will unset them.
19
+ #
20
+ def update_ad(id, visibility, min_amount, max_amount, price_equation)
21
+ # Can't use this method until the Ads API is complete!
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,21 @@
1
+ module LocalBitcoins
2
+ module Escrows
3
+ # Get a list of the token owner's releaseable escrows
4
+ #
5
+ def escrows()
6
+ data = oauth_request(:get, '/api/escrows')
7
+ Hashie::Mash.new(data['escrow_list'])
8
+ end
9
+
10
+ # Release an escrow
11
+ #
12
+ # release_url => the url of the escrow you want to release,
13
+ # probably found by running the `escrows`
14
+ # method above
15
+ #
16
+ def escrow_release(release_url)
17
+ data = oauth_request(:post, release_url)
18
+ data['data']['message']
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,27 @@
1
+ require 'localbitcoins/client'
2
+ require 'localbitcoins/client/escrows'
3
+ require 'localbitcoins/client/ads'
4
+
5
+ module LocalBitcoins
6
+ class Client
7
+ include LocalBitcoins::Escrows
8
+ include LocalBitcoins::Ads
9
+
10
+ attr_reader :oauth_token
11
+
12
+ # Initialize a LocalBitcoins::Client instance
13
+ #
14
+ # All API calls to LocalBitcoins require an OAuth token, so you
15
+ # need to include one when you initialize the client.
16
+ #
17
+ # options[:oauth_token]
18
+ #
19
+ def initialize(options={})
20
+ unless options.kind_of?(Hash)
21
+ raise ArgumentError, "Options hash required."
22
+ end
23
+
24
+ @oauth_token = options[:oauth_token]
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,6 @@
1
+ module LocalBitcoins
2
+ class Error < StandardError; end
3
+ class ConfigurationError < Error; end
4
+ class Unauthorized < Error; end
5
+ class NotFound < Error; end
6
+ end
@@ -0,0 +1,38 @@
1
+ require 'rest-client'
2
+ require 'active_support/core_ext'
3
+ require 'json'
4
+ require 'hashie'
5
+
6
+ module LocalBitcoins
7
+ module Request
8
+
9
+ protected
10
+
11
+ # Perform an OAuth API request. The client must be initialized
12
+ # with a valid OAuth access token. All API requests to
13
+ # LocalBitcoins currently require an access token.
14
+ #
15
+ # path - Request path
16
+ # params - Parameters hash
17
+ #
18
+ def oauth_request(http_method, path, params={})
19
+ raise 'OAuth access token required!' unless @oauth_token
20
+ params.merge!('Accept'=>'application/json')
21
+ resp = @oauth_token.request(http_method, path, params)
22
+
23
+ case resp
24
+ when Net::HTTPUnauthorized
25
+ raise LocalBitcoins::Unauthorized
26
+ when Net::HTTPNotFound
27
+ raise LocalBitcoins::NotFound
28
+ end
29
+
30
+ parse(resp)
31
+ end
32
+
33
+ def parse(resp)
34
+ object = JSON.parse(resp.body)
35
+ object
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,5 @@
1
+ module LocalBitcoins
2
+ unless defined?(LocalBitcoins::VERSION)
3
+ VERSION = '0.0.2'
4
+ end
5
+ end
@@ -0,0 +1,41 @@
1
+ require 'localbitcoins/version'
2
+ require 'localbitcoins/errors'
3
+ require 'localbitcoins/request'
4
+ require 'localbitcoins/client'
5
+
6
+ module LocalBitcoins
7
+ @@options = {}
8
+
9
+ # Create a new LocalBitcoins::Client instance
10
+ #
11
+ def self.new(options={})
12
+ LocalBitcoins::Client.new(options)
13
+ end
14
+
15
+ # Define a global configuration
16
+ #
17
+ # options[:client_id]
18
+ # options[:client_secret]
19
+ #
20
+ def self.configure(options={})
21
+ unless options.kind_of?(Hash)
22
+ raise ArgumentError, "Options hash required."
23
+ end
24
+
25
+ @@options[:client_id] = options[:client_id]
26
+ @@options[:client_secret] = options[:client_secret]
27
+ @@options
28
+ end
29
+
30
+ # Returns global configuration hash
31
+ #
32
+ def self.configuration
33
+ @@options
34
+ end
35
+
36
+ # Resets the global configuration
37
+ #
38
+ def self.reset_configuration
39
+ @@options = {}
40
+ end
41
+ end
@@ -0,0 +1,27 @@
1
+ require File.expand_path('../lib/localbitcoins/version', __FILE__)
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = "localbitcoins"
5
+ s.version = LocalBitcoins::VERSION.dup
6
+ s.summary = "LocalBitcoins API wrapper"
7
+ s.description = "Ruby wrapper for the LocalBitcoins API"
8
+ s.homepage = "http://github.com/pemulis/localbitcoins"
9
+ s.authors = ["John Shutt"]
10
+ s.email = ["john.d.shutt@gmail.com"]
11
+ s.homepage = "http://shutt.in"
12
+ s.license = "MIT"
13
+
14
+ s.add_development_dependency 'rspec', '~> 2.13'
15
+ s.add_development_dependency 'webmock', '~> 1.11'
16
+
17
+ s.add_runtime_dependency 'json', '~> 1.8.0'
18
+ s.add_runtime_dependency 'rest-client', '~> 1.6'
19
+ s.add_runtime_dependency 'hashie', '~> 2.0.2'
20
+ s.add_runtime_dependency 'activesupport', '~> 3'
21
+ s.add_runtime_dependency 'oauth', '~> 0.4'
22
+
23
+ s.files = `git ls-files`.split("\n")
24
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
25
+ s.executables = `git ls-files -- bin/*`.split("\n").map{|f| File.basename(f)}
26
+ s.require_paths = ["lib"]
27
+ end
@@ -0,0 +1,6 @@
1
+ require 'spec_helper'
2
+ require 'oauth'
3
+
4
+ describe 'Client' do
5
+
6
+ end
@@ -0,0 +1,4 @@
1
+ require 'spec-helper'
2
+
3
+ describe 'LocalBitcoins' do
4
+ end
@@ -0,0 +1,10 @@
1
+ $:.unshift File.expand_path("../..", __FILE__)
2
+
3
+ require 'localbitcoins'
4
+ require 'rest-client'
5
+ require 'webmock'
6
+ require 'webmock/rspec'
7
+
8
+ RSpec.configure do |config|
9
+ config.color_enabled = true
10
+ end
metadata ADDED
@@ -0,0 +1,157 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: localbitcoins
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - John Shutt
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-07-12 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rspec
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '2.13'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '2.13'
27
+ - !ruby/object:Gem::Dependency
28
+ name: webmock
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: '1.11'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: '1.11'
41
+ - !ruby/object:Gem::Dependency
42
+ name: json
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ~>
46
+ - !ruby/object:Gem::Version
47
+ version: 1.8.0
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: 1.8.0
55
+ - !ruby/object:Gem::Dependency
56
+ name: rest-client
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: '1.6'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ~>
67
+ - !ruby/object:Gem::Version
68
+ version: '1.6'
69
+ - !ruby/object:Gem::Dependency
70
+ name: hashie
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ~>
74
+ - !ruby/object:Gem::Version
75
+ version: 2.0.2
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ~>
81
+ - !ruby/object:Gem::Version
82
+ version: 2.0.2
83
+ - !ruby/object:Gem::Dependency
84
+ name: activesupport
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ~>
88
+ - !ruby/object:Gem::Version
89
+ version: '3'
90
+ type: :runtime
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ~>
95
+ - !ruby/object:Gem::Version
96
+ version: '3'
97
+ - !ruby/object:Gem::Dependency
98
+ name: oauth
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ~>
102
+ - !ruby/object:Gem::Version
103
+ version: '0.4'
104
+ type: :runtime
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ~>
109
+ - !ruby/object:Gem::Version
110
+ version: '0.4'
111
+ description: Ruby wrapper for the LocalBitcoins API
112
+ email:
113
+ - john.d.shutt@gmail.com
114
+ executables: []
115
+ extensions: []
116
+ extra_rdoc_files: []
117
+ files:
118
+ - .gitignore
119
+ - Gemfile
120
+ - README.md
121
+ - Rakefile
122
+ - lib/localbitcoins.rb
123
+ - lib/localbitcoins/client.rb
124
+ - lib/localbitcoins/client/ads.rb
125
+ - lib/localbitcoins/client/escrows.rb
126
+ - lib/localbitcoins/errors.rb
127
+ - lib/localbitcoins/request.rb
128
+ - lib/localbitcoins/version.rb
129
+ - localbitcoins.gemspec
130
+ - spec/client_spec.rb
131
+ - spec/localbitcoins_spec.rb
132
+ - spec/spec_helper.rb
133
+ homepage: http://shutt.in
134
+ licenses:
135
+ - MIT
136
+ metadata: {}
137
+ post_install_message:
138
+ rdoc_options: []
139
+ require_paths:
140
+ - lib
141
+ required_ruby_version: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - '>='
144
+ - !ruby/object:Gem::Version
145
+ version: '0'
146
+ required_rubygems_version: !ruby/object:Gem::Requirement
147
+ requirements:
148
+ - - '>='
149
+ - !ruby/object:Gem::Version
150
+ version: '0'
151
+ requirements: []
152
+ rubyforge_project:
153
+ rubygems_version: 2.0.3
154
+ signing_key:
155
+ specification_version: 4
156
+ summary: LocalBitcoins API wrapper
157
+ test_files: []