Class: EngagingNetworks::API

Inherits:
Vertebrae::API
  • Object
show all
Defined in:
lib/engaging_networks/api.rb

Direct Known Subclasses

Client

Instance Method Summary collapse

Instance Method Details

#default_optionsObject



10
11
12
13
14
15
16
# File 'lib/engaging_networks/api.rb', line 10

def default_options
  {
    user_agent: 'EngagingNetworksGem',
    host: 'e-activist.com',
    content_type: 'application/x-www-form-urlencoded'
  }
end

#extract_data_from_params(params) ⇒ Object

:nodoc:



18
19
20
21
22
23
24
# File 'lib/engaging_networks/api.rb', line 18

def extract_data_from_params(params) # :nodoc:
  if params.has_key?('data') && params['data'].present?
    return params['data']
  else
    return params
  end
end

#post_request_with_get_params(path, get_params, post_data, options = {}) ⇒ Object

:nodoc:



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/engaging_networks/api.rb', line 67

def post_request_with_get_params(path, get_params, post_data, options={}) # :nodoc:
  method = :post

  if !::Vertebrae::Request::METHODS.include?(method)
    raise ArgumentError, "unknown http method: #{method}"
  end

  path =  connection.configuration.prefix + '/' + path

  ::Vertebrae::Base.logger.debug "EXECUTED: #{method} - #{path}? #{get_params} with #{post_data} and #{options}"

  connection.connection.send(method) do |request|

    case method.to_sym
      when *(::Vertebrae::Request::METHODS - ::Vertebrae::Request::METHODS_WITH_BODIES)
        request.body = get_params.delete('data') if get_params.has_key?('data')
        request.url(path, get_params)
      when *::Vertebrae::Request::METHODS_WITH_BODIES
        request.path = path
        request.body = extract_data_from_params(post_data) unless post_data.empty?
        request.url(path, get_params)
    end
  end
end

#request(*args) ⇒ Object



6
7
8
# File 'lib/engaging_networks/api.rb', line 6

def request(*args)
  EngagingNetworks::Response::Wrapper.new( super(*args) )
end

#request_with_options(method, path, params, options) ⇒ Object

:nodoc:



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/engaging_networks/api.rb', line 46

def request_with_options(method, path, params, options) # :nodoc:
  if !::Vertebrae::Request::METHODS.include?(method)
    raise ArgumentError, "unknown http method: #{method}"
  end
  connection.options = default_options.merge(options)
  path =  connection.configuration.prefix + '/' + path

  ::Vertebrae::Base.logger.debug "EXECUTED: #{method} - #{path} with #{params} and #{options}"

  connection.connection.send(method) do |request|

    case method.to_sym
      when *(::Vertebrae::Request::METHODS - ::Vertebrae::Request::METHODS_WITH_BODIES)
        request.url(path, params)
      when *::Vertebrae::Request::METHODS_WITH_BODIES
        request.path = path
        request.body = params
    end
  end
end

#setupObject



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/engaging_networks/api.rb', line 26

def setup
  connection.stack do |builder|
    #request middleware first, in order of importance
    builder.use EngagingNetworks::Request::MultiTokenAuthentication,
      :public_token => connection.configuration.options[:public_token],
      :private_token => connection.configuration.options[:private_token]

    builder.use Faraday::Request::Multipart
    builder.use Faraday::Request::UrlEncoded

    #response middleware second, in reverse order of importance
    builder.use FaradayMiddleware::ParseXml,  :content_type => /\bxml$/
    
    builder.use Faraday::Response::Logger if ENV['DEBUG']

    builder.use EngagingNetworks::Response::RaiseError
    builder.adapter connection.configuration.adapter
  end
end