Class: VerticalResponse::API::Client

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/client.rb

Direct Known Subclasses

OAuth, Resource

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(response) ⇒ Client

Returns a new instance of Client.



107
108
109
# File 'lib/client.rb', line 107

def initialize(response)
  self.response = response
end

Instance Attribute Details

#responseObject

Returns the value of attribute response.



105
106
107
# File 'lib/client.rb', line 105

def response
  @response
end

Class Method Details

.add_default_query_param(param, value) ⇒ Object



80
81
82
83
# File 'lib/client.rb', line 80

def add_default_query_param(param, value)
  @@default_query_params ||= {}
  @@default_query_params[param] = value
end

.assign_headers(headers_info = {}) ⇒ Object

Assign the headers required by our partner Mashery



21
22
23
24
# File 'lib/client.rb', line 21

def assign_headers(headers_info = {})
  access_token = headers_info[:access_token]
  add_default_query_param(:access_token, access_token)
end

.base_uri(host_uri = nil) ⇒ Object

Returns the base URI of the VerticalResponse API. It builds the URI based on the values from the API configuration file. ‘host’ must be defined (unless host_uri is specified as an input param) otherwise the method will raise an exception.



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/client.rb', line 35

def base_uri(host_uri = nil)
  uri = host_uri
  unless uri
    unless VerticalResponse::CONFIG[:host]
      raise ConfigurationError, 'Configuration option "host" must be defined.'
    end

    uri = URI::Generic.new(
      VerticalResponse::CONFIG[:protocol] || 'http', # protocol scheme
      nil,                          # user info
      VerticalResponse::CONFIG[:host],               # host
      VerticalResponse::CONFIG[:port],               # port
      nil,                          # registry of naming authorities
      nil,                          # path on server
      nil,                          # opaque part
      nil,                          # query data
      nil                           # fragment (part of URI after '#' sign)
    )
  end

  paths = ['api', 'v1']
  paths << @embed_resource.to_s if @embed_resource
  paths << @embed_resource_id.to_s if @embed_resource_id
  URI.join(uri, File.join(*paths))
end

.build_params(params, query_params = {}) ⇒ Object



61
62
63
64
65
66
# File 'lib/client.rb', line 61

def build_params(params, query_params = {})
  request_params = {}
  request_params[:body] = params if params
  # Add whatever query params we have as well
  request_params.merge(build_query_params(query_params))
end

.build_query_params(params = {}) ⇒ Object



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

def build_query_params(params = {})
  query_params = {}
  # Include the default query params
  params = params.merge(default_query_params)
  query_params[:query] = params if params.any?
  query_params
end

.configObject



16
17
18
# File 'lib/client.rb', line 16

def config
  VerticalResponse::CONFIG
end

.default_query_paramsObject



76
77
78
# File 'lib/client.rb', line 76

def default_query_params
  @@default_query_params ||= {}
end

.embed_resource(resource, resource_id = nil) ⇒ Object



26
27
28
29
# File 'lib/client.rb', line 26

def embed_resource(resource, resource_id = nil)
  @embed_resource = resource
  @embed_resource_id = resource_id if resource_id
end

.resource_uri(*additional_paths) ⇒ Object

Resource URI for the current class



86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/client.rb', line 86

def resource_uri(*additional_paths)
  uri = base_uri
  if additional_paths.any?
    # Convert all additional paths to string
    additional_paths = additional_paths.map do |path|
      # We need to escape each path in case it contains caracters that
      # are not appropriate to use as part of an URL.
      # Unescape and then escape again in case the path is already escaped
      URI::escape(URI::unescape(path.to_s))
    end
    uri = File.join(uri, *additional_paths)
  end
  uri
end