Class: Patchboard::Mapping

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api, name, definition) ⇒ Mapping

Returns a new instance of Mapping.



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/patchboard/api.rb', line 90

def initialize(api, name, definition)
  @api = api
  @name = name
  @definition = definition
  @resource = @definition[:resource]
  @query = @definition[:query]
  @url = @definition[:url]
  @path = @definition[:path]
  @template = @definition[:template]

  unless (resource_name = @definition[:resource])
    raise "Mapping does not specify 'resource'"
  end

  unless (@resource = @api.resources[resource_name.to_sym])
    raise "Mapping specifies a resource that is not defined"
  end

  unless (@definition[:url] || @definition[:path] || @definition[:template])
    raise "Mapping is missing any form of URL specification"
  end

end

Instance Attribute Details

#klassObject

Returns the value of attribute klass.



87
88
89
# File 'lib/patchboard/api.rb', line 87

def klass
  @klass
end

#nameObject (readonly)

Returns the value of attribute name.



88
89
90
# File 'lib/patchboard/api.rb', line 88

def name
  @name
end

#pathObject (readonly)

Returns the value of attribute path.



88
89
90
# File 'lib/patchboard/api.rb', line 88

def path
  @path
end

#queryObject (readonly)

Returns the value of attribute query.



88
89
90
# File 'lib/patchboard/api.rb', line 88

def query
  @query
end

#resourceObject (readonly)

Returns the value of attribute resource.



88
89
90
# File 'lib/patchboard/api.rb', line 88

def resource
  @resource
end

#templateObject (readonly)

Returns the value of attribute template.



88
89
90
# File 'lib/patchboard/api.rb', line 88

def template
  @template
end

#urlObject (readonly)

Returns the value of attribute url.



88
89
90
# File 'lib/patchboard/api.rb', line 88

def url
  @url
end

Instance Method Details

#generate_url(params = {}) ⇒ Object



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
# File 'lib/patchboard/api.rb', line 114

def generate_url(params={})
  if @url
    base = @url
  elsif params[:url]
    base = params[:url]
  elsif @path
    if @api.service_url
      base = [@api.service_url, @path].join("/")
    else
      raise "Tried to generate url from path, but API did not define service_url"
    end
  elsif @template
    raise "Template mappings are not yet implemented in the client"
  end

  if @query
    parts = []
    keys = @query.keys.sort()
    # TODO check query schema
    keys.each do |key|
      if string = (params[key.to_s] || params[key.to_sym])
        parts << "#{CGI::escape(key.to_s)}=#{CGI::escape(string)}"
      end
    end
    if parts.size > 0
      query_string = "?#{parts.join("&")}"
    else
      query_string = ""
    end
    [base, query_string].join()
  else
    base
  end
end