Class: Spire::API::Requestable::Request

Inherits:
Object
  • Object
show all
Defined in:
lib/spire/api/requestable.rb

Defined Under Namespace

Classes: Response

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client, options) ⇒ Request

Returns a new instance of Request.



54
55
56
57
58
59
60
61
62
# File 'lib/spire/api/requestable.rb', line 54

def initialize(client, options)
  @client = client
  @method = options.delete(:method) 
  @url = options.delete(:url) 
  @options = options
  @options[:headers] = {
    "User-Agent" => "Ruby spire.io client"
  }.merge(@options[:headers] || {})
end

Instance Attribute Details

#urlObject

Url of the request



53
54
55
# File 'lib/spire/api/requestable.rb', line 53

def url
  @url
end

Instance Method Details

#bodyObject



68
69
70
# File 'lib/spire/api/requestable.rb', line 68

def body
  @options[:body]
end

#body=(val) ⇒ Object



72
73
74
# File 'lib/spire/api/requestable.rb', line 72

def body=(val)
  @options[:body] = val
end

#execObject



80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/spire/api/requestable.rb', line 80

def exec
  response = Response.new(@client.send(@method, @url, @options))
  headers = response.headers
  content_type = headers.values_at("Content-Type", "content-type").compact.first
  if content_type =~ /json/ && [200, 201].include?(response.status)
    # FIXME: detect appropriate response headers, rather than rescuing
    begin
      response.data = API.deserialize(response.body)
    rescue
    end
  end
  response
end

#headersObject



64
65
66
# File 'lib/spire/api/requestable.rb', line 64

def headers
  @options[:headers]
end

#queryObject



76
77
78
# File 'lib/spire/api/requestable.rb', line 76

def query
  @options[:query]
end