Skip to content

Commit

Permalink
Implement project detail fetch end-point
Browse files Browse the repository at this point in the history
  • Loading branch information
İsmail Akbudak committed Mar 1, 2018
1 parent eb039a1 commit 4651bac
Show file tree
Hide file tree
Showing 15 changed files with 248 additions and 17 deletions.
2 changes: 2 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ PATH
remote: .
specs:
smartcat_sdk (0.1.0)
multi_json (~> 1.13, >= 1.13.1)

GEM
remote: https://rubygems.org/
Expand Down Expand Up @@ -39,6 +40,7 @@ GEM
httparty (0.15.6)
multi_xml (>= 0.5.2)
ice_nine (0.11.2)
multi_json (1.13.1)
multi_xml (0.6.0)
multipart-post (2.0.0)
octokit (4.8.0)
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# SmartcatSdk
# SmartcatSDK

Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/smartcat_sdk`. To experiment with that code, run `bin/console` for an interactive prompt.

Expand Down Expand Up @@ -40,4 +40,4 @@ The gem is available as open source under the terms of the [MIT License](https:/

## Code of Conduct

Everyone interacting in the SmartcatSdk project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/smartcat_sdk/blob/master/CODE_OF_CONDUCT.md).
Everyone interacting in the SmartcatSDK project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/smartcat_sdk/blob/master/CODE_OF_CONDUCT.md).
Empty file added conf/cert.pem
Empty file.
10 changes: 10 additions & 0 deletions examples/example.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
require 'smartcat_sdk'

ACCOUNT_ID = 'ACCOUNT_ID'.freeze
API_KEY = 'API_KEY'.freeze

# Project API end-point
# -------------------------------------------------------------
project = SmartcatSDK::REST::Project.new(ACCOUNT_ID, API_KEY)
# Get project details
puts project.get('EXAMPLE_PROJECT_ID')
15 changes: 13 additions & 2 deletions lib/smartcat_sdk.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
require 'smartcat_sdk/version'
require 'net/http'
require 'net/https'
require 'net/http/post/multipart'

module SmartcatSdk
# JSON parser
require 'multi_json'

# Smartcat files
require 'smartcat_sdk/version' unless defined?(SmartcatSDK::VERSION)
require 'smartcat_sdk/util/client_config'
require 'smartcat_sdk/rest/errors'
require 'smartcat_sdk/rest/project'

module SmartcatSDK
# Your code goes here...
end
105 changes: 105 additions & 0 deletions lib/smartcat_sdk/rest/base_request.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
module SmartcatSDK
module REST
class BaseRequest
RUBY_INFO = "(#{RUBY_ENGINE}/#{RUBY_PLATFORM} #{RUBY_VERSION}-p#{RUBY_PATCHLEVEL})".freeze
HTTP_HEADERS = {
'Accept' => 'application/json',
'Accept-Charset' => 'utf-8',
'User-Agent' => "smartcat_sdk/#{SmartcatSDK::VERSION} #{RUBY_INFO}"
}.freeze

def initialize(*args)
options = args.last.is_a?(Hash) ? args.pop : {}
@config = SmartcatSDK::Util::ClientConfig.new options
@user = args[0] || nil
@password = args[1] || nil
raise ArgumentError, 'Account ID is required' if @user.nil?
raise ArgumentError, 'API key is required' if @password.nil?
set_up_connection
end

protected

##
# Prepare http request
# rubocop:disable Metrics/AbcSize
# rubocop:disable Metrics/MethodLength
def prepare_request(method, path, params: {})
request_path = @config.host
request_path += "/api/integration/v1/#{path}"
uri = URI.parse(request_path)
uri.query = URI.encode_www_form(params) if %w[get delete].include?(method)
method_class = Net::HTTP.const_get method.to_s.capitalize
request = method_class.new(uri.to_s, HTTP_HEADERS)
request.form_data = params if %w[post put].include?(method)
request.basic_auth(@user, @password)
connect_and_send(request)
end

private

##
# Set up and cache a Net::HTTP object to use when making requests.
def set_up_connection
uri = URI.parse(@config.host)
@http = Net::HTTP.new(
uri.host,
uri.port,
@config.proxy_addr,
@config.proxy_port,
@config.proxy_user,
@config.proxy_pass
)
@http.verify_mode = OpenSSL::SSL::VERIFY_NONE
@http.use_ssl = @config.use_ssl
if @config.ssl_verify_peer
@http.verify_mode = OpenSSL::SSL::VERIFY_PEER
@http.ca_file = @config.ssl_ca_file
else
@http.verify_mode = OpenSSL::SSL::VERIFY_NONE
end
@http.open_timeout = @config.timeout
@http.read_timeout = @config.timeout
end

##
# Send an HTTP request using the cached <tt>@http</tt> object and
# return the JSON response body parsed into a hash. Also save the raw
# Net::HTTP::Request and Net::HTTP::Response objects as
# <tt>@last_request</tt> and <tt>@last_response</tt> to allow for
# inspection later.
def connect_and_send(request, is_file = false)
@last_request = request
retries_left = @config.retry_limit
begin
response = @http.request request
@last_response = response
raise SmartcatSDK::REST::ServerError if response.is_a?(Net::HTTPServerError)
rescue StandardError => _error
raise if request.class == Net::HTTP::Post
raise if retries_left <= 0
retries_left -= 1
retry
end
handle_response_errors(handle_response(is_file, response), response)
end

def handle_response_errors(object, response)
return object unless response.is_a?(Net::HTTPClientError)
raise SmartcatSDK::REST::RequestError.new(object['error'], object['code'])
end

def handle_response(is_file, response)
if response.body && !response.body.empty?
return response.body if is_file
MultiJson.load(response.body)
elsif response.is_a?(Net::HTTPBadRequest)
{
message: 'Bad request',
code: 400
}
end
end
end
end
end
24 changes: 24 additions & 0 deletions lib/smartcat_sdk/rest/errors.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
module SmartcatSDK
module REST
class ServerError < StandardError; end

class RequestError < StandardError
attr_reader :code

def initialize(message, code=nil)
super message
@code = code
end
end

class SDKError < StandardError
attr_reader :code

def initialize(message, code=nil)
super message
@code = code
end
end

end
end
17 changes: 17 additions & 0 deletions lib/smartcat_sdk/rest/project.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
require 'smartcat_sdk/rest/base_request'

module SmartcatSDK
module REST
class Project < BaseRequest

def initialize(*args)
super(*args)
@resource = 'project'
end

def get(project_id)
prepare_request(:get, "#{@resource}/#{project_id}")
end
end
end
end
29 changes: 29 additions & 0 deletions lib/smartcat_sdk/util/client_config.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
module SmartcatSDK
module Util
class ClientConfig
DEFAULTS = {
host: 'https://smartcat.ai',
port: 80,
use_ssl: true,
ssl_verify_peer: false,
ssl_ca_file: File.dirname(__FILE__) + '/../../../conf/cert.pem',
timeout: 30,
proxy_addr: nil,
proxy_port: nil,
proxy_user: nil,
proxy_pass: nil,
retry_limit: 2
}.freeze

DEFAULTS.each_key do |attribute|
attr_accessor attribute
end

def initialize(opts={})
DEFAULTS.each do |attribute, value|
send("#{attribute}=".to_sym, opts.fetch(attribute, value))
end
end
end
end
end
2 changes: 1 addition & 1 deletion lib/smartcat_sdk/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module SmartcatSdk
module SmartcatSDK
VERSION = '0.1.0'.freeze
end
5 changes: 3 additions & 2 deletions smartcat_sdk.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ require 'date'

Gem::Specification.new do |spec|
spec.name = 'smartcat_sdk'
spec.version = SmartcatSdk::VERSION
spec.version = SmartcatSDK::VERSION
spec.authors = ['İsmail Akbudak']
spec.email = ['ismail.akbudak@lab2023.com']
spec.summary = 'SmartCat Ruby SDK.'
Expand All @@ -20,7 +20,8 @@ Gem::Specification.new do |spec|
spec.bindir = 'exe'
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
spec.require_paths = ['lib']

spec.add_runtime_dependency 'multi_json', '~> 1.13', '>= 1.13.1'
spec.add_runtime_dependency 'multipart-post', '~> 2.0'
spec.add_development_dependency 'bundler', '~> 1.16'
spec.add_development_dependency 'pronto', '~> 0.9.5'
spec.add_development_dependency 'pronto-fasterer', '~> 0.9.0'
Expand Down
23 changes: 23 additions & 0 deletions spec/features/rest/base_request_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
require 'smartcat_sdk/rest/base_request'

RSpec.describe SmartcatSDK::REST::BaseRequest do
context '#errors' do
it 'valid arguments' do
expect(described_class.new('account_id', 'api_key').is_a?(described_class)).to eq(true)
end
end

context '#errors' do
it 'mission api key error' do
expect do
described_class.new
end.to raise_error(ArgumentError, 'Account ID is required')
end

it 'mission account id error' do
expect do
described_class.new('account_id')
end.to raise_error(ArgumentError, 'API key is required')
end
end
end
5 changes: 5 additions & 0 deletions spec/features/smartcat_sdk_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
RSpec.describe SmartcatSDK do
it 'has a version number' do
expect(SmartcatSDK::VERSION).not_to be nil
end
end
9 changes: 0 additions & 9 deletions spec/smartcat_sdk_spec.rb

This file was deleted.

15 changes: 14 additions & 1 deletion spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
require 'bundler/setup'
require 'smartcat_sdk'

Bundler.require(:default, :test)

dir = Pathname.new(__FILE__).dirname + '../lib/smartcat_sdk'
require dir.expand_path

RSpec.configure do |config|
# Enable flags like --only-failures and --next-failure
Expand All @@ -11,4 +15,13 @@
config.expect_with :rspec do |c|
c.syntax = :expect
end

# Use color in STDOUT
config.color = true

# Use color not only in STDOUT but also in pagers and files
config.tty = true

# Use the specified formatter
config.formatter = :documentation
end

0 comments on commit 4651bac

Please sign in to comment.