Class: Moo::Client

Inherits:
Object show all
Defined in:
lib/moo/client.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) {|_self| ... } ⇒ Client

Returns a new instance of Client.

Yields:

  • (_self)

Yield Parameters:

  • _self (Moo::Client)

    the object that the method was called on



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/moo/client.rb', line 22

def initialize(options={})
  @oauth_consumer_secret = options[:oauth_consumer_secret]
  @oauth_consumer_key = options[:oauth_consumer_key]

  yield self if block_given?

  # set key/secret to default if not set
  @oauth_consumer_secret ||= @@oauth_consumer_secret
  @oauth_consumer_key ||= @@oauth_consumer_key

  # error if the oauth secret and key have not been set by now
  unless @oauth_consumer_secret and @oauth_consumer_key
    raise "Moo Client requires OAuth consumer key/secret"
  end

  # create the consumer
  @oauth_consumer = OAuth::Consumer.new(
    @oauth_consumer_key,
    @oauth_consumer_secret, {
      site: "https://secure.moo.com",
      request_token_path: "/oauth/request_token.php",
      access_token_path: "/oauth/access_token.php",
      authorize_path: "/oauth/authorize.php" 
    })

  # create the access token
  if options[:oauth_access_token] and options[:oauth_access_token_secret]
    @oauth_access_token = OAuth::AccessToken.new(
      @oauth_consumer,
      options[:oauth_access_token],
      options[:oauth_access_token_secret])
  end
end

Instance Attribute Details

#oauth_access_tokenObject

Returns the value of attribute oauth_access_token.



5
6
7
# File 'lib/moo/client.rb', line 5

def oauth_access_token
  @oauth_access_token
end

#oauth_consumerObject

Returns the value of attribute oauth_consumer.



5
6
7
# File 'lib/moo/client.rb', line 5

def oauth_consumer
  @oauth_consumer
end

#oauth_consumer_keyObject

Returns the value of attribute oauth_consumer_key.



5
6
7
# File 'lib/moo/client.rb', line 5

def oauth_consumer_key
  @oauth_consumer_key
end

#oauth_consumer_secretObject

Returns the value of attribute oauth_consumer_secret.



5
6
7
# File 'lib/moo/client.rb', line 5

def oauth_consumer_secret
  @oauth_consumer_secret
end

Class Method Details

.config {|_self| ... } ⇒ Object

Yields:

  • (_self)

Yield Parameters:

  • _self (Moo::Client)

    the object that the method was called on



18
19
20
# File 'lib/moo/client.rb', line 18

def self.config
  yield self if block_given?
end

.oauth_consumer_key=(key) ⇒ Object



14
15
16
# File 'lib/moo/client.rb', line 14

def self.oauth_consumer_key=(key)
  @@oauth_consumer_key = key
end

.oauth_consumer_secret=(secret) ⇒ Object



10
11
12
# File 'lib/moo/client.rb', line 10

def self.oauth_consumer_secret=(secret)
  @@oauth_consumer_secret = secret
end

Instance Method Details

#create_pack(pack) ⇒ Object



60
61
62
63
64
65
66
# File 'lib/moo/client.rb', line 60

def create_pack(pack)
  call({
    method: "moo.pack.createPack",
    product: pack.product_code.to_s,
    pack: pack.to_json
  })
end

#get_request_token(options) ⇒ Object



56
57
58
# File 'lib/moo/client.rb', line 56

def get_request_token(options)
  @oauth_consumer.get_request_token(oauth_callback: options[:callback])
end

#import_image(image_url) ⇒ Object



89
90
91
92
93
94
# File 'lib/moo/client.rb', line 89

def import_image(image_url)
  call({
    method: "moo.image.importImage",
    imageUrl: image_url
  })
end

#update_pack(pack_id, pack) ⇒ Object



68
69
70
71
72
73
74
# File 'lib/moo/client.rb', line 68

def update_pack(pack_id, pack)
  call({
    method: "moo.pack.updatePack",
    packId: pack_id,
    pack: pack.to_json
  })
end

#upload_image(path) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/moo/client.rb', line 76

def upload_image(path)
  File.open(path) do |image|
    params = {
      method: "moo.image.uploadImage",
      imageFile: UploadIO.new(image, "image/jpeg", "image.jpg")
    }
    request = Net::HTTP::Post::Multipart.new("/api/service/", params)
    http = Net::HTTP.new("www.moo.com", 80)
    response = http.start {|net| net.request(request) }
    handle_response(response)
  end
end