Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move to Faraday #24

Closed
wants to merge 34 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
c17bdba
Script to use custom api keys without having to commit them
trobrock Jul 11, 2012
6c21440
Start of an Etsy::Client class beginning to implement Etsy module lev…
trobrock Jul 11, 2012
63bfe73
Changing Rakefile output a little
trobrock Jul 11, 2012
cbdf30b
Make specs run in default task also
trobrock Jul 14, 2012
c256775
Adding SimpleCov coverage
trobrock Jul 14, 2012
3b9ce64
Merge branch 'master' of github.com:kytrinyx/etsy into faraday
trobrock Jul 14, 2012
fb21554
Fix failure from jruby, need openssl gem
trobrock Jul 14, 2012
ae71d9a
Match requests on parsed query strings rather than URI, which can be …
trobrock Jul 14, 2012
58a89e5
Turn of simplecov in travis to avoid issues with 1.9 mode on rbx
trobrock Jul 14, 2012
fef0edd
Use VCR's builtin filtering for keys
trobrock Jul 14, 2012
49c9bbf
Remove credentials file from ignore
trobrock Jul 14, 2012
e2d71aa
Merge branch 'master' of github.com:kytrinyx/etsy into faraday
trobrock Jul 17, 2012
31d128b
Pattern for using ENV variables for keys
trobrock Jul 18, 2012
32e46ad
Support basic oauth flow, still want to make testing this oauth stuff…
trobrock Jul 19, 2012
44b85a1
Removing a puts
trobrock Jul 19, 2012
54be011
Add sandbox endpoint
kytrinyx Jul 19, 2012
362646d
Add rake task to help set up specs
kytrinyx Jul 19, 2012
5be5c43
Tweak sandbox endpoints
kytrinyx Jul 19, 2012
2aa220f
Implement basic query object.
kytrinyx Jul 26, 2012
5b36d1c
Deliver params and endpoint separately in query.
kytrinyx Jul 29, 2012
bb7512c
Wrap hashes in curly braces
kytrinyx Jul 29, 2012
93c0702
Remove to_s override on query
kytrinyx Jul 29, 2012
7c27be5
Add rudimentary support for associations in query.
kytrinyx Jul 29, 2012
07b71c0
Add support for arbitrary query parameters.
kytrinyx Jul 29, 2012
fcf1c17
Tweak syntax for specifying resources in query
kytrinyx Jul 29, 2012
f5f2370
Add basic query syntax for associated resources
kytrinyx Aug 21, 2012
0c5074b
Add spec helper to query and resource specs
kytrinyx Aug 21, 2012
3e20b95
Add nested resources
kytrinyx Aug 22, 2012
25cb865
Merge branch 'master' into faraday
kytrinyx Sep 23, 2012
a947cd4
Downgrade rspec.
kytrinyx Sep 23, 2012
d29fb09
Add link to JRuby issue
kytrinyx Sep 23, 2012
ebcab8a
Add spec for method missing
kytrinyx Sep 23, 2012
5de051f
Merge branch 'master' into faraday
kytrinyx Nov 18, 2013
51d75ea
fixed broken rspec, may cause problems with jruby
irosenb Nov 18, 2013
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ Gemfile.lock
.rvmrc
*.sw*
*.orig
coverage/
53 changes: 52 additions & 1 deletion Rakefile
Original file line number Diff line number Diff line change
@@ -1,12 +1,63 @@
#!/usr/bin/env rake
require 'rubygems'
require 'rake/testtask'
require 'rspec/core/rake_task'
require "bundler/gem_tasks"

task :default => :test
task :default => [:test, :spec]

Rake::TestTask.new do |t|
t.libs << 'test'
t.test_files = FileList["test/**/*_test.rb"]
t.verbose = false
end

desc "Run specs"
RSpec::Core::RakeTask.new do |t|
t.pattern = "./spec/**/*_spec.rb"
end

namespace :spec do
desc "Setup specs"
task :setup do
require 'oauth'
require './lib/etsy/client'
key = ENV["ETSY_API_KEY"]
unless key
puts
puts "No API key found. Run (or add to .bashrc):"
puts " export ETSY_API_KEY=<your api key>"
end

secret = ENV["ETSY_API_SECRET"]
unless secret
puts
puts "For authenticated (oauth) requests, you also need a secret"
puts " export ETSY_API_SECRET=<your api secret>"
end

environment = ENV["ETSY_ENVIRONMENT"]
unless environment
puts
puts "Defaulting to production environment. To override:"
puts " export ETSY_ENVIRONMENT=sandbox"
end

client = Etsy::Client.new(:api_key => key, :api_secret => secret)

req = client.request_token

puts "Go to:"
puts req.params[:login_url]
print "What is the verifier key? "
verifier = STDIN.gets.chomp.strip
token, secret = client.oauth_token(req, verifier)

puts
puts "These are your environment variables: "
puts "export ETSY_OAUTH_TOKEN=#{token}"
puts "export ETSY_OAUTH_SECRET=#{secret}"
puts "export ETSY_OAUTH_VERIFIER=#{verifier}"
puts
end
end
11 changes: 11 additions & 0 deletions etsy.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,20 @@ Gem::Specification.new do |gem|

gem.add_dependency "json", ">= 1.5.0"
gem.add_dependency "oauth", "~> 0.4.0"
gem.add_dependency "faraday", "~> 0.8.0"
gem.add_dependency "faraday_middleware", "~> 0.8.8"
gem.add_dependency "simple_oauth", "~> 0.1.8"
gem.add_dependency "jruby-openssl", "~> 0.7.7" if RUBY_PLATFORM == 'java'

