Skip to content

Commit

Permalink
Started hooking up the Root module
Browse files Browse the repository at this point in the history
  • Loading branch information
akilburge committed May 1, 2015
1 parent 431229a commit df07681
Show file tree
Hide file tree
Showing 10 changed files with 123 additions and 47 deletions.
3 changes: 2 additions & 1 deletion lib/gogokit/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
require 'gogokit/connection'
require 'gogokit/error'
require 'gogokit/client/oauth'
require 'gogokit/client/root'
require 'gogokit/version'

module GogoKit
Expand All @@ -13,7 +14,7 @@ module GogoKit
class Client
include GogoKit::Configuration
include GogoKit::Connection
include GogoKit::OAuth
include GogoKit::Client::OAuth

attr_accessor :client_id,
:client_secret,
Expand Down
82 changes: 42 additions & 40 deletions lib/gogokit/client/oauth.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,51 +3,53 @@
require 'gogokit/utils'

module GogoKit
# OAuth authentication methods for {GogoKit::Client}
module OAuth
include GogoKit::Utils
class Client
# OAuth authentication methods for {GogoKit::Client}
module OAuth
include GogoKit::Utils

# Get an OAuth access token
#
# @see http://viagogo.github.io/developer.viagogo.net/#authentication
# @param [String] grant_type The grant type to use to get the token
# @param [Hash] options Token request information
# @return [GogoKit::OAuthToken] The OAuth token
def get_access_token(grant_type, options = {})
object_from_response(GogoKit::OAuthToken,
GogoKit::OAuthTokenRepresenter,
:post,
oauth_token_endpoint,
body: token_request_body(grant_type, options),
headers: token_request_headers)
end
# Get an OAuth access token
#
# @see http://viagogo.github.io/developer.viagogo.net/#authentication
# @param [String] grant_type The grant type to use to get the token
# @param [Hash] options Token request information
# @return [GogoKit::OAuthToken] The OAuth token
def get_access_token(grant_type, options = {})
object_from_response(GogoKit::OAuthToken,
GogoKit::OAuthTokenRepresenter,
:post,
oauth_token_endpoint,
body: token_request_body(grant_type, options),
headers: token_request_headers)
end

# Get an OAuth access token for an application.
#
# @see
# http://viagogo.github.io/developer.viagogo.net/#client-credentials-grant
# @param [Hash] options Token request information
# @return [GogoKit::OAuthToken] The OAuth token
def get_client_access_token(options = {})
get_access_token('client_credentials', options)
end
# Get an OAuth access token for an application.
#
# @see
# http://viagogo.github.io/developer.viagogo.net/#client-credentials-grant
# @param [Hash] options Token request information
# @return [GogoKit::OAuthToken] The OAuth token
def get_client_access_token(options = {})
get_access_token('client_credentials', options)
end

private
private

def token_request_body(grant_type, options)
body = options || {}
body[:grant_type] = grant_type
body
end
def token_request_body(grant_type, options)
body = options || {}
body[:grant_type] = grant_type
body
end

def token_request_headers
credentials = "#{client_id}:#{client_secret}"
basic_header_value = Base64.encode64(credentials).gsub("\n", '')
{
content_type: 'application/x-www-form-urlencoded',
accept: 'application/json',
authorization: "Basic #{basic_header_value}"
}
def token_request_headers
credentials = "#{client_id}:#{client_secret}"
basic_header_value = Base64.encode64(credentials).gsub("\n", '')
{
content_type: 'application/x-www-form-urlencoded',
accept: 'application/json',
authorization: "Basic #{basic_header_value}"
}
end
end
end
end
19 changes: 19 additions & 0 deletions lib/gogokit/client/root.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
require 'gogokit/utils'

module GogoKit
class Client
# {GogoKit::Client} methods for getting the root resource
module Root
include GogoKit::Utils

# Gets the root of the viagogo API service.
#
# @see http://viagogo.github.io/developer.viagogo.net/#root-endpoint
# @param [Hash] options Optional options
# @return [GogoKit::Resource::Root] The root resource
def get_root
nil
end
end
end
end
24 changes: 19 additions & 5 deletions lib/gogokit/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,33 @@ module GogoKit
module Configuration
attr_writer :oauth_token_endpoint

