Commit c7f0a34a5cb5f7a9a7344019089d68ac74e1ba15
- Diff rendering mode:
- inline
- side by side
lib/couch_object/view.rb
(37 / 10)
|   | |||
| 65 | 65 | # * CouchObject::Errors::MissingView if the view doesn't exist | |
| 66 | 66 | # | |
| 67 | 67 | def query(params = {}) | |
| 68 | |||
| 69 | response = raw_query(params) | ||
| 70 | |||
| 71 | rows_to_return = [] | ||
| 72 | response["rows"].each do |params_for_object| | ||
| 73 | rows_to_return << params_for_object["value"] | ||
| 74 | end | ||
| 75 | |||
| 76 | rows_to_return | ||
| 77 | |||
| 78 | end | ||
| 79 | |||
| 80 | # | ||
| 81 | # Returns the response data from CouchDB for a view query without | ||
| 82 | # | ||
| 83 | # Takes: | ||
| 84 | # * [+params+] (hash): a hash of URL query arguments supported | ||
| 85 | # by couchDB. If omitted it defaults to not use a key | ||
| 86 | # and to update the view. | ||
| 87 | # | ||
| 88 | # Example: | ||
| 89 | # | ||
| 90 | # view.raw_query({:update => false, :key => "bar"}) => data | ||
| 91 | # | ||
| 92 | # Returns: | ||
| 93 | # * the response data for the view | ||
| 94 | # | ||
| 95 | # Raises: | ||
| 96 | # * CouchObject::Errors::MissingView if the view doesn't exist | ||
| 97 | # | ||
| 98 | def raw_query(params = {}) | ||
| 99 | |||
| 68 | 100 | #Create a querystring with the parameters passed inn | |
| 69 | 101 | querystring = "?" | |
| 70 | 102 | params.each_pair do |key, value| | |
| … | … | ||
| 107 | 107 | ||
| 108 | 108 | view_with_parameters = name + querystring | |
| 109 | 109 | ||
| 110 | rows_to_return = [] | ||
| 111 | |||
| 112 | 110 | response = JSON.parse(db.get(view_with_parameters).body) | |
| 113 | 111 | ||
| 114 | 112 | raise CouchObject::Errors::MissingView, \ | |
| … | … | ||
| 119 | 119 | raise CouchObject::Errors::CouchDBError, \ | |
| 120 | 120 | "CouchDB returned and error and described the problem as #{response['reason']}" \ | |
| 121 | 121 | if response["error"] | |
| 122 | |||
| 123 | response["rows"].each do |params_for_object| | ||
| 124 | rows_to_return << params_for_object["value"] | ||
| 125 | end | ||
| 126 | 122 | ||
| 127 | rows_to_return | ||
| 123 | return response | ||
| 128 | 124 | ||
| 129 | 125 | end | |
| 130 | 126 | ||
| … | … | ||
| 128 | 128 | @db.delete("/#{db.name}/#{name}") | |
| 129 | 129 | end | |
| 130 | 130 | ||
| 131 | protected | ||
| 132 | def self.get_db(db) | ||
| 131 | protected | ||
| 132 | def self.get_db(db) | ||
| 133 | 133 | case db.class.to_s | |
| 134 | 134 | when "CouchObject::Database" | |
| 135 | 135 | db | |
| … | … | ||
| 140 | 140 | "or a CouchObject::Database object" | |
| 141 | 141 | end | |
| 142 | 142 | end | |
| 143 | end | ||
| 143 | |||
| 144 | end | ||
| 144 | 145 | end |

