ebayr 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,18 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ *.sw?
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in ebayr.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 JJ Buckley
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,86 @@
1
+ # Ebayr
2
+
3
+ Ebayr is a small gem which makes it a little easier to use the eBay Trading API
4
+ with Ruby.
5
+
6
+ ## Installation
7
+
8
+ Add this line to your application's Gemfile:
9
+
10
+ gem 'ebayr'
11
+
12
+ And then execute:
13
+
14
+ $ bundle
15
+
16
+ Or install it yourself as:
17
+
18
+ $ gem install ebayr
19
+
20
+ ## Usage
21
+
22
+ To use the eBay Trading API, you'll need a developer keyset. Sign up at
23
+ http://developer.ebay.com if you haven't already done so.
24
+
25
+ Next, you'll need to require Ebayr, and tell it to use your keys. You will also
26
+ need to generate an RUName, and get the key for that. (This is all standard
27
+ stuff - look at the eBay developer docs for details).
28
+
29
+ ```ruby
30
+ require 'ebayr'
31
+
32
+ Ebay.dev_id = "my-dev-id"
33
+
34
+ # This is only needed if you want to retrieve user tokens
35
+ Ebay.authorization_callback_url = "https://my-site/callback-url"
36
+
37
+ Ebay.auth_token = "myverylongebayauthtoken"
38
+
39
+ Ebay.app_id = "my-ebay-app-id"
40
+
41
+ Ebay.cert_id = "my-ebay-cert-id"
42
+
43
+ Ebay.ru_name = "my-ebay-ru-name"
44
+
45
+ # Set this to true for testing in the eBay Sandbox (but remember to use the
46
+ # appropriate keys!). It's true by default.
47
+ Ebay.sandbox = false
48
+ ```
49
+
50
+ Now you're ready to make calls
51
+
52
+ ```ruby
53
+ Ebayr.call(:GeteBayOfficialTime)
54
+ ```
55
+
56
+ To use an authorized user's key, pass in an `auth_token` parameter
57
+ ```ruby
58
+ Ebayr.call(:GetOrders, :auth_token => "another-ebay-auth-token")
59
+ ```
60
+
61
+ ### Configuration
62
+
63
+ Ebayr will look for the following Ruby files, and load them *once* in order (if
64
+ they exist) when the module is evaluated:
65
+
66
+ 1. /etc/ebayrc.conf
67
+ 2. /usr/local/etc/ebayrc.conf
68
+ 3. ~/.ebayrc.conf
69
+ 4. ./.ebayrc.conf
70
+
71
+ You can put configuration code in there (such as the variable setting shown
72
+ above). The files should be plain old Ruby.
73
+
74
+ In a Ruby on Rails project, just create a file called
75
+ config/initializers/ebayr.rb (or something), and put the configuration there. Of
76
+ course, you should probably not check in these files, if you're using a public
77
+ repository.
78
+
79
+ ## Contributing
80
+
81
+ 1. Fork it
82
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
83
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
84
+ 4. Push to the branch (`git push origin my-new-feature`)
85
+
86
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
3
+ require "rake/testtask"
4
+
5
+ Rake::TestTask.new do |t|
6
+ t.libs << "test"
7
+ t.test_files = FileList["test/test*.rb"]
8
+ end
9
+
10
+ task :default => :test
data/ebayr.gemspec ADDED
@@ -0,0 +1,19 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/ebayr/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["JJ Buckley"]
6
+ gem.email = ["jj@bjjb.org"]
7
+ gem.description = %q{A tidy library for using the eBay Trading API with Ruby}
8
+ gem.summary = %q{eBayR is a gem that makes it (relatively) easy to use the eBay Trading API from Ruby. Includes a self-contained XML parser, a flexible callback system, and easy integration into Rails.}
9
+ gem.homepage = "http://jjbuckley.github.com/ebayr"
10
+
11
+ gem.files = `git ls-files`.split($\)
12
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
13
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
14
+ gem.name = "ebayr"
15
+ gem.require_paths = ["lib"]
16
+ gem.version = Ebayr::VERSION
17
+ gem.add_dependency 'activesupport'
18
+ gem.add_development_dependency 'rake'
19
+ end
@@ -0,0 +1,3 @@
1
+ module Ebayr
2
+ VERSION = "0.0.1"
3
+ end
data/lib/ebayr.rb ADDED
@@ -0,0 +1,167 @@
1
+ # -*- encoding : utf-8 -*-
2
+ require "ebayr/version"
3
+ require 'net/https'
4
+ require 'active_support/core_ext/module/attribute_accessors'
5
+ require 'active_support/core_ext/hash/conversions'
6
+ require 'active_support/buffered_logger'
7
+
8
+ module Ebayr
9
+ mattr_accessor :dev_id,
10
+ :app_id,
11
+ :cert_id,
12
+ :ru_name,
13
+ :auth_token,
14
+ :sandbox,
15
+ :authorization_callback_url,
16
+ :callbacks,
17
+ :site_id,
18
+ :compatability_level,
19
+ :logger
20
+
21
+ @@logger ||= if defined?(Rails)
22
+ Rails.logger
23
+ else
24
+ ActiveSupport::BufferedLogger.new(STDOUT)
25
+ end
26
+
27
+ %W(/etc/ebayrc.conf /usr/local/etc/ebayrc.conf ~/.ebayrc.conf ./.ebayrc.conf).each do |path|
28
+ load path if File.exists?(path = File.expand_path(path))
29
+ end
30
+
31
+
32
+ @@site_id ||= 0 # US
33
+ @@compatability_level ||= 745
34
+
35
+ def self.sandbox?
36
+ !!sandbox
37
+ end
38
+
39
+ # Gets the URI used for calls
40
+ def self.uri
41
+ URI::parse("https://api#{sandbox ? ".sandbox" : ""}.ebay.com/ws/api.dll")
42
+ end
43
+
44
+ def self.xml(structure)
45
+ case structure
46
+ when Hash then structure.map { |k, v| "<#{k.to_s}>#{xml(v)}</#{k.to_s}>" }.join
47
+ when Array then structure.map { |v| xml(v) }
48
+ else structure.to_s
49
+ end
50
+ end
51
+
52
+ # Make an eBay call (symbol or string). You can pass in these arguments:
53
+ #
54
+ # auth_token:: to use a user's token instead of the general token
55
+ # site_id:: to use a specific eBay site (default is 0, which is US ebay.com)
56
+ # compatability_level:: declare another eBay Trading API compatability_level
57
+ #
58
+ # All other arguments are passed into the API call, and may be nested.
59
+ #
60
+ # Remember, case matters.
61
+ #
62
+ # call(:GeteBayOfficialTime)
63
+ #
64
+ # The response is a Hash of the response, deserialized from the XML by
65
+ # ActiveSupport's XML deserializer.
66
+ def self.call(call, arguments = {})
67
+ call = call.to_s
68
+
69
+ auth_token = arguments.delete(:auth_token) || self.auth_token.to_s
70
+ site_id = arguments.delete(:site_id) || self.site_id.to_s
71
+ compatability_level = arguments.delete(:compatability_level) || self.compatability_level.to_s
72
+
73
+ headers = {
74
+ 'X-EBAY-API-COMPATIBILITY-LEVEL' => compatability_level.to_s,
75
+ 'X-EBAY-API-DEV-NAME' => dev_id.to_s,
76
+ 'X-EBAY-API-APP-NAME' => app_id.to_s,
77
+ 'X-EBAY-API-CERT-NAME' => cert_id.to_s,
78
+ 'X-EBAY-API-CALL-NAME' => call.to_s,
79
+ 'X-EBAY-API-SITEID' => site_id.to_s,
80
+ 'Content-Type' => 'text/xml'
81
+ }
82
+
83
+ xml = xml(arguments)
84
+
85
+ xml = <<-XML
86
+ <?xml version="1.0" encoding="utf-8"?>
87
+ <#{call}Request xmlns="urn:ebay:apis:eBLBaseComponents">
88
+ <RequesterCredentials>
89
+ <eBayAuthToken>#{auth_token}</eBayAuthToken>
90
+ </RequesterCredentials>
91
+ #{xml}
92
+ </#{call}Request>
93
+ XML
94
+
95
+ request = Net::HTTP::Post.new(uri.path, headers)
96
+
97
+ request.body = xml.to_s
98
+
99
+ http = Net::HTTP.new(uri.host, uri.port)
100
+
101
+ if uri.port == 443
102
+ http.use_ssl = true
103
+ http.verify_mode = OpenSSL::SSL::VERIFY_NONE
104
+ end
105
+
106
+ response = http.start { |http| http.request(request) }
107
+
108
+ if callbacks
109
+ callbacks.each do |callback|
110
+ if callback.is_a?(Symbol)
111
+ send(callback, request, response)
112
+ elsif callback.respond_to?(:call)
113
+ callback.call(request, response)
114
+ else
115
+ throw Error.new("Invalid callback: #{callback.to_s}")
116
+ end
117
+ end
118
+ end
119
+
120
+ case response
121
+ when Net::HTTPSuccess
122
+ result = Hash.from_xml(response.body)["#{call}Response"]
123
+ unless result
124
+ raise Exception.new("No #{call}Response in response", request, response)
125
+ end
126
+ case result['Ack']
127
+ when 'Success'
128
+ return result
129
+ when 'Warning'
130
+ @@logger.warn(result['Errors'].inspect)
131
+ return result
132
+ else
133
+ raise Error.new(result['Errors'], request, response)
134
+ end
135
+ return result
136
+ else
137
+ raise Exception.new("Unexpected response from server", request, response)
138
+ end
139
+ end
140
+
141
+ # Shorthand for call(call, arguments.merge(:auth_token => this.ebay_token))
142
+ # Allows objects which mix in this module to use their own token.
143
+ def ebay_call(call, arguments = {})
144
+ raise "#{self} has no eBay token" unless ebay_token
145
+ Ebay.call(call, arguments.merge(:auth_token => ebay_token))
146
+ end
147
+
148
+ class Exception < ::Exception
149
+ attr_reader :request, :response
150
+ def initialize(message, request, response)
151
+ super message
152
+ @request, @response = request, response
153
+ end
154
+ end
155
+
156
+ class Error < Exception
157
+ attr_reader :request, :response, :errors
158
+ def initialize(errors, request, response)
159
+ @errors, @request, @response = errors, request, response
160
+ super
161
+ end
162
+
163
+ def to_s
164
+ [@errors].flatten.map { |e| "<#{e['LongMessage']}>" }.join(", ")
165
+ end
166
+ end
167
+ end
@@ -0,0 +1,10 @@
1
+ require 'test/unit'
2
+ require 'ebayr'
3
+
4
+ class TestEbayr < Test::Unit::TestCase
5
+ # If this passes without an exception, then we're ok.
6
+ def test_sanity
7
+ result = Ebayr.call(:GeteBayOfficialTime)
8
+ puts "The eBay time is: #{result['Timestamp']}"
9
+ end
10
+ end
metadata ADDED
@@ -0,0 +1,85 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ebayr
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - JJ Buckley
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-03-22 00:00:00.000000000Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: activesupport
16
+ requirement: &81216930 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *81216930
25
+ - !ruby/object:Gem::Dependency
26
+ name: rake
27
+ requirement: &81216720 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :development
34
+ prerelease: false
35
+ version_requirements: *81216720
36
+ description: A tidy library for using the eBay Trading API with Ruby
37
+ email:
38
+ - jj@bjjb.org
39
+ executables: []
40
+ extensions: []
41
+ extra_rdoc_files: []
42
+ files:
43
+ - .gitignore
44
+ - Gemfile
45
+ - LICENSE
46
+ - README.md
47
+ - Rakefile
48
+ - ebayr.gemspec
49
+ - lib/ebayr.rb
50
+ - lib/ebayr/version.rb
51
+ - test/test_ebayr.rb
52
+ homepage: http://jjbuckley.github.com/ebayr
53
+ licenses: []
54
+ post_install_message:
55
+ rdoc_options: []
56
+ require_paths:
57
+ - lib
58
+ required_ruby_version: !ruby/object:Gem::Requirement
59
+ none: false
60
+ requirements:
61
+ - - ! '>='
62
+ - !ruby/object:Gem::Version
63
+ version: '0'
64
+ segments:
65
+ - 0
66
+ hash: 248100125
67
+ required_rubygems_version: !ruby/object:Gem::Requirement
68
+ none: false
69
+ requirements:
70
+ - - ! '>='
71
+ - !ruby/object:Gem::Version
72
+ version: '0'
73
+ segments:
74
+ - 0
75
+ hash: 248100125
76
+ requirements: []
77
+ rubyforge_project:
78
+ rubygems_version: 1.8.10
79
+ signing_key:
80
+ specification_version: 3
81
+ summary: eBayR is a gem that makes it (relatively) easy to use the eBay Trading API
82
+ from Ruby. Includes a self-contained XML parser, a flexible callback system, and
83
+ easy integration into Rails.
84
+ test_files:
85
+ - test/test_ebayr.rb