Class: SocialSnippet::Registry::RegistryResources::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/social_snippet/registry/registry_resources/base.rb

Direct Known Subclasses

Repositories

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(new_social_snippet) ⇒ Base

Returns a new instance of Base


17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/social_snippet/registry/registry_resources/base.rb', line 17

def initialize(new_social_snippet)
  @social_snippet = new_social_snippet
  @host           = social_snippet.config.sspm_host
  @api_version    = social_snippet.config.sspm_version
  @protocol       = social_snippet.config.sspm_protocol
  @end_point      = "#{protocol}://#{host}/api/#{api_version}"
  @rest_client    = ::RestClient::Resource.new(end_point)
  @cookies        = {}
  @default_headers = {
    :accept => :json,
  }

  social_snippet.logger.debug "registry: end-point = #{end_point}"
end

Instance Attribute Details

#api_versionObject (readonly)

Returns the value of attribute api_version


12
13
14
# File 'lib/social_snippet/registry/registry_resources/base.rb', line 12

def api_version
  @api_version
end

#cookiesObject (readonly)

Returns the value of attribute cookies


14
15
16
# File 'lib/social_snippet/registry/registry_resources/base.rb', line 14

def cookies
  @cookies
end

#default_headersObject (readonly)

Returns the value of attribute default_headers


15
16
17
# File 'lib/social_snippet/registry/registry_resources/base.rb', line 15

def default_headers
  @default_headers
end

#end_pointObject (readonly)

Returns the value of attribute end_point


13
14
15
# File 'lib/social_snippet/registry/registry_resources/base.rb', line 13

def end_point
  @end_point
end

#hostObject (readonly)

Returns the value of attribute host


11
12
13
# File 'lib/social_snippet/registry/registry_resources/base.rb', line 11

def host
  @host
end

#protocolObject (readonly)

Returns the value of attribute protocol


10
11
12
# File 'lib/social_snippet/registry/registry_resources/base.rb', line 10

def protocol
  @protocol
end

#rest_clientObject (readonly)

Returns the value of attribute rest_client


9
10
11
# File 'lib/social_snippet/registry/registry_resources/base.rb', line 9

def rest_client
  @rest_client
end

#social_snippetObject (readonly)

Returns the value of attribute social_snippet


8
9
10
# File 'lib/social_snippet/registry/registry_resources/base.rb', line 8

def social_snippet
  @social_snippet
end

Instance Method Details

#get(req_path, headers = {}) ⇒ Object


50
51
52
53
54
55
56
# File 'lib/social_snippet/registry/registry_resources/base.rb', line 50

def get(req_path, headers = {})
  social_snippet.logger.debug "registry: get #{req_path}"
  headers.merge! default_headers
  social_snippet.logger.debug "registry: headers:"
  social_snippet.logger.debug headers
  parse_response rest_client[req_path].get(headers)
end

#post(req_path, params, headers = {}) ⇒ Object


32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/social_snippet/registry/registry_resources/base.rb', line 32

def post(req_path, params, headers = {})
  social_snippet.logger.debug "registry: post: #{req_path}"
  social_snippet.logger.debug params
  csrf_token = fetch_csrf_token

  # set headers
  headers.merge! default_headers
  headers["Content-Type"] = "application/json"
  headers["X-CSRF-Token"] = csrf_token

  # debug output
  social_snippet.logger.debug "registry: post: csrf_token = #{csrf_token}"
  social_snippet.logger.debug "registry: post: headers:"
  social_snippet.logger.debug headers

  parse_response rest_client[req_path].post(params.to_json, headers)
end