Class: OpenStreetMap::Api

Inherits:
Object
  • Object
show all
Includes:
ChangesetCallbacks, HTTParty
Defined in:
lib/open_street_map/api.rb

Constant Summary collapse

API_VERSION =
"0.6".freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from ChangesetCallbacks

before_write, included

Constructor Details

#initialize(client = nil) ⇒ Api

Returns a new instance of Api.



20
21
22
# File 'lib/open_street_map/api.rb', line 20

def initialize(client=nil)
  @client = client
end

Instance Attribute Details

#changesetObject

Returns the value of attribute changeset.



18
19
20
# File 'lib/open_street_map/api.rb', line 18

def changeset
  @changeset
end

#clientObject

Returns the value of attribute client.



16
17
18
# File 'lib/open_street_map/api.rb', line 16

def client
  @client
end

Instance Method Details

#changeset!Object



24
25
26
# File 'lib/open_street_map/api.rb', line 24

def changeset!
  @changeset ||= create_changeset
end

#close_changesetObject



106
107
108
# File 'lib/open_street_map/api.rb', line 106

def close_changeset
  put("/changeset/#{changeset.id}/close")
end

#create(element) ⇒ Object



89
90
91
# File 'lib/open_street_map/api.rb', line 89

def create(element)
  put("/#{element.type.downcase}/create", :body => element.to_xml)
end

#create_changesetObject



100
101
102
103
104
# File 'lib/open_street_map/api.rb', line 100

def create_changeset
  changeset = Changeset.new
  changeset_id = put("/changeset/create", :body => changeset.to_xml).to_i
  find_changeset(changeset_id) unless changeset_id == 0
end

#destroy(element) ⇒ Object

Delete an element

Raises:



72
73
74
75
76
77
# File 'lib/open_street_map/api.rb', line 72

def destroy(element)
  raise ChangesetMissing unless changeset.open?
  element.changeset = changeset.id
  response = delete("/#{element.type.downcase}/#{element.id}", :body => element.to_xml) unless element.id.nil?
  response.to_i # New version number
end

#find_changeset(id) ⇒ Object

Get a Changeset with specified ID from API.

call-seq: find_changeset(id) -> OpenStreetMap::Changeset



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

def find_changeset(id)
  find_element('changeset', id)
end

#find_changesets_for_user(options = {}) ⇒ Object



110
111
112
113
114
# File 'lib/open_street_map/api.rb', line 110

def find_changesets_for_user(options = {})
  user_id = find_user.id
  changesets = get("/changesets", :query => options.merge({:user => user_id}))
  changesets.nil? ? [] : changesets
end

#find_node(id) ⇒ Object

Get a Node with specified ID from API.

call-seq: find_node(id) -> OpenStreetMap::Node



32
33
34
# File 'lib/open_street_map/api.rb', line 32

def find_node(id)
  find_element('node', id)
end

#find_relation(id) ⇒ Object

Get a Relation with specified ID from API.

call-seq: find_relation(id) -> OpenStreetMap::Relation



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

def find_relation(id)
  find_element('relation', id)
end

#find_userObject

Get the user which represented by the OpenStreetMap::Client

call-seq: find_user -> OpenStreetMap::User

Raises:



64
65
66
67
68
69
# File 'lib/open_street_map/api.rb', line 64

def find_user
  raise CredentialsMissing if client.nil?
  resp = do_authenticated_request(:get, "/user/details")
  raise resp if resp.is_a? String
  resp
end

#find_way(id) ⇒ Object

Get a Way with specified ID from API.

call-seq: find_way(id) -> OpenStreetMap::Way



40
41
42
# File 'lib/open_street_map/api.rb', line 40

def find_way(id)
  find_element('way', id)
end

#save(element) ⇒ Object

Saves an element to the API. If it has no id yet, the element will be created, otherwise updated.



81
82
83
84
85
86
87
# File 'lib/open_street_map/api.rb', line 81

def save(element)
  response = if element.id.nil?
    create(element)
  else
    update(element)
  end
end

#update(element) ⇒ Object

Raises:



93
94
95
96
97
98
# File 'lib/open_street_map/api.rb', line 93

def update(element)
  raise ChangesetMissing unless changeset.open?
  element.changeset = changeset.id
  response = put("/#{element.type.downcase}/#{element.id}", :body => element.to_xml)
  response.to_i # New Version number
end