Class: EasyqaApi::Item

Inherits:
Object
  • Object
show all
Extended by:
ClassMethodsSettable
Defined in:
lib/easyqa_api/item.rb

Overview

The class for representation EasyQA objects

Constant Summary collapse

CONNECTION =

constant for Faradat connection by content type

{
  json: {
    instance: -> { json_connection },
    content_type: 'application/json'
  },
  multipart: {
    instance: -> { multipart_connection },
    content_type: 'multipart/form-data'
  }
}.freeze
@@default_user =

Deafult user in EasyqaApi Items.

If you set this attributes you can use any methods without set user in all methods

See Also:

nil

Constants included from ClassMethodsSettable

ClassMethodsSettable::METHODS

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ClassMethodsSettable

install_class_methods!

Constructor Details

#initialize(*_args) ⇒ Item

Returns a new instance of Item.



127
128
# File 'lib/easyqa_api/item.rb', line 127

def initialize(*_args)
end

Class Method Details

.json_connectionFaraday::Connection

retrieve or create Faraday json connection

Returns:

  • (Faraday::Connection)

    Faraday json connection



68
69
70
71
72
73
74
# File 'lib/easyqa_api/item.rb', line 68

def json_connection
  @json_connection ||= Faraday.new(url: EasyqaApi.configuration.url) do |faraday|
    faraday.request :json
    faraday.response :json
    faraday.adapter Faraday.default_adapter
  end
end

.multipart_connectionFaraday::Connection

retrieve or create Faraday multipart connection

Returns:

  • (Faraday::Connection)

    Faraday multipart connection



78
79
80
81
82
83
84
# File 'lib/easyqa_api/item.rb', line 78

def multipart_connection
  @multipart_connection ||= Faraday.new(url: EasyqaApi.configuration.url) do |faraday|
    faraday.request :multipart
    faraday.response :json
    faraday.adapter Faraday.default_adapter
  end
end

.operation_status(response) ⇒ Hash

Processing response

Parameters:

  • response (Faraday::Response)

    response of your request

Returns:

  • (Hash)

    response body

Raises:

  • (NotFoundError, PermissionError, RequestError, ValidationError)

    if response not valid

See Also:



121
122
123
124
# File 'lib/easyqa_api/item.rb', line 121

def operation_status(response)
  EasyqaApi::Exception.check_response_status!(response)
  response.body
end

.send_request(url, html_method, type = :json) { ... } ⇒ Hash

Send request to EasyQA api within default configuration

Examples:

send_request('projects', :post) do |req|
  req.body = {
    project: {
      title: 'Test Project'
    },
    organization_id: 1,
    auth_token: 'IcdHzYZXDlX8SsjoOC5MV59lPVPzbaEUuUgZly3ESmopojMaN5pNlzOJCAV2_Rfe'
  }
end

Parameters:

  • url (String)

    url for method

  • html_method (Symbol)

    html method for request

  • type (Symbol) (defaults to: :json)

    type of request

Yields:

  • faraday request config

Returns:

  • (Hash)

    response body

Raises:

  • (NotFoundError, PermissionError, RequestError, ValidationError)

    if request fail

See Also:



106
107
108
109
110
111
112
113
114
# File 'lib/easyqa_api/item.rb', line 106

def send_request(url, html_method, type = :json, &block)
  response = EasyqaApi::Item::CONNECTION[type][:instance].call.send(html_method) do |req|
    req.url EasyqaApi.configuration.api_path + url
    req.headers['Content-Type'] = EasyqaApi::Item::CONNECTION[type][:content_type]
    yield(req) if block
  end

  operation_status(response)
end

Instance Method Details

#install_variables!(arguments) ⇒ Object

Install variables for Item instance if key in attribute has a valid setter in instance

Parameters:

  • arguments (Hash)


132
133
134
135
136
137
138
# File 'lib/easyqa_api/item.rb', line 132

def install_variables!(arguments)
  allowed_methods = retrive_allowed_methods
  arguments.each do |key, value|
    method_name = "#{key}=".to_sym
    send(method_name, value) if allowed_methods.include? method_name
  end
end