# Gets the endpoint for obtaining OAuth access tokens
# The endpoint for the API root resource
def api_root_endpoint
nil
end

# The endpoint for obtaining OAuth access tokens
def oauth_token_endpoint
@oauth_token_endpoint || GogoKit::Default::OAUTH_TOKEN_ENDPOINT
end

private

def endpoints
{
# api_root_endpoint: api_root_endpoint,
oauth_token_endpoint: oauth_token_endpoint
}
end

def validate_configuration!
return if oauth_token_endpoint =~ /\A#{URI.regexp}\z/
endpoints.each do |endpoint, value|
next if !value.nil? && value =~ /\A#{URI.regexp}\z/

fail(ConfigurationError,
'Invalid :oauth_token_endpoint specified: ' \
"#{oauth_token_endpoint.inspect} must be a valid URL")
fail(ConfigurationError,
"Invalid #{endpoint} specified: " \
"#{value.inspect} must be a valid URL")
end
end
end
end
3 changes: 3 additions & 0 deletions lib/gogokit/default.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
module GogoKit
# Default configuration options for {GogoKit::Client}
module Default
# Default API root endpoint
API_ROOT_ENDPOINT = 'https://api.viagogo.net/v2'.freeze

# Default OAuth token endpoint
OAUTH_TOKEN_ENDPOINT = 'https://www.viagogo.com/secure/oauth2/token'.freeze
end
Expand Down
19 changes: 19 additions & 0 deletions lib/gogokit/resource/root.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
require 'ostruct'
require 'roar/decorator'
require 'roar/json/hal'

module GogoKit
# Resources returned in API responses
module Resource
# The root of the viagogo API service.
#
# @see http://viagogo.github.io/developer.viagogo.net/#root
class Root < OpenStruct
end

# A {Representable::Decorator} for {GogoKit::Root}
class RootRepresenter < Roar::Decorator
include Roar::JSON::HAL
end
end
end
1 change: 1 addition & 0 deletions spec/fixtures/root.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"_links":{"self":{"href":"https://api.viagogo.net/v2/","title":null,"templated":false},"viagogo:user":{"href":"https://api.viagogo.net/v2/user","title":"Account Settings","templated":false},"viagogo:search":{"href":"https://api.viagogo.net/v2/search","title":"Search","templated":false},"viagogo:genres":{"href":"https://api.viagogo.net/v2/categories/0/children","title":"Browse","templated":false},"viagogo:countries":{"href":"https://api.viagogo.net/v2/countries","title":"Country","templated":false},"viagogo:currencies":{"href":"https://api.viagogo.net/v2/currencies","title":"Currency","templated":false},"viagogo:languages":{"href":"https://api.viagogo.net/v2/venues","title":null,"templated":false},"viagogo:metroareas":{"href":"https://api.viagogo.net/v2/metroareas","title":null,"templated":false},"viagogo:venues":{"href":"https://api.viagogo.net/v2/venues","title":"Venue","templated":false}}}
2 changes: 1 addition & 1 deletion spec/gogokit/client/oauth_spec.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
require 'rspec'
require 'spec_helper'

describe 'GogoKit::OAuth' do
describe 'GogoKit::Client::OAuth' do
let(:client) do
GogoKit::Client.new(client_id: 'CK', client_secret: 'CS')
end
Expand Down
7 changes: 7 additions & 0 deletions spec/gogokit/client/root_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
require 'rspec'
require 'spec_helper'

describe GogoKit::Client::Root do
describe '#get_root' do
end
end
10 changes: 10 additions & 0 deletions spec/gogokit/configuration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,16 @@
client_secret: 'CS')
end

describe '#api_root_endpoint' do
context 'when no value is given' do
it 'return default API root endpoint' do
client = GogoKit::Client.new
expect(client.api_root_endpoint)
.to eq(GogoKit::Default::OAUTH_TOKEN_ENDPOINT)
end
end
end

describe '#oauth_token_endpoint' do
context 'when no value is given' do
it 'return default token endpoint' do
Expand Down

0 comments on commit df07681

Please sign in to comment.