Class: BlueStateDigital::Connection
- Inherits:
-
Object
- Object
- BlueStateDigital::Connection
- Defined in:
- lib/blue_state_digital/connection.rb
Constant Summary collapse
- API_VERSION =
2- API_BASE =
'/page/api'- GRAPH_API_BASE =
'/page/graph'
Instance Attribute Summary collapse
-
#constituent_groups ⇒ Object
readonly
Returns the value of attribute constituent_groups.
-
#constituents ⇒ Object
readonly
Returns the value of attribute constituents.
-
#dataset_maps ⇒ Object
readonly
Returns the value of attribute dataset_maps.
-
#datasets ⇒ Object
readonly
Returns the value of attribute datasets.
Instance Method Summary collapse
- #compute_hmac(path, api_ts, params) ⇒ Object
- #extended_params(path, params) ⇒ Object
- #get_deferred_results(deferred_id) ⇒ Object
-
#initialize(params = {}) ⇒ Connection
constructor
A new instance of Connection.
- #perform_graph_request(call, params, method = 'POST') ⇒ Object
- #perform_request(call, params = {}, method = "GET", body = nil) ⇒ Object
- #perform_request_raw(call, params = {}, method = "GET", body = nil) ⇒ Object
- #retrieve_results(deferred_id) ⇒ Object
-
#set_up_resources ⇒ Object
:doc:.
- #wait_for_deferred_result(deferred_id) ⇒ Object
Constructor Details
#initialize(params = {}) ⇒ Connection
Returns a new instance of Connection.
9 10 11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/blue_state_digital/connection.rb', line 9 def initialize(params = {}) @api_id = params[:api_id] @api_secret = params[:api_secret] @client = Faraday.new(:url => "https://#{params[:host]}/") do |faraday| faraday.request :url_encoded # form-encode POST params if defined?(Rails) && Rails.env.development? faraday.response :logger # log requests to STDOUT end faraday.response :error_middleware faraday.adapter(params[:adapter] || Faraday.default_adapter) # make requests with Net::HTTP by default end set_up_resources end |
Instance Attribute Details
#constituent_groups ⇒ Object (readonly)
Returns the value of attribute constituent_groups.
7 8 9 |
# File 'lib/blue_state_digital/connection.rb', line 7 def constituent_groups @constituent_groups end |
#constituents ⇒ Object (readonly)
Returns the value of attribute constituents.
7 8 9 |
# File 'lib/blue_state_digital/connection.rb', line 7 def constituents @constituents end |
#dataset_maps ⇒ Object (readonly)
Returns the value of attribute dataset_maps.
7 8 9 |
# File 'lib/blue_state_digital/connection.rb', line 7 def dataset_maps @dataset_maps end |
#datasets ⇒ Object (readonly)
Returns the value of attribute datasets.
7 8 9 |
# File 'lib/blue_state_digital/connection.rb', line 7 def datasets @datasets end |
Instance Method Details
#compute_hmac(path, api_ts, params) ⇒ Object
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/blue_state_digital/connection.rb', line 54 def compute_hmac(path, api_ts, params) # Support Faraday 0.9.0 forward # Faraday now normalizes request parameters via sorting by default but also allows # the params encoder to be configured by client. It includes Faraday::NestedParamsEncoder # and Faraday::FlatParamsEncoder, but a 3rd party one can be provided. # # When computing the hmac, we need to normalize/sort the exact same way. if Faraday::VERSION == "0.8.9" # do it the old way canon_params= params.map { |k, v| "#{k.to_s}=#{v.to_s}" }.join('&') else # 0.9.0+ do it the new way # Find out which one is in use or select default params_encoder = @client.[:params_encoder] || Faraday::Utils.default_params_encoder # Call that params_encoder when creating signing string. Note we must unescape for BSD canon_params = URI.decode_www_form_component(params_encoder.encode(params)) end signing_string = [@api_id, api_ts, path, canon_params].join("\n") OpenSSL::HMAC.hexdigest('sha1', @api_secret, signing_string) end |
#extended_params(path, params) ⇒ Object
81 82 83 84 85 86 |
# File 'lib/blue_state_digital/connection.rb', line 81 def extended_params(path, params) api_ts = Time.now.utc.to_i.to_s extended_params = { api_ver: API_VERSION, api_id: @api_id, api_ts: api_ts }.merge(params) extended_params[:api_mac] = compute_hmac(path, api_ts, extended_params) extended_params end |
#get_deferred_results(deferred_id) ⇒ Object
95 96 97 |
# File 'lib/blue_state_digital/connection.rb', line 95 def get_deferred_results(deferred_id) perform_request '/get_deferred_results', {deferred_id: deferred_id}, "GET" end |
#perform_graph_request(call, params, method = 'POST') ⇒ Object
44 45 46 47 48 49 50 51 52 |
# File 'lib/blue_state_digital/connection.rb', line 44 def perform_graph_request(call, params, method = 'POST') path = GRAPH_API_BASE + call if method == "POST" @client.post do |req| req.url(path, params) end end end |
#perform_request(call, params = {}, method = "GET", body = nil) ⇒ Object
23 24 25 |
# File 'lib/blue_state_digital/connection.rb', line 23 def perform_request(call, params = {}, method = "GET", body = nil) perform_request_raw(call,params,method,body).body end |
#perform_request_raw(call, params = {}, method = "GET", body = nil) ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/blue_state_digital/connection.rb', line 27 def perform_request_raw(call, params = {}, method = "GET", body = nil) path = API_BASE + call if method == "POST" || method == "PUT" @client.send(method.downcase.to_sym) do |req| content_type = params.delete(:content_type) || 'application/x-www-form-urlencoded' accept = params.delete(:accept) || 'text/xml' req.url(path, extended_params(path, params)) req.body = body req..timeout = 120 req.headers['Content-Type'] = content_type req.headers['Accept'] = accept end else @client.get(path, extended_params(path, params)) end end |
#retrieve_results(deferred_id) ⇒ Object
99 100 101 102 103 104 105 106 107 |
# File 'lib/blue_state_digital/connection.rb', line 99 def retrieve_results(deferred_id) begin return get_deferred_results(deferred_id) rescue Faraday::Error::ClientError => e if e.response[:status] == 503 return nil end end end |
#set_up_resources ⇒ Object
:doc:
88 89 90 91 92 93 |
# File 'lib/blue_state_digital/connection.rb', line 88 def set_up_resources # :doc: @constituents = BlueStateDigital::Constituents.new(self) @constituent_groups = BlueStateDigital::ConstituentGroups.new(self) @datasets = BlueStateDigital::Datasets.new(self) @dataset_maps = BlueStateDigital::DatasetMaps.new(self) end |
#wait_for_deferred_result(deferred_id) ⇒ Object
109 110 111 112 113 114 115 116 |
# File 'lib/blue_state_digital/connection.rb', line 109 def wait_for_deferred_result(deferred_id) result = nil while result.nil? || (result.respond_to?(:length) && result.length == 0) result = retrieve_results(deferred_id) sleep(2) if result.nil? || (result.respond_to?(:length) && result.length == 0) end result end |