Class: VerticalResponse::API::Client

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

Direct Known Subclasses

OAuth, Resource

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(response, access_token = nil) ⇒ Client

Returns a new instance of Client.



147
148
149
150
# File 'lib/verticalresponse/api/client.rb', line 147

def initialize(response, access_token = nil)
  @access_token = access_token
  self.response = response
end

Instance Attribute Details

#access_tokenObject (readonly)

Returns the value of attribute access_token.



11
12
13
# File 'lib/verticalresponse/api/client.rb', line 11

def access_token
  @access_token
end

#responseObject

Returns the value of attribute response.



145
146
147
# File 'lib/verticalresponse/api/client.rb', line 145

def response
  @response
end

Class Method Details

.add_default_query_param(param, value) ⇒ Object



99
100
101
102
# File 'lib/verticalresponse/api/client.rb', line 99

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



23
24
25
26
# File 'lib/verticalresponse/api/client.rb', line 23

def assign_headers(headers_info = {})
  access_token = headers_info[:access_token]
  add_default_query_param(:access_token, access_token) unless access_token.nil?
end

.base_service_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.



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

def base_service_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

.base_uri(host_uri = nil) ⇒ Object



63
64
65
# File 'lib/verticalresponse/api/client.rb', line 63

def base_uri(host_uri = nil)
  @base_uri ||= File.join(base_service_uri(host_uri).to_s, *resource_uri_suffix)
end

.base_uri_with_prefix(*prefix) ⇒ Object

Allow getting contacts from a list, creating contacts inside a list, etc. This works better than embed_resource, since it avoids conflicts between multiple actors. In a multithreaded environment this is obvious, but in a multi-process environment conflicts can still happen, so we want to set the prefix of a uri on instances, not at the class level.



72
73
74
# File 'lib/verticalresponse/api/client.rb', line 72

def base_uri_with_prefix(*prefix)
  File.join(base_service_uri.to_s, *prefix, *resource_uri_suffix)
end

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



80
81
82
83
84
85
# File 'lib/verticalresponse/api/client.rb', line 80

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



87
88
89
90
91
92
93
# File 'lib/verticalresponse/api/client.rb', line 87

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



18
19
20
# File 'lib/verticalresponse/api/client.rb', line 18

def config
  VerticalResponse::CONFIG
end

.default_query_paramsObject



95
96
97
# File 'lib/verticalresponse/api/client.rb', line 95

def default_query_params
  @@default_query_params ||= {}
end

.embed_resource(resource, resource_id = nil) ⇒ Object



28
29
30
31
# File 'lib/verticalresponse/api/client.rb', line 28

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



105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/verticalresponse/api/client.rb', line 105

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

.resource_uri_suffixObject



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

def resource_uri_suffix
  []
end

.resource_uri_with_prefix(prefix, *additional_paths) ⇒ Object



120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/verticalresponse/api/client.rb', line 120

def resource_uri_with_prefix(prefix, *additional_paths)
  uri = base_uri_with_prefix(prefix)
  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

.resource_uri_with_token(access_token, *additional_paths) ⇒ Object

Used when posting The access_token at this time seems to only work as GET parameter



137
138
139
# File 'lib/verticalresponse/api/client.rb', line 137

def resource_uri_with_token(access_token, *additional_paths)
  resource_uri(*additional_paths) + "?access_token=#{access_token}"
end