gogokit 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (43) hide show
  1. checksums.yaml +15 -0
  2. data/LICENSE.txt +19 -0
  3. data/README.md +18 -0
  4. data/Rakefile +12 -0
  5. data/gogokit.gemspec +27 -0
  6. data/lib/gogokit.rb +6 -0
  7. data/lib/gogokit/client.rb +171 -0
  8. data/lib/gogokit/client/category.rb +39 -0
  9. data/lib/gogokit/client/country.rb +38 -0
  10. data/lib/gogokit/client/currency.rb +38 -0
  11. data/lib/gogokit/client/event.rb +41 -0
  12. data/lib/gogokit/client/language.rb +38 -0
  13. data/lib/gogokit/client/listing.rb +41 -0
  14. data/lib/gogokit/client/metro_area.rb +39 -0
  15. data/lib/gogokit/client/oauth.rb +55 -0
  16. data/lib/gogokit/client/root.rb +24 -0
  17. data/lib/gogokit/client/search.rb +27 -0
  18. data/lib/gogokit/client/venue.rb +39 -0
  19. data/lib/gogokit/configuration.rb +40 -0
  20. data/lib/gogokit/connection.rb +34 -0
  21. data/lib/gogokit/default.rb +10 -0
  22. data/lib/gogokit/error.rb +12 -0
  23. data/lib/gogokit/middleware/raise_error.rb +27 -0
  24. data/lib/gogokit/money.rb +19 -0
  25. data/lib/gogokit/oauth_token.rb +21 -0
  26. data/lib/gogokit/paged_resource.rb +23 -0
  27. data/lib/gogokit/resource.rb +18 -0
  28. data/lib/gogokit/resource/category.rb +43 -0
  29. data/lib/gogokit/resource/country.rb +27 -0
  30. data/lib/gogokit/resource/currency.rb +27 -0
  31. data/lib/gogokit/resource/event.rb +54 -0
  32. data/lib/gogokit/resource/language.rb +27 -0
  33. data/lib/gogokit/resource/listing.rb +89 -0
  34. data/lib/gogokit/resource/listing_note.rb +17 -0
  35. data/lib/gogokit/resource/metro_area.rb +29 -0
  36. data/lib/gogokit/resource/root.rb +15 -0
  37. data/lib/gogokit/resource/search_result.rb +52 -0
  38. data/lib/gogokit/resource/ticket_type.rb +17 -0
  39. data/lib/gogokit/resource/venue.rb +49 -0
  40. data/lib/gogokit/seating.rb +19 -0
  41. data/lib/gogokit/utils.rb +18 -0
  42. data/lib/gogokit/version.rb +17 -0
  43. metadata +155 -0
