Class: SquarespaceApi::Connection

Inherits:
Object
  • Object
show all
Defined in:
lib/squarespace_api/connection.rb

Constant Summary collapse

DEFAULT_HEADERS =
{
  'Content-Type' => 'application/json',
  'User-Agent' => 'SquarespaceApi'
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Connection

Returns a new instance of Connection.



15
16
17
18
19
20
21
22
23
24
# File 'lib/squarespace_api/connection.rb', line 15

def initialize(config)
  @config = config
  @connection = Faraday.new do |connection|
    connection.headers = DEFAULT_HEADERS
    connection.response :json
    connection.adapter(Faraday.default_adapter)
  end

  initialize_app_session if config.access_token
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



13
14
15
# File 'lib/squarespace_api/connection.rb', line 13

def config
  @config
end

Instance Method Details

#delete(path, params: {}, headers: {}) ⇒ Object



34
35
36
# File 'lib/squarespace_api/connection.rb', line 34

def delete(path, params: {}, headers: {})
  handle_response(connection.delete(path, params, connection.headers.merge(headers)))
end

#get(path, params: {}, headers: {}) ⇒ Object



26
27
28
# File 'lib/squarespace_api/connection.rb', line 26

def get(path, params: {}, headers: {})
  handle_response(connection.get(path, params, connection.headers.merge(headers)))
end

#initialize_app_sessionObject



59
60
61
62
63
# File 'lib/squarespace_api/connection.rb', line 59

def initialize_app_session
  @connection.headers['Authorization'] = "Bearer #{config.access_token}"
  @connection.url_prefix = api_base_url
  self
end

#initialize_oauth_sessionObject



70
71
72
73
74
# File 'lib/squarespace_api/connection.rb', line 70

def initialize_oauth_session
  @connection.headers['Authorization'] = "Basic #{config.encoded_oauth_token}"
  @connection.url_prefix = config.oauth_base_url
  self
end

#post(path, params: {}, headers: {}) ⇒ Object



30
31
32
# File 'lib/squarespace_api/connection.rb', line 30

def post(path, params: {}, headers: {})
  handle_response(connection.post(path, params, connection.headers.merge(headers)))
end

#upload_file(path, file_path) ⇒ Object



38
39
40
41
42
43
44
45
46
47
# File 'lib/squarespace_api/connection.rb', line 38

def upload_file(path, file_path)
  temp_connection = Faraday.new(url: api_base_url) do |f|
    f.request :multipart
    f.response :json
    f.headers = DEFAULT_HEADERS.merge(
      'Authorization' => "Bearer #{config.access_token}",
      'Content-Type' => 'multipart/form-data',
    )
  end.post(path, { file: Faraday::FilePart.new(file_path, 'text/x-ruby') })
end

#with_app_sessionObject



54
55
56
57
# File 'lib/squarespace_api/connection.rb', line 54

def with_app_session
  initialize_app_session
  yield
end

#with_idempotency_keyObject



49
50
51
52
# File 'lib/squarespace_api/connection.rb', line 49

def with_idempotency_key
  @connection.headers['Idempotency-Key'] = SecureRandom.hex
  yield
end

#with_oauth_sessionObject



65
66
67
68
# File 'lib/squarespace_api/connection.rb', line 65

def with_oauth_session
  initialize_oauth_session
  yield
end