Class: Episodic::Platform::Connection

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

Overview

Class used to make the actual requests to the Episodic Platform API.

Defined Under Namespace

Modules: Management

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(episodic_api_key, episodic_secret_key, options = {}) ⇒ Connection

Constructor

episodic_api_key<String>

The caller’s Episodic API Key

episodic_secret_key<String>

The caller’s Episodic Secret Key

options<Hash>

Used mostly for testing by allowing the caller to override some constants such

the API host.


19
20
21
22
23
# File 'lib/episodic/platform/connection.rb', line 19

def initialize(episodic_api_key, episodic_secret_key, options = {})
  @episodic_api_key = episodic_api_key
  @episodic_secret_key = episodic_secret_key
  @connection_options = options
end

Instance Attribute Details

#connection_optionsObject (readonly)

Returns the value of attribute connection_options.



9
10
11
# File 'lib/episodic/platform/connection.rb', line 9

def connection_options
  @connection_options
end

#episodic_api_keyObject (readonly)

Returns the value of attribute episodic_api_key.



9
10
11
# File 'lib/episodic/platform/connection.rb', line 9

def episodic_api_key
  @episodic_api_key
end

#episodic_secret_keyObject (readonly)

Returns the value of attribute episodic_secret_key.



9
10
11
# File 'lib/episodic/platform/connection.rb', line 9

def episodic_secret_key
  @episodic_secret_key
end

Instance Method Details

#do_get(url, params) ⇒ Object

Perform the GET request to the specified URL. This method takes the params passed in and generates the signature parameter using the Episodic Secret Key for this connection. The signature, the Episodic API Key for this connection and the passed in params are then used to generate the query string.

Parameters

url<URI>

The URL to the Episodic Platform API endpoint

params<Hash>

A hash of parameters to include include in the query string.

Returns

Episodic::Platform::HTTPResponse

The full response object.



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/episodic/platform/connection.rb', line 83

def do_get url, params
  
  # Convert all the params to strings
  request_params = convert_params_for_request(params)
  
  # Add in the common params
  append_common_params(request_params)
  
  queryString = ""
  request_params.keys.each_with_index do |key, index|
    queryString << "#{index == 0 ? '?' : '&'}#{key}=#{::URI.escape(request_params[key])}"
  end
  
  # Create the request
  http = Net::HTTP.new(url.host, url.port)
  response = http.start() {|req| req.get(url.path + queryString)}
  
  return Episodic::Platform::HTTPResponse.new(response.code, response.body)
end

#do_post(url, params, file_params = nil) ⇒ Object

Perform the POST request to the specified URL. This method takes the params passed in and generates the signature parameter using the Episodic Secret Key for this connection. The signature, the Episodic API Key for this connection and the passed in params are then used to generate the form post.

If there are any filenames passed then these are also included in the form post.

Parameters

url<URI>

The URL to the Episodic Platform API endpoint

params<Hash>

A hash of parameters to include include in the post request.

file_params<Hash>

A hash of file parameters. The name is the parameter name and value is the path to the file.

Returns

Episodic::Platform::HTTPResponse

The full response object.



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/episodic/platform/connection.rb', line 43

def do_post url, params, file_params = nil
  
  # Convert all the params to strings
  request_params = convert_params_for_request(params)
  
  # Add in the common params
  append_common_params(request_params)
  
  c = Curl::Easy.new(url.to_s)  
  c.multipart_form_post = true
  
  fields = []
  request_params.each_pair do |name, value|
    fields << Curl::PostField.content(name, value)
  end
  file_params.each do |name, value|
    fields << Curl::PostField.file(name, value) 
  end unless file_params.nil?
  
  # Make the request
  c.http_post(*fields)
  
  return Episodic::Platform::HTTPResponse.new(c.response_code, c.body_str)
end