Module: Scribesend

Defined in:
lib/scribesend.rb,
lib/scribesend/util.rb,
lib/scribesend/entry.rb,
lib/scribesend/error.rb,
lib/scribesend/account.rb,
lib/scribesend/version.rb,
lib/scribesend/entry_line.rb,
lib/scribesend/list_object.rb,
lib/scribesend/api_resource.rb,
lib/scribesend/scribesend_object.rb

Defined Under Namespace

Modules: Util Classes: APIResource, Account, Entry, EntryLine, Error, ListObject, ScribesendObject

Constant Summary collapse

VERSION =
File.open(File.expand_path("../../../VERSION", __FILE__)).read().strip

Class Method Summary collapse

Class Method Details

.api_baseObject



38
39
40
# File 'lib/scribesend.rb', line 38

def self.api_base
  @api_base
end

.api_keyObject



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

def self.api_key
  @api_key
end

.api_key=(api_key) ⇒ Object



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

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

.api_url(url = '', api_base_url = nil) ⇒ Object



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

def self.api_url(url='', api_base_url=nil)
  (api_base_url || @api_base) + url
end

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



42
43
44
45
46
47
48
49
50
51
52
53
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
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
# File 'lib/scribesend.rb', line 42

def self.request(method, url, api_key, params={}, headers={}, api_base_url=nil)
  api_base_url = api_base_url || @api_base

  unless api_key ||= @api_key
    raise Error.new("No API key provided. " \
      "Set your API key using 'Scribesend.api_key = <API-KEY>'.")
  end

  params = Util.objects_to_ids(params)
  url = api_url(url, api_base_url)

  case method.to_s.downcase.to_sym
  when :get, :head, :delete
    # Make params into GET parameters
    url += "#{URI.parse(url).query ? '&' : '?'}#{uri_encode(params)}" if params && params.any?
    payload = nil
  else
    if headers[:content_type] && (headers[:content_type] == "multipart/form-data" || headers[:content_type] == "application/json")
      payload = params
    else
      payload = uri_encode(params)
    end
  end

  if headers[:content_type] && headers[:content_type] == "application/json"
    content_type = headers[:content_type]
  else
    content_type = "application/x-www-form-urlencoded"
  end

  headers = {
    :user_agent => "Scribesend/v0 RubyClient/#{Scribesend::VERSION}",
    :authorization => "Bearer #{api_key}",
    :content_type => content_type
  }.update(headers)

  request_opts = {
    :method => method,
    :headers => headers,
    :url => url,
    :payload => payload,
    :verify_ssl => false,
    :open_timeout => 30,
    :timeout => 80, 
  }

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

  [parse(response), api_key]
end