@@ -0,0 +1,41 @@
1
+ require 'gogokit/utils'
2
+ require 'gogokit/resource/listing'
3
+
4
+ module GogoKit
5
+ class Client
6
+ # {GogoKit::Client} methods for getting listings
7
+ module Listing
8
+ include GogoKit::Utils
9
+
10
+ # Retrieves a listing by ID
11
+ #
12
+ # @param [Integer] listing_id The ID of the listing to be retrieved
13
+ # @param [Hash] options Optional options
14
+ # @return [GogoKit::Listing] The requested listing
15
+ def get_listing(listing_id, options = {})
16
+ root = get_root
17
+ object_from_response(GogoKit::Listing,
18
+ GogoKit::ListingRepresenter,
19
+ :get,
20
+ "#{root.links['self'].href}/listings/" \
21
+ "#{listing_id}",
22
+ options)
23
+ end
24
+
25
+ # Retrieves all listings in a particular event
26
+ #
27
+ # @see http://viagogo.github.io/developer.viagogo.net/#eventlistings
28
+ # @param [Hash] options Optional options
29
+ # @return [GogoKit::PagedResource] All listings in the specified event
30
+ def get_listings_by_event(event_id, options = {})
31
+ root = get_root
32
+ object_from_response(GogoKit::PagedResource,
33
+ GogoKit::ListingsRepresenter,
34
+ :get,
35
+ "#{root.links['self'].href}/events/" \
36
+ "#{event_id}/listings",
37
+ options)
38
+ end
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,39 @@
1
+ require 'gogokit/utils'
2
+ require 'gogokit/resource/metro_area'
3
+
4
+ module GogoKit
5
+ class Client
6
+ # {GogoKit::Client} methods for getting metro areas
7
+ module MetroArea
8
+ include GogoKit::Utils
9
+
10
+ # Retrieves a metro area by ID
11
+ #
12
+ # @param [Integer] metro_area_id The ID of the metro area to be retrieved
13
+ # @param [Hash] options Optional options
14
+ # @return [GogoKit::MetroArea] The requested metro area
15
+ def get_metro_area(metro_area_id, options = {})
16
+ root = get_root
17
+ object_from_response(GogoKit::MetroArea,
18
+ GogoKit::MetroAreaRepresenter,
19
+ :get,
20
+ "#{root.links['self'].href}/metro_areas/" \
21
+ "#{metro_area_id}",
22
+ options)
23
+ end
24
+
25
+ # Retrieves all metro_areas
26
+ #
27
+ # @see http://viagogo.github.io/developer.viagogo.net/#viagogometro_areas
28
+ # @param [Hash] options Optional options
29
+ # @return [GogoKit::PagedResource] All metro_areas
30
+ def get_metro_areas(options = {})
31
+ object_from_response(GogoKit::PagedResource,
32
+ GogoKit::MetroAreasRepresenter,
33
+ :get,
34
+ get_root.links['viagogo:metro_areas'].href,
35
+ options)
36
+ end
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,55 @@
1
+ require 'base64'
2
+ require 'gogokit/oauth_token'
3
+ require 'gogokit/utils'
4
+
5
+ module GogoKit
6
+ class Client
7
+ # OAuth authentication methods for {GogoKit::Client}
8
+ module OAuth
9
+ include GogoKit::Utils
10
+
11
+ # Get an OAuth access token
12
+ #
13
+ # @see http://viagogo.github.io/developer.viagogo.net/#authentication
14
+ # @param [String] grant_type The grant type to use to get the token
15
+ # @param [Hash] options Token request information
16
+ # @return [GogoKit::OAuthToken] The OAuth token
17
+ def get_access_token(grant_type, options = {})
18
+ object_from_response(GogoKit::OAuthToken,
19
+ GogoKit::OAuthTokenRepresenter,
20
+ :post,
21
+ oauth_token_endpoint,
22
+ body: token_request_body(grant_type, options),
23
+ headers: token_request_headers)
24
+ end
25
+
26
+ # Get an OAuth access token for an application.
27
+ #
28
+ # @see
29
+ # http://viagogo.github.io/developer.viagogo.net/#client-credentials-grant
30
+ # @param [Hash] options Token request information
31
+ # @return [GogoKit::OAuthToken] The OAuth token
32
+ def get_client_access_token(options = {})
33
+ get_access_token('client_credentials', options)
34
+ end
35
+
36
+ private
37
+
38
+ def token_request_body(grant_type, options)
39
+ body = options || {}
40
+ body[:grant_type] = grant_type
41
+ body
42
+ end
43
+
44
+ def token_request_headers
45
+ credentials = "#{client_id}:#{client_secret}"
46
+ basic_header_value = Base64.encode64(credentials).gsub("\n", '')
47
+ {
48
+ content_type: 'application/x-www-form-urlencoded',
49
+ accept: 'application/json',
50
+ authorization: "Basic #{basic_header_value}"
51
+ }
52
+ end
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,24 @@
1
+ require 'gogokit/utils'
2
+ require 'gogokit/resource/root'
3
+
4
+ module GogoKit
5
+ class Client
6
+ # {GogoKit::Client} methods for getting the root resource
7
+ module Root
8
+ include GogoKit::Utils
9
+
10
+ # Gets the root of the viagogo API service.
11
+ #
12
+ # @see http://viagogo.github.io/developer.viagogo.net/#root-endpoint
13
+ # @param [Hash] options Optional options
14
+ # @return [GogoKit::Resource::Root] The root resource
15
+ def get_root(options = {})
16
+ object_from_response(GogoKit::Root,
17
+ GogoKit::RootRepresenter,
18
+ :get,
19
+ api_root_endpoint,
20
+ options)
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,27 @@
1
+ require 'gogokit/utils'
2
+ require 'gogokit/resource/search_result'
3
+
4
+ module GogoKit
5
+ class Client
6
+ # {GogoKit::Client} methods for searching for viagogo entities
7
+ module Search
8
+ include GogoKit::Utils
9
+
10
+ # Search for entities that match a given query
11
+ #
12
+ # @see http://viagogo.github.io/developer.viagogo.net/#viagogosearch
13
+ # @param [String] query The query text to be used to match entities
14
+ # @param [Hash] options Optional options
15
+ # @return [GogoKit::PagedResource] The results of the query
16
+ def search(query, options = {})
17
+ options[:params] ||= {}
18
+ options[:params] = options[:params].merge(query: query)
19
+ object_from_response(GogoKit::PagedResource,
20
+ GogoKit::SearchResultsRepresenter,
21
+ :get,
22
+ get_root.links['viagogo:search'].href,
23
+ options)
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,39 @@
1
+ require 'gogokit/utils'
2
+ require 'gogokit/resource/venue'
3
+
4
+ module GogoKit
5
+ class Client
6
+ # {GogoKit::Client} methods for getting venues
7
+ module Venue
8
+ include GogoKit::Utils
9
+
10
+ # Retrieves a venue by ID
11
+ #
12
+ # @param [Integer] venue_id The ID of the venue to be retrieved
13
+ # @param [Hash] options Optional options
14
+ # @return [GogoKit::Venue] The requested venue
15
+ def get_venue(venue_id, options = {})
16
+ root = get_root
17
+ object_from_response(GogoKit::Venue,
18
+ GogoKit::VenueRepresenter,
19
+ :get,
20
+ "#{root.links['self'].href}/venues/" \
21
+ "#{venue_id}",
22
+ options)
23
+ end
24
+
25
+ # Retrieves all venues
26
+ #
27
+ # @see http://viagogo.github.io/developer.viagogo.net/#viagogovenues
28
+ # @param [Hash] options Optional options
29
+ # @return [GogoKit::PagedResource] All venues
30
+ def get_venues(options = {})
31
+ object_from_response(GogoKit::PagedResource,
32
+ GogoKit::VenuesRepresenter,
33
+ :get,
34
+ get_root.links['viagogo:venues'].href,
35
+ options)
36
+ end
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,40 @@
1
+ require 'uri'
2
+ require 'gogokit/default'
3
+
4
+ module GogoKit
5
+ # Configuration options for {GogoKit::Client} or falls back to
6
+ # {GogoKit::Default}
7
+ module Configuration
8
+ attr_writer :api_root_endpoint,
9
+ :oauth_token_endpoint
10
+
11
+ # The endpoint for the API root resource
12
+ def api_root_endpoint
13
+ @api_root_endpoint || GogoKit::Default::API_ROOT_ENDPOINT
14
+ end
15
+
16
+ # The endpoint for obtaining OAuth access tokens
17
+ def oauth_token_endpoint
18
+ @oauth_token_endpoint || GogoKit::Default::OAUTH_TOKEN_ENDPOINT
19
+ end
20
+
21
+ private
22
+
23
+ def endpoints
24
+ {
25
+ api_root_endpoint: api_root_endpoint,
26
+ oauth_token_endpoint: oauth_token_endpoint
27
+ }
28
+ end
29
+
30
+ def validate_configuration!
31
+ endpoints.each do |endpoint, value|
32
+ next if !value.nil? && value =~ /\A#{URI.regexp}\z/
33
+
34
+ fail(ConfigurationError,
35
+ "Invalid #{endpoint} specified: " \
36
+ "#{value.inspect} must be a valid URL")
37
+ end
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,34 @@
1
+ require 'faraday'
2
+ require 'gogokit/middleware/raise_error'
3
+
4
+ module GogoKit
5
+ # HTTP Connection methods for {GogoKit::Client}
6
+ module Connection
7
+ private
8
+
9
+ def connection_options # rubocop:disable Metrics/MethodLength
10
+ @connection_options ||= {
11
+ headers: {
12
+ accept: 'application/hal+json',
13
+ content_type: 'application/hal+json',
14
+ user_agent: user_agent
15
+ },
16
+ request: {
17
+ open_timeout: 5,
18
+ timeout: 10
19
+ }
20
+ }
21
+ end
22
+
23
+ def connection
24
+ Faraday::Connection.new(nil, connection_options) do |builder|
25
+ builder.use Faraday::Request::UrlEncoded
26
+
27
+ # Handle error responses
28
+ builder.use GogoKit::Middleware::RaiseError
29
+
30
+ builder.adapter Faraday.default_adapter
31
+ end
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,10 @@
1
+ module GogoKit
2
+ # Default configuration options for {GogoKit::Client}
3
+ module Default
4
+ # Default API root endpoint
5
+ API_ROOT_ENDPOINT = 'https://api.viagogo.net/v2'.freeze
6
+
7
+ # Default OAuth token endpoint
8
+ OAUTH_TOKEN_ENDPOINT = 'https://www.viagogo.com/secure/oauth2/token'.freeze
9
+ end
10
+ end
@@ -0,0 +1,12 @@
1
+ module GogoKit
2
+ # Custom error class for rescuing from all GogoKit errors
3
+ class Error < StandardError; end
4
+
5
+ # Raised when {GogoKit::Client} is not configured correctly
6
+ class ConfigurationError < Error; end
7
+
8
+ # Raised for any error returned by the API
9
+ class ApiError < Error
10
+ attr_accessor :response
11
+ end
12
+ end
@@ -0,0 +1,27 @@
1
+ require 'faraday'
2
+ require 'gogokit/error'
3
+
4
+ module GogoKit
5
+ module Middleware
6
+ class RaiseError < Faraday::Middleware
7
+ def initialize(app)
8
+ super app
9
+ end
10
+
11
+ def call(request_env)
12
+ @app.call(request_env).on_complete do |response_env|
13
+ if response_env[:status].to_i >= 400
14
+ api_error = GogoKit::ApiError.new
15
+ api_error.response = response_env
16
+ fail api_error, error_message(response_env)
17
+ end
18
+ end
19
+ end
20
+
21
+ def error_message(response_env)
22
+ "#{response_env[:method].to_s.upcase} #{response_env[:url]}:" \
23
+ " #{response_env[:status]}}"
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,19 @@
1
+ require 'ostruct'
2
+ require 'representable/json'
3
+
4
+ module GogoKit
5
+ # Returned for monetary values, such as ticket prices, fees charged and tax
6
+ # amounts.
7
+ #
8
+ # @see http://viagogo.github.io/developer.viagogo.net/#money
9
+ class Money < OpenStruct
10
+ end
11
+
12
+ module MoneyRepresenter
13
+ include Representable::JSON
14
+
15
+ property :amount
16
+ property :currency
17
+ property :display
18
+ end
19
+ end
@@ -0,0 +1,21 @@
1
+ require 'ostruct'
2
+ require 'representable/json'
3
+
4
+ module GogoKit
5
+ # An OAuth access token that can be used to access the viagogo API
6
+ #
7
+ # @see http://viagogo.github.io/developer.viagogo.net/#authentication
8
+ class OAuthToken < OpenStruct
9
+ end
10
+
11
+ # A representer for {GogoKit::OAuthToken}
12
+ module OAuthTokenRepresenter
13
+ include Representable::JSON
14
+
15
+ property :access_token
16
+ property :token_type
17
+ property :expires_in
18
+ property :scope
19
+ property :refresh_token
20
+ end
21
+ end
@@ -0,0 +1,23 @@
1
+ require 'roar/json'
2
+ require 'roar/json/hal'
3
+ require 'gogokit/resource'
4
+
5
+ module GogoKit
6
+ # Base class for pages of resources returned in API responses
7
+ class PagedResource < Resource
8
+ attr_accessor :page,
9
+ :page_size,
10
+ :total_items,
11
+ :items
12
+ end
13
+
14
+ # Representer for {GogoKit::PagedResource}s
15
+ module PagedResourceRepresenter
16
+ include Representable::JSON
17
+ include GogoKit::ResourceRepresenter
18
+
19
+ property :page
20
+ property :page_size
21
+ property :total_items
22
+ end
23
+ end
@@ -0,0 +1,18 @@
1
+ require 'roar/json'
2
+ require 'roar/json/hal'
3
+
4
+ module GogoKit
5
+ # Base class for resources returned in API responses
6
+ class Resource
7
+ attr_accessor :links
8
+ end
9
+
10
+ # A base representer module for every {GogoKit::Resource}
11
+ module ResourceRepresenter
12
+ include Roar::Hypermedia
13
+ include Roar::JSON
14
+ include Roar::JSON::HAL
15
+
16
+ link(:self) { 'http://self' }
17
+ end
18
+ end