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

Use net http instead faraday #39

Merged
merged 4 commits into from
Sep 9, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
42 changes: 19 additions & 23 deletions lib/rakuten_web_service/client.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
require 'uri'
require 'faraday'
require 'faraday_middleware'

require 'rakuten_web_service/response'

Expand All @@ -12,44 +10,42 @@ class SystemError < StandardError; end
class ServiceUnavailable < StandardError; end

class Client
attr_reader :url, :path
attr_reader :url

def initialize(resource_class)
@resource_class = resource_class
url = URI.parse(@resource_class.endpoint)
@url = "#{url.scheme}://#{url.host}"
@path = url.path
@url = URI.parse(@resource_class.endpoint)
end

def get(params)
params = RakutenWebService.configuration.generate_parameters(params)
response = connection.get(path, params)
case response.status
response = request(url.path, params)
body = JSON.parse(response.body)
case response.code.to_i
when 200
return RakutenWebService::Response.new(@resource_class, response.body)
return RakutenWebService::Response.new(@resource_class, body)
when 400
raise WrongParameter, response.body['error_description']
raise WrongParameter, body['error_description']
when 404
raise NotFound, response.body['error_description']
raise NotFound, body['error_description']
when 429
raise TooManyRequests, response.body['error_description']
raise TooManyRequests, body['error_description']
when 500
raise SystemError, response.body['error_description']
raise SystemError, body['error_description']
when 503
raise ServiceUnavailable, response.body['error_description']
raise ServiceUnavailable, body['error_description']
end
end

private
def connection
return @connection if @connection
@connection = Faraday.new(:url => url) do |conn|
conn.request :url_encoded
conn.response :json
conn.response :logger if RakutenWebService.configuration.debug
conn.adapter Faraday.default_adapter
conn.headers['User-Agent'] = "RakutenWebService SDK for Ruby v#{RWS::VERSION}(ruby-#{RUBY_VERSION} [#{RUBY_PLATFORM}])"
end
def request(path, params)
http = Net::HTTP.new(@url.host, @url.port)
http.use_ssl = true
path = "#{path}?#{params.map { |k, v| "#{k}=#{URI.encode(v.to_s)}" }.join('&')}"
header = {
'User-Agent' => "RakutenWebService SDK for Ruby v#{RWS::VERSION}(ruby-#{RUBY_VERSION} [#{RUBY_PLATFORM}])"
}
http.get(path, header)
end
end
end
3 changes: 0 additions & 3 deletions rakuten_web_service.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,6 @@ Gem::Specification.new do |spec|
spec.require_paths = ["lib"]
spec.required_ruby_version = '>= 2.1.0'

spec.add_dependency 'faraday', '~> 0.9.0'
spec.add_dependency 'faraday_middleware'

spec.add_development_dependency "bundler", "~> 1.3"
spec.add_development_dependency "rake"
spec.add_development_dependency "webmock", "~> 1.20.4"
Expand Down
2 changes: 1 addition & 1 deletion spec/rakuten_web_service/client_spec.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
require 'spec_helper'

describe RakutenWebService::Client do
let(:endpoint) { 'http://api.example.com/resources' }
let(:endpoint) { 'https://api.example.com/resources' }
let(:resource_class) do
double('resource_class', endpoint: endpoint)
end
Expand Down
1 change: 1 addition & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
require File.expand_path(File.join(__dir__, '..', 'lib', 'rakuten_web_service'))

require 'webmock/rspec'
require 'tapp'

Dir[File.expand_path(File.join(__dir__, "support/**/*.rb"))].each { |f| require f }

Expand Down