Module: TrailerVote::Api

Defined in:
lib/trailer_vote/api.rb,
lib/trailer_vote/api/issue.rb,
lib/trailer_vote/api/links.rb,
lib/trailer_vote/api/place.rb,
lib/trailer_vote/api/errors.rb,
lib/trailer_vote/api/product.rb,
lib/trailer_vote/api/version.rb,
lib/trailer_vote/api/issue/find.rb,
lib/trailer_vote/api/place/find.rb,
lib/trailer_vote/api/issue/create.rb,
lib/trailer_vote/api/place/create.rb,
lib/trailer_vote/api/place/lookup.rb,
lib/trailer_vote/api/product/find.rb,
lib/trailer_vote/api/vista_config.rb,
lib/trailer_vote/api/configuration.rb,
lib/trailer_vote/api/product/image.rb,
lib/trailer_vote/api/product/place.rb,
lib/trailer_vote/api/product/video.rb,
lib/trailer_vote/api/type_registry.rb,
lib/trailer_vote/api/composable/get.rb,
lib/trailer_vote/api/place/children.rb,
lib/trailer_vote/api/product/create.rb,
lib/trailer_vote/api/product/lookup.rb,
lib/trailer_vote/api/product/update.rb,
lib/trailer_vote/api/push_recipe_ios.rb,
lib/trailer_vote/api/composable/common.rb,
lib/trailer_vote/api/product/image/find.rb,
lib/trailer_vote/api/product/image/urls.rb,
lib/trailer_vote/api/product/place/link.rb,
lib/trailer_vote/api/product/video/find.rb,
lib/trailer_vote/api/product/video/urls.rb,
lib/trailer_vote/api/place/children/urls.rb,
lib/trailer_vote/api/push_recipe_android.rb,
lib/trailer_vote/api/product/image/create.rb,
lib/trailer_vote/api/product/video/create.rb,
lib/trailer_vote/api/fallback_content_types.rb,
lib/trailer_vote/api/fallback_content_types.rb

Defined Under Namespace

Modules: Composable, Headers, HtmlTypeAdapter, JsonTypeAdapter, TypeRegistry Classes: BadRequest, ClientError, Configuration, Conflict, ConnectionError, DecodeError, EncodeError, Error, ErrorsResponse, Forbidden, Gone, Issue, Links, NetworkError, NotFound, Place, PreconditionFailed, Product, PushRecipeAndroid, PushRecipeIos, ServerError, TimeoutError, TooManyRequests, Unauthorized, UnknownMediaType, UnprocessableEntity, VistaConfig

Constant Summary collapse

ERROR_MAPPING =
Hash.new { |_, key| key < 500 ? ClientError : ServerError }.merge(
  400 => BadRequest,
  401 => Unauthorized,
  403 => Forbidden,
  404 => NotFound,
  409 => Conflict,
  410 => Gone,
  412 => PreconditionFailed,
  422 => UnprocessableEntity,
  429 => TooManyRequests
).freeze
VERSION =
'3.1.0'

Class Method Summary collapse

Class Method Details

.configure(url:, key:, secret:) ⇒ Object



11
12
13
14
# File 'lib/trailer_vote/api/configuration.rb', line 11

def configure(url:, key:, secret:)
  client = Api.default_client(key, secret)
  Configuration.new(client: client, url: url)
end

.decode(result) ⇒ Object



19
20
21
22
23
24
25
26
27
28
# File 'lib/trailer_vote/api.rb', line 19

def decode(result)
  begin
    #Oj.load(result, mode: :strict, symbol_keys: true)
    JSON.parse(result, {symbolize_names: true})
  rescue Oj::Error => err
    raise DecodeError.new(media_type: media_type, source: err)
  rescue JSON::JSONError => err
    raise DecodeError.new(media_type: media_type, source: err)
  end
end

.default_client(username, password) ⇒ Object



41
42
43
# File 'lib/trailer_vote/api.rb', line 41

def default_client(username, password)
  default_unauthenticated_client.basic_auth(user: username, pass: password)
end

.default_unauthenticated_clientObject



34
35
36
37
38
39
# File 'lib/trailer_vote/api.rb', line 34

def default_unauthenticated_client
  HTTP_KLAZZ.headers(
    Headers::ACCEPT_ENCODING => 'gzip, deflate; q=0.5, identity; q=0.1',
    Headers::USER_AGENT => format('TrailerVote/httpx.rb (client %<version>s)', version: VERSION)
  )
end

.encode(media_type, data) ⇒ Object



30
31
32
# File 'lib/trailer_vote/api.rb', line 30

def encode(media_type, data)
  Oj.dump(data, mode: :compat)
end

.raise_error(result) ⇒ Object

Raises:

  • ()


108
109
110
# File 'lib/trailer_vote/api/errors.rb', line 108

def raise_error(result)
  raise ERROR_MAPPING[result.status], result
end