Class: Thingiverse::Things

Inherits:
Object
  • Object
show all
Includes:
ActiveModel::Validations
Defined in:
lib/thingiverse/things.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params = {}) ⇒ Things

Returns a new instance of Things.



11
12
13
14
15
# File 'lib/thingiverse/things.rb', line 11

def initialize(params={})
  params.each do |name, value|
    send("#{name}=", value)
  end
end

Instance Attribute Details

#addedObject

Returns the value of attribute added.



6
7
8
# File 'lib/thingiverse/things.rb', line 6

def added
  @added
end

#ancestorsObject

Returns the value of attribute ancestors.



9
10
11
# File 'lib/thingiverse/things.rb', line 9

def ancestors
  @ancestors
end

#ancestors_urlObject

Returns the value of attribute ancestors_url.



8
9
10
# File 'lib/thingiverse/things.rb', line 8

def ancestors_url
  @ancestors_url
end

#categories_urlObject

Returns the value of attribute categories_url.



8
9
10
# File 'lib/thingiverse/things.rb', line 8

def categories_url
  @categories_url
end

#categoryObject

Returns the value of attribute category.



9
10
11
# File 'lib/thingiverse/things.rb', line 9

def category
  @category
end

#creatorObject

Returns the value of attribute creator.



6
7
8
# File 'lib/thingiverse/things.rb', line 6

def creator
  @creator
end

#derivatives_urlObject

Returns the value of attribute derivatives_url.



8
9
10
# File 'lib/thingiverse/things.rb', line 8

def derivatives_url
  @derivatives_url
end

#descriptionObject

Returns the value of attribute description.



7
8
9
# File 'lib/thingiverse/things.rb', line 7

def description
  @description
end

#files_urlObject

Returns the value of attribute files_url.



8
9
10
# File 'lib/thingiverse/things.rb', line 8

def files_url
  @files_url
end

#idObject

Returns the value of attribute id.



6
7
8
# File 'lib/thingiverse/things.rb', line 6

def id
  @id
end

#images_urlObject

Returns the value of attribute images_url.



8
9
10
# File 'lib/thingiverse/things.rb', line 8

def images_url
  @images_url
end

#instructionsObject

Returns the value of attribute instructions.



7
8
9
# File 'lib/thingiverse/things.rb', line 7

def instructions
  @instructions
end

#is_publishedObject

Returns the value of attribute is_published.



6
7
8
# File 'lib/thingiverse/things.rb', line 6

def is_published
  @is_published
end

#is_wipObject

Returns the value of attribute is_wip.



6
7
8
# File 'lib/thingiverse/things.rb', line 6

def is_wip
  @is_wip
end

#licenseObject

Returns the value of attribute license.



7
8
9
# File 'lib/thingiverse/things.rb', line 7

def license
  @license
end

#like_countObject

Returns the value of attribute like_count.



7
8
9
# File 'lib/thingiverse/things.rb', line 7

def like_count
  @like_count
end

#likes_urlObject

Returns the value of attribute likes_url.



8
9
10
# File 'lib/thingiverse/things.rb', line 8

def likes_url
  @likes_url
end

#modifiedObject

Returns the value of attribute modified.



6
7
8
# File 'lib/thingiverse/things.rb', line 6

def modified
  @modified
end

#nameObject

Returns the value of attribute name.



6
7
8
# File 'lib/thingiverse/things.rb', line 6

def name
  @name
end

#public_urlObject

Returns the value of attribute public_url.



6
7
8
# File 'lib/thingiverse/things.rb', line 6

def public_url
  @public_url
end

#ratings_enabledObject

Returns the value of attribute ratings_enabled.



7
8
9
# File 'lib/thingiverse/things.rb', line 7

def ratings_enabled
  @ratings_enabled
end

#tagsObject

Returns the value of attribute tags.



9
10
11
# File 'lib/thingiverse/things.rb', line 9

def tags
  @tags
end

#tags_urlObject

Returns the value of attribute tags_url.



8
9
10
# File 'lib/thingiverse/things.rb', line 8

def tags_url
  @tags_url
end

#thumbnailObject

Returns the value of attribute thumbnail.



6
7
8
# File 'lib/thingiverse/things.rb', line 6

def thumbnail
  @thumbnail
end

#urlObject

Returns the value of attribute url.



6
7
8
# File 'lib/thingiverse/things.rb', line 6

def url
  @url
end

Class Method Details

.create(params) ⇒ Object



163
164
165
166
167
168
169
170
171
# File 'lib/thingiverse/things.rb', line 163

def self.create(params)
  thing = self.new(params)
  raise "Invalid Parameters" unless thing.valid?

  response = Thingiverse::Connection.post('/things', :body => thing.attributes.to_json)
  raise "#{response.code}: #{JSON.parse(response.body)['error']} #{response.headers['x-error']}" unless response.success?

  self.new(response.parsed_response)
end

.find(thing_id) ⇒ Object



153
154
155
156
157
# File 'lib/thingiverse/things.rb', line 153

def self.find(thing_id)
  response = Thingiverse::Connection.get("/things/#{thing_id}")
  raise "#{response.code}: #{JSON.parse(response.body)['error']} #{response.headers['x-error']}" unless response.success?
  self.new response.parsed_response
end

.newest(query = {}) ⇒ Object



159
160
161
# File 'lib/thingiverse/things.rb', line 159

def self.newest(query = {})
  Thingiverse::Pagination.new(Thingiverse::Connection.get('/newest', :query => query), Thingiverse::Things)
end

Instance Method Details

#attributesObject



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/thingiverse/things.rb', line 17

def attributes
  {
    :id => id.to_s,
    :name => name.to_s,
    :thumbnail => thumbnail.to_s,
    :url => url.to_s,
    :public_url => public_url.to_s,
    :creator => creator.to_s,
    :added => added.to_s,
    :modified => modified.to_s,
    :is_published => is_published != true ? false : true,
    :is_wip => is_wip != true ? false : true,
    :ratings_enabled => ratings_enabled != true ? false : true,
    :like_count => like_count.to_s,
    :description => description.to_s,
    :instructions => instructions.to_s,
    :license => license.to_s,
    :files_url => files_url.to_s,
    :images_url => images_url.to_s,
    :likes_url => likes_url.to_s,
    :ancestors_url => ancestors_url.to_s,
    :derivatives_url => derivatives_url.to_s,
    :tags_url => tags_url.to_s,
    :categories_url => categories_url.to_s,
    :category => category.to_s,
    :ancestors => ancestors || [],
    :tags => tags || []
  }
end

#categories(query = {}) ⇒ Object



61
62
63
# File 'lib/thingiverse/things.rb', line 61

def categories(query = {})
  Thingiverse::Pagination.new(Thingiverse::Connection.get(@categories_url, :query => query), Thingiverse::Categories)
end

#files(query = {}) ⇒ Object



53
54
55
# File 'lib/thingiverse/things.rb', line 53

def files(query = {})
  Thingiverse::Pagination.new(Thingiverse::Connection.get(@files_url, :query => query), Thingiverse::Files)
end

#images(query = {}) ⇒ Object



57
58
59
# File 'lib/thingiverse/things.rb', line 57

def images(query = {})
  Thingiverse::Pagination.new(Thingiverse::Connection.get(@images_url, :query => query), Thingiverse::Images)
end

#parents(query = {}) ⇒ Object



65
66
67
# File 'lib/thingiverse/things.rb', line 65

def parents(query = {})
  Thingiverse::Pagination.new(Thingiverse::Connection.get(@ancestors_url, :query => query), Thingiverse::Things)
end

#publishObject



138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/thingiverse/things.rb', line 138

def publish
  if id.to_s == ""
    raise "Cannot publish until thing is saved"
  else
    response = Thingiverse::Connection.post("/things/#{id}/publish")
    raise "#{response.code}: #{JSON.parse(response.body)['error']}" unless response.success?

    thing = Thingiverse::Things.new(response.parsed_response)
  end

  thing.attributes.each do |name, value|
    send("#{name}=", value)
  end
end

#saveObject



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/thingiverse/things.rb', line 78

def save
  if id.to_s == ""
    thing = Thingiverse::Things.create(attributes)
  else
    raise "Invalid Parameters" unless self.valid?

    response = Thingiverse::Connection.patch("/things/#{id}", :body => attributes.to_json)
    raise "#{response.code}: #{JSON.parse(response.body)['error']}" unless response.success?

    thing = Thingiverse::Things.new(response.parsed_response)
  end

  thing.attributes.each do |name, value|
    send("#{name}=", value)
  end
end

#tag_recordsObject

TODO: this is a dumb name, come up with a better way to set/retrieve



70
71
72
73
74
75
76
# File 'lib/thingiverse/things.rb', line 70

def tag_records
  response = Thingiverse::Connection.get(tags_url)
  raise "#{response.code}: #{JSON.parse(response.body)['error']}" unless response.success?
  response.parsed_response.collect do |attrs|
    Thingiverse::Tags.new attrs
  end
end

#upload(file) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/thingiverse/things.rb', line 95

def upload(file)
  response = Thingiverse::Connection.post("/things/#{id}/files", :body => {:filename => File.basename(file.path)}.to_json)
  raise "#{response.code}: #{JSON.parse(response.body)['error']} #{response.headers['x-error']}" unless response.success?

  parsed_response = JSON.parse(response.body)
  action = parsed_response["action"]
  query = parsed_response["fields"]
  query["file"] = file

  # stupid S3 requires params to be in a certain order... so can't use HTTParty :(
  # prepare post data
  post_data = []
  # TODO: is query['bucket'] needed here?
  post_data << Curl::PostField.content('key',                     query['key'])
  post_data << Curl::PostField.content('AWSAccessKeyId',          query['AWSAccessKeyId'])
  post_data << Curl::PostField.content('acl',                     query['acl'])
  post_data << Curl::PostField.content('success_action_redirect', query['success_action_redirect'])
  post_data << Curl::PostField.content('policy',                  query['policy'])
  post_data << Curl::PostField.content('signature',               query['signature'])
  post_data << Curl::PostField.content('Content-Type',            query['Content-Type'])
  post_data << Curl::PostField.content('Content-Disposition',     query['Content-Disposition'])

  post_data << Curl::PostField.file('file', file.path)

  # post
  c = Curl::Easy.new(action) do |curl|
    # curl.verbose = true
    # can't follow redirect to finalize here because need to pass access_token for auth
    curl.follow_location = false
  end
  c.multipart_form_post = true
  c.http_post(post_data)

  if c.response_code == 303
    # finalize it
    response = Thingiverse::Connection.post(query['success_action_redirect'])
    raise "#{response.code}: #{JSON.parse(response.body)['error']} #{response.headers['x-error']}" unless response.success?
    Thingiverse::Files.new(response.parsed_response)
  else
    raise "#{c.response_code}: #{c.body_str}"
  end
end

#userObject



47
48
49
50
51
# File 'lib/thingiverse/things.rb', line 47

def user
  response = Thingiverse::Connection.get("/users/#{creator['name']}")
  raise "#{response.code}: #{JSON.parse(response.body)['error']}" unless response.success?
  Thingiverse::Users.new response.parsed_response
end