Module: Investolink

Defined in:
lib/investolink.rb,
lib/investolink/json.rb,
lib/investolink/util.rb,
lib/investolink/asset.rb,
lib/investolink/issue.rb,
lib/investolink/issuer.rb,
lib/investolink/reward.rb,
lib/investolink/project.rb,
lib/investolink/version.rb,
lib/investolink/donation.rb,
lib/investolink/resource.rb,
lib/investolink/sociallink.rb,
lib/investolink/subscriber.rb,
lib/investolink/fundallocation.rb,
lib/investolink/operations/list.rb,
lib/investolink/errors/api_error.rb,
lib/investolink/issuer_ownership.rb,
lib/investolink/issue_transaction.rb,
lib/investolink/operations/create.rb,
lib/investolink/operations/update.rb,
lib/investolink/investolink_object.rb,
lib/investolink/errors/investolink_error.rb,
lib/investolink/errors/api_connection_error.rb,
lib/investolink/errors/authentication_error.rb,
lib/investolink/errors/invalid_request_error.rb

Defined Under Namespace

Modules: JSON, Operations, Util Classes: APIConnectionError, APIError, Asset, AuthenticationError, Donation, Fundallocation, InvalidRequestError, InvestolinkError, InvestolinkObject, Issue, IssueTransaction, Issuer, IssuerOwnership, Project, Resource, Reward, Sociallink, Subscriber

Constant Summary collapse

VERSION =
"1.0.2"
@@api_key =
nil
@@api_token =
nil
@@api_base =
"https://api.crowdnetic.com/v1"

Class Method Summary collapse

Class Method Details

.api_baseObject



64
65
66
# File 'lib/investolink.rb', line 64

def self.api_base
  @@api_base
end

.api_base=(api_base) ⇒ Object



60
61
62
# File 'lib/investolink.rb', line 60

def self.api_base=(api_base)
  @@api_base = api_base
end

.api_keyObject



56
57
58
# File 'lib/investolink.rb', line 56

def self.api_key
  @@api_key
end

.api_key=(api_key) ⇒ Object



52
53
54
# File 'lib/investolink.rb', line 52

def self.api_key=(api_key)
  @@api_key = api_key
end

.api_tokenObject



72
73
74
# File 'lib/investolink.rb', line 72

def self.api_token
	@@api_token
end

.api_token=(api_token) ⇒ Object



68
69
70
# File 'lib/investolink.rb', line 68

def self.api_token=(api_token)
	@@api_token = api_token
end

.api_url(url = '') ⇒ Object



48
49
50
# File 'lib/investolink.rb', line 48

def self.api_url(url='')
  @@api_base + url
end

.request(method, url, api_key, api_token, params = nil, headers = {}) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# File 'lib/investolink.rb', line 76

def self.request(method, url, api_key, api_token, params=nil, headers={})
  api_key ||= @@api_key
  api_token ||= @@api_token
  raise AuthenticationError.new('No API key provided.  (HINT: set your API key using "Investolink.api_key = <API-KEY> and Investolink.api_token = <API-TOKEN>".  You can generate API keys from the Crowdnetic web interface.  See http://www.crowdnetic.com for details, or email [email protected] if you have any questions.)') unless api_key

=begin
  if !verify_ssl_certs
    unless @no_verify
      $stderr.puts "WARNING: Running without SSL cert verification.  Execute 'Investolink.verify_ssl_certs = true' to enable verification."
      @no_verify = true
    end
    ssl_opts = { :verify_ssl => false }
  elsif !Util.file_readable(@@ssl_bundle_path)
    unless @no_bundle
      $stderr.puts "WARNING: Running without SSL cert verification because #{@@ssl_bundle_path} isn't readable"
      @no_bundle = true
    end
    ssl_opts = { :verify_ssl => false }
  else
    ssl_opts = {
      :verify_ssl => OpenSSL::SSL::VERIFY_PEER,
      :ssl_ca_file => @@ssl_bundle_path
    }
  end
=end
  ssl_opts = {}

  uname = (@@uname ||= RUBY_PLATFORM =~ /linux|darwin/i ? `uname -a 2>/dev/null`.strip : nil)
  lang_version = "#{RUBY_VERSION} p#{RUBY_PATCHLEVEL} (#{RUBY_RELEASE_DATE})"
  ua = {
    :bindings_version => Investolink::VERSION,
    :lang => 'ruby',
    :lang_version => lang_version,
    :platform => RUBY_PLATFORM,
    :publisher => 'investolink',
    :uname => uname
  }

  params = Util.objects_to_ids(params)
  url = self.api_url(url) + ".json"
  case method.to_s.downcase.to_sym
  when :get, :head, :delete
    # Make params into GET parameters
    if params && params.count
      query_string = Util.flatten_params(params).collect{|p| "#{p[0]}=#{p[1]}"}.join('&')
      url += "?#{query_string}"
    end
    payload = nil
  else
    payload = params
  end

  begin
    headers = { :x_investolink_client_user_agent => Investolink::JSON.dump(ua) }.merge(headers)
  rescue => e
    headers = {
      :x_investolink_client_raw_user_agent => ua.inspect,
      :error => "#{e} (#{e.class})"
    }.merge(headers)
  end

  headers = {
    :user_agent => "Investolink/v1 RubyBindings/#{Investolink::VERSION}",
    :x_api_key => "#{api_key}",
    :x_api_token => "#{api_token}"
  }.merge(headers)
  opts = {
    :method => method,
    :url => url,
    :headers => headers,
    :open_timeout => 30,
    :payload => payload,
    :timeout => 80
  }.merge(ssl_opts)

  begin
    response = execute_request(opts)
  rescue SocketError => e
    self.handle_restclient_error(e)
  rescue NoMethodError => e
    # Work around RestClient bug
    if e.error_message =~ /\WRequestFailed\W/
      e = APIConnectionError.new('Unexpected HTTP response code')
      self.handle_restclient_error(e)
    else
      raise
    end
  rescue RestClient::ExceptionWithResponse => e
    if rcode = e.http_code and rbody = e.http_body
      self.handle_api_error(rcode, rbody)
    else
      self.handle_restclient_error(e)
    end
  rescue RestClient::Exception, Errno::ECONNREFUSED => e
    self.handle_restclient_error(e)
  end

  rbody = response.body
  rcode = response.code
  begin
    # Would use :symbolize_names => true, but apparently there is
    # some library out there that makes symbolize_names not work.
    resp = Investolink::JSON.load(rbody)
  rescue MultiJson::DecodeError
    raise APIError.new("Invalid response object from API: #{rbody.inspect} (HTTP response code was #{rcode})", rcode, rbody)
  end

  resp = Util.symbolize_names(resp)
  [resp, api_key, api_token]
end