gem.add_development_dependency "rake", "~> 10.0.4"
gem.add_development_dependency "jnunemaker-matchy", "~> 0.4.0"
# rspec version > 2.7.x breaks with jruby
# see http://jira.codehaus.org/browse/JRUBY-6324
# apparently this has been fixed. See http://jira.codehaus.org/browse/JRUBY-6324?focusedCommentId=287912&page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel#comment-287912
gem.add_development_dependency "rspec", "~> 2.12.0"
gem.add_development_dependency "vcr", "~> 2.2.3"
gem.add_development_dependency "webmock", "~> 1.8.7"
gem.add_development_dependency "simplecov", "~> 0.6.4"
gem.add_development_dependency 'shoulda', "~> 3.4.0"
gem.add_development_dependency 'mocha', "~> 0.13.3"
# shoulda-context blows up on ActiveSupport not being defined
Expand Down
4 changes: 4 additions & 0 deletions lib/etsy.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
$:.unshift File.dirname(__FILE__)

require 'net/http'
require 'faraday'
require 'faraday_middleware'
require 'json'
require 'oauth'
require 'uri'

require 'etsy/client'

require 'etsy/request'
require 'etsy/response'

Expand Down
71 changes: 71 additions & 0 deletions lib/etsy/client.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
module Etsy
class Client
attr_accessor :oauth_token, :oauth_secret

def initialize(options={})
@api_key = options[:api_key]
@api_secret = options[:api_secret]
@oauth_token = options[:oauth_token]
@oauth_secret = options[:oauth_secret]
@environment = options[:environment] || :production
end

def myself
get("users/__SELF__").body["results"].first
end

def user(username)
get("users", :keywords => username).body["results"].first
end

def request_token
consumer = OAuth::Consumer.new(@api_key, @api_secret,
:site => endpoint,
:request_token_path => "/v2/oauth/request_token",
:access_token_path => "/v2/oauth/access_token"
)
consumer.get_request_token
end

def oauth_token(req, verifier)
access_token = req.get_access_token :oauth_verifier => verifier
[access_token.token, access_token.secret]
end

def get(path, options={})
connection.get(path, options)
end

private

def connection
@connection ||= Faraday.new(
:url => "#{endpoint}/v2",
:params => default_params
) do |conn|
conn.request :json
conn.request :oauth, {
:token => @oauth_token,
:token_secret => @oauth_secret,
:consumer_key => @api_key,
:consumer_secret => @api_secret
} if @oauth_token

conn.response :json, :content_type => /\bjson$/

conn.adapter Faraday.default_adapter
end
end

def endpoint
@environment == :production ? "http://openapi.etsy.com" : "http://sandbox.openapi.etsy.com"
end

def default_params
params = {}
params[:api_key] = @api_key if @api_key && !@oauth_token

params
end
end
end
70 changes: 70 additions & 0 deletions lib/etsy/query.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
require 'cgi'
require 'etsy/resource'

module Etsy
class Query

attr_reader :resource, :options, :associations
def initialize(*resource)
@resource = resource.join('/')
@options = {}
@associations = []
end

def limit
options.fetch(:limit) { 25 }
end

def offset
options.fetch(:offset) { 0 }
end

def page
options.fetch(:page) { 1 }
end

def include(resource, options = {})
@associations << Resource.new(resource, options) #.to_s.split('_').map(&:capitalize).join
end

def endpoint
"/#{resource}"
end

def query
return {} if options.size == 0 && associations.empty?
params = {}
options.each do |k, v|
params[k] = list(v)
end
unless associations.empty?
params[:includes] = associations.join(',')
end
params
end

private

def method_missing(method, *args, &block)
if assignment?(method)
key = method.to_s[0..-2].to_sym
options[key] = args.length == 1 ? args.first : args
elsif options.has_key?(method)
options[method]
else
super
end
end

def assignment?(method)
# Depending on the version of Ruby,
# s[-1] will return a character or a Fixnum
method.to_s[-1] == '='[0]
end

def list(values)
Array(values).join(',')
end

end
end
47 changes: 47 additions & 0 deletions lib/etsy/resource.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
module Etsy
class Resource

attr_reader :name, :limit, :offset, :fields, :scope, :child
def initialize(name, options = {})
@name = name.to_s.split('_').map(&:capitalize).join
@limit = options[:limit]
@offset = options[:offset]
@fields = options[:fields] || []
@scope = options[:scope]
end

def to_s
"#{name}#{selected_fields}#{scoped}#{range}#{child_resource}"
end

def include(resource)
@child = resource
self
end

private

def range
return unless offset || limit

if limit
":#{offset || 0}:#{limit}"
else
":#{offset}"
end
end

def selected_fields
"(#{fields.join(',')})" unless fields.empty?
end

def scoped
":#{scope}" if scope
end

def child_resource
"/#{child}" if child
end

end
end
10 changes: 10 additions & 0 deletions spec/api_credentials.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module Etsy
module Test
API_KEY = ENV["ETSY_API_KEY"] || "API_KEY"
API_SECRET = ENV["ETSY_API_SECRET"] || "API_SECRET"
OAUTH_TOKEN = ENV["ETSY_OAUTH_TOKEN"] || "OAUTH_TOKEN"
OAUTH_SECRET = ENV["ETSY_OAUTH_SECRET"] || "OAUTH_SECRET"
OAUTH_VERIFIER = ENV["ETSY_OAUTH_VERIFIER"] || "OAUTH_VERIFIER"
ENVIRONMENT = ENV["ETSY_ENVIRONMENT"] || 'production'
end
end
36 changes: 36 additions & 0 deletions spec/cassettes/Etsy_Client/_get.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

40 changes: 40 additions & 0 deletions spec/cassettes/Etsy_Client/_myself